python - Numba JIT changing results if values are printed -
i started working numba today, because have nested for-loop can take quite while regular python code.
i have macports version of python-2.7 llvm-3.6 , pip version of numba (everything up-to-date)
here code i'm using:
import pandas pd numba import jit numpy import nan, full @jit def movingaverage(adj_close, maxma): ma = full([len(adj_close), maxma], nan, dtype=float64) ind = range( 1, len(adj_close)+1 ) d in ind: m = max( 0, d-maxma-1) adj = adj_close[d-1:m:-1] if (m or d==maxma+1) else adj_close[d-1::-1] cs = adj.cumsum() in range( len(adj) ): ma[d-1][i] = ( cs[i] / (i+1) ) print ma return ma
i'm calculating rolling mean input adj_close
maxma
days.
adj_close
array of values, 1 value per day
i started creating ma
, holder values going calculated. , work out vaules each day individually (note first day can have average involving 1 day, second, 2 , on maxma)
if input adj_close = array(range(5), dtype=float64)
, maxma = 3
right answer follows:
array([[ 0., nan, nan], [ 1., 0.5, nan], [ 2., 1.5, 1.], [ 3., 2.5, 2.], [ 4., 3.5, 3.]])
however, if take out print ma
line, before return of function, returns part of answer:
array([[ nan, nan, nan], [ nan, nan, nan], [ nan, nan, nan], [ 3., 2.5, 2.], [ 4., 3.5, 3.]])
why happening? why @jit needs print between loops answer right? can rid of print statement (that increases runtime)?
edit: i'm accepting @joshadel suggestion , opened issue @ numba's github. i'm, therefore, accepting @mseifert answer workaround solved problem me.
i think numba
strange here because of mixture of python
, nopython
mode. if use python 3.5 returns identical , without print
.
for python 2.7 think problem because for-loop either compiled in nopython
mode (without print) or in python
mode (with print). converted python
when exits loop. that's guessing. tried with:
import pandas pd numba import jit numpy import nan, full import numpy np @jit def movingaverage(adj_close, maxma): ma = full([len(adj_close), maxma], nan, dtype=np.float64) ind = range( 1, len(adj_close)+1 ) d in ind: m = max( 0, d-maxma-1) adj = adj_close[d-1:m:-1] if (m or d==maxma+1) else adj_close[d-1::-1] cs = adj.cumsum() in range( len(adj) ): ma[d-1][i] = ( cs[i] / (i+1) ) if d == ind[-1]: return ma # notice return after last loop before loop terminates. #return ma
and return:
array([[ 0., nan, nan], [ 1., 0.5, nan], [ 2., 1.5, 1.], [ 3., 2.5, 2.], [ 4., 3.5, 3.]])
this not effient way because of recalculation of len(adj_close)+1
. stored somewhere.
Comments
Post a Comment