python - Fastest way to initialize numpy array with values given by function -
i interested in ((d1,d2)) numpy arrays (matrices) question makes sense arrays more axes. have function f(i,j) , i'd initialize array operation of function
a=np.empty((d1,d2)) in range(d1): j in range(d2): a[i,j]=f(i,j)
this readable , works wondering if there faster way since array large , have optimize bit.
one way use np.fromfunction
. code can replaced line:
np.fromfunction(f, shape=(d1, d2))
this implemented in terms of numpy functions , should quite bit faster python for
loops larger arrays.
Comments
Post a Comment