# [Numpy-discussion] the fast way to loop over ndarray elements?

Chao YUE chaoyuejoy@gmail....
Tue Nov 20 10:16:32 CST 2012

```Dear Thouis,

I take some time to check, before I tried with cython, I tried the
np.interp first, and very luckily, it's exeactly what I need.

And with the old written by me, it spend 20 seconds, now it's 0.2 seconds!

Thanks a lot to all you guys.

Chao

On Mon, Nov 19, 2012 at 3:08 PM, Thouis (Ray) Jones <thouis@gmail.com>wrote:

> On Sat, Nov 17, 2012 at 8:28 AM, Chao YUE <chaoyuejoy@gmail.com> 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[0]):
> >     for col in range(data.shape[1]):
> >         for i in range(len(base)-1):
> >             if data[row,col]>=base[i] and data[row,col]<=base[i+1]:
> >
> >
> data[row,col]=lintrans(data[row,col],(base[i],base[i+1]),(target[i],target[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
> > filename:lineno(function)
> >   18047    0.110    0.000      0.110        0.000
> > mathex.py:132(lintrans)
> >   1            12.495  12.495   19.061      19.061
> > mathex.py:196(plot_array_transg)
> >
> >
> > so is there anyway I can speed up this loop?  Thanks for any
> suggestions!!
> >
> > best,
> >
> > Chao
>
> If lintrans() is a linear interpolation, could you use interp?
>
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.interp.html
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

--
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20121120/e909e4b6/attachment.html
```