[Numpy-discussion] efficient use of numpy.where() and .any()

Pierre GM pgmdevlist at gmail.com
Mon Apr 23 12:32:18 EDT 2007


> > Have you tried nonzero() ?
>
> Nonzero isn't quite what I'm after, as the tests are more complicated
> than what I illustrated in my example.

Tests such as (a<0)&(b>1) will give you arrays of booleans. The nonzero give 
you where the two conditions are met (viz, where the results is True, or 1)

> > a[a<0] = numpy.random.normal(0,1)
>
> This is a neat construct that I didn't realize was possible.  However,
> it has the undesirable (in my case) effect of placing a single new
> random number in each locations where a<0.  While this could work, I
> ideally need a different random number chosen for each replaced value.
> Does that make sense?

Completely. So what about the slightly more complicated:
test = a<0
if test .any()
   a[test] = numpy.random.normal(0,1,size=len(test.nonzero()[0]))

test.nonzero() outputs a tuple of indices. test.nonzero()[0] gives you the 
indices along the first axis, len(test.nonzero()[0]) the number of elements 
where the condition is met. You could also get the same result with something 
like
test = a<0
if test .any()
   a[test] = numpy.random.normal(0,1,size=test.sum())

Actually, the following simpler solution  works as well:

a[a<0] = numpy.random.normal(0,1,size=a.size)



More information about the NumPy-Discussion mailing list