[Numpy-discussion] the fast way to loop over ndarray elements?
ben.root at ou.edu
Sat Nov 17 08:40:46 EST 2012
On Saturday, November 17, 2012, Chao YUE wrote:
> Dear all,
> I need to make a linear contrast of the 2D numpy array "data" from an
> interval to another, the approach is:
> I have another two list: "base" & "target", then I check for each ndarray
> element "data[i,j]",
> if base[m] =< data[i,j] <= base[m+1], then it will be linearly converted
> to be in the interval of (target[m], target[m+1]),
> using another function called "lintrans".
> #The way I do is to loop each row and column of the 2D array, and finally
> loop the intervals constituted by base list:
> for row in range(data.shape):
> for col in range(data.shape):
> for i in range(len(base)-1):
> if data[row,col]>=base[i] and data[row,col]<=base[i+1]:
> break #use break to jump out of loop as the data have to
> be ONLY transferred ONCE.
> Now the profiling result shows that most of the time has been used in this
> loop over the array ("plot_array_transg"),
> and less time in calling the linear transformation fuction "lintrans":
> ncalls tottime percall cumtime percall
> 18047 0.110 0.000 0.110 0.000
> 1 12.495 12.495 19.061 19.061
> so is there anyway I can speed up this loop? Thanks for any suggestions!!
If the values in base are ascending, you can use searchsorted() to find out
where values from data can be placed into base while maintaining order.
Don't know if it is faster, but it would certainly be easier to read.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion