[Numpy-discussion] fancy indexing/broadcasting question

Mark.Miller mpmusu at cc.usu.edu
Sat Jul 7 15:41:52 EDT 2007


That seems to do the trick.  Runtimes are reduced by 15-20% in my code.



Robert Kern wrote:
> The reason is that tmp1 is no longer a mask into RNDarray, but into
> RNDarray[tmp1] (the old tmp1). For something as small as (50, 50) and simple
> criteria (no looping), the first version will probably be faster than any
> attempt to optimize it.
> 
> However, if you do have larger arrays or slower criteria, you can reduce the
> size of the re-evaluated array pretty simply. I'm still not sure it will be
> faster, but here it is:
> 
> 
> import numpy
> normal = numpy.random.normal
> 
> RNDarray = normal(25, 15, (50, 50))
> badmask = (RNDarray < 0) | (RNDarray > 25)
> nbad = badmask.sum()
> while nbad > 0:
>   new = normal(25, 15, size=nbad)
>   RNDarray[badmask] = new
>   newbad = (new < 0) | (new > 25)
>   badmask[badmask] = newbad
>   nbad = newbad.sum()
> 




More information about the NumPy-Discussion mailing list