[Numpy-discussion] Getting indices from numpy array with condition
Scott Sinclair
scott.sinclair.za@gmail....
Wed Nov 19 05:24:31 CST 2008
2008/11/19 Robert Kern <robert.kern@gmail.com>:
> On Wed, Nov 19, 2008 at 01:31, David Warde-Farley <dwf@cs.toronto.edu> wrote:
>> On 18-Nov-08, at 3:06 PM, Robert Kern wrote:
>>
>>> I like to discourage this use of where(). For some reason, back in
>>> Numeric's days, where() got stuck with two functionalities. nonzero()
>>> is the preferred function for this functionality. IMO, where(cond,
>>> if_true, if_false) should be the only use of where().
>>
>> Hmm. nonzero() seems semantically awkward to be calling on a boolean
>> array, n'est pas?
>
> Why? In Python and numpy, False==0 and True==1.
Well, using nonzero() isn't actually all that obvious until you
understand that 1) a conditional expression like (a < 3) returns a
boolean array *and* 2) that False==0 and True==1. These two things
are not necessarily known to users who are scientists/engineers etc.
I've added an example of this use case at
http://docs.scipy.org/numpy/docs/numpy.core.fromnumeric.nonzero/ and
added an FAQ http://www.scipy.org/FAQ , so that there's something to
point to in future.
Cheers,
Scott
More information about the Numpy-discussion
mailing list