[Numpy-discussion] using boolean arrays as indices

Travis Oliphant oliphant at ee.byu.edu
Mon Mar 27 16:57:02 EST 2006


Michael Sorich wrote:

> If I have a 2D array and I want to use a boolean array to select the 
> rows. If I use a[boolarray] it works, but if I use a[boolarray, :] an 
> exception is raised. Is there a reason for this?

Yes.  Currently, you can only use Boolean indexing using

X[obj]  which is equivalent to (but faster than) X[obj.nonzero()]

There is no way currently to use Boolean indexing on a single axis.  A 
useful specification would need to be made that is consistent with the 
current definition along with an implementation.

You could use instead

a.compress(condition, axis=0)

This selects out sub-arrays along a particular axis.


-Travis





More information about the NumPy-Discussion mailing list