[Numpy-discussion] how to select indices from boolean array
Thu Oct 25 07:44:30 CDT 2007
wBernhard Voigt <email@example.com> kirjoitti:
> is nonzero the appropriate function to get the indexes of a boolean array?
> foo = numpy.random.random(10000)
> cut = foo > .5
> indexes = cut.nonzero()
I think writing
indices = where(foo > 5)
might be more clear, although it does the same thing as .nonzero().
> Another question: Why is the the type returned by nonzero a tuple and not an
> ndarray of the same shape as the input (or self if used on an object)?
Because indexing with booleans in numpy works differently from indexing with numbers, especially for multidimensional arrays. You want to have
( foo[foo > 5] == foo[where(foo > 5)] ).all()
so where/nonzero returns a tuple that contains 1D-arrays, one per dimension, which together enumerate the relevant entries. (IIRC, in Matlab, find returns indices in the flattened array.)
More information about the Numpy-discussion