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

Robert Kern robert.kern@gmail....
Mon Apr 23 11:23:49 CDT 2007


Mark.Miller wrote:
> Pierre GM wrote:

>> 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?

Certainly. How about this?

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

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list