[Numpy-discussion] how do I speed up this?

Timothy Hochberg tim.hochberg@ieee....
Tue Nov 27 13:50:55 CST 2007


On Nov 27, 2007 12:41 PM, Giorgio F. Gilestro <giorgio@gilestro.tk> wrote:

> Thanks Tim,
> shape of the array is variable but follows the rule (x, y*1440) with
> both x an y being values greater than 1 (usually around 10 or 20).


OK. That's helpful.


> So as result second dimension is always much bigger. (and the bug you
> foresaw is actually taken care of)


Are you sure? This still looks problematic.


>
> I figured out that somehow removing the unnecessary *1 multiplication
> after the boolean verification will increase speed by almost 100% (wow!).
> I guess if I can get rid of the iteration too and use some internal
> function I'd probably get even faster.
> Could .flat() help me somehow?


I doubt it. I'll look at this a little later if I can find some time and see
what I can come up with.



>
>
> Timothy Hochberg wrote:
> >
> >
> > On Nov 27, 2007 11:38 AM, Giorgio F. Gilestro <giorgio@gilestro.tk
> > <mailto:giorgio@gilestro.tk>> wrote:
> >
> >     Hello everyone,
> >
> >     ma and new_ma are bi-dimensional array with shape (a1, a2) on
> >     which I am
> >     performing the following iteration:
> >
> >     for fd in range(a1):
> >         new_ma[fd] = [( ma[fd][i-5:i+5].sum() == 0 )*1 for i in range
> >     (a2)]
> >
> >
> > This looks like it's probably buggy; when, for example,, a2 == 0, I
> > don't think ma[fd][-5:5] is going to return what you want.
> >
> >
> >     Is there any faster more elegant way to do that with numpy?
> >
> >
> > There are faster ways, I'm not sure that they are more elegant. It
> > looks like what you want is essentially a moving average, possible
> > with periodic boundary conditions. There are a few different tricks
> > that can make this fast; which is appropriate depends on what the
> > shape of ma is expected to be. If 'a1' if large, then you can
> > profitably remove the outer loop; something vaguely like:
> >
> > for i in range(a2):
> >     new_ma[:, i] = ( ma[fd][i-5:i+5].sum() == 0)
> >
> > (This is still buggy as above, since I'm not sure what kind of
> > boundary conditions you need).
> >
> > If that's not applicable, another approach is to loop over the offset,
> > in this case -5 to 5, and remove the loop over a2. That method is more
> > complicated and I'm going to avoid describing it in detail unless
> > needed. One might even be able to do both, but that's probably overkill.
> >
> > HTH
> >
> > --
> > .  __
> > .   |-\
> > .
> > .  tim.hochberg@ieee.org <mailto:tim.hochberg@ieee.org>
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion@scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> >
>
>
> --
> giorgio@gilestro.tk
> http://www.cafelamarck.it
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
.  __
.   |-\
.
.  tim.hochberg@ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20071127/f1f6f587/attachment-0001.html 


More information about the Numpy-discussion mailing list