Re: [Numpy-discussion] how to select indices from boolean array
wBernhard Voigt <bernhard.voigt@gmail.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()[0]
I think writing indices = where(foo > 5)[0] 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.) -- Pauli Virtanen --
participants (1)
-
Pauli Virtanen