[Numpy-discussion] picking elements with boolean masks

Neal Becker ndbecker2@gmail....
Mon Mar 25 07:24:23 CDT 2013


starting with a NxM array, I want to select elements of the array using a set of 
boolean masks.  The masks are simply where the indexes have a 0 or 1 in the 
corresponding bit position.  For example, consider the case where M = 4.

all_syms = np.arange (4)
all_bits = np.arange (2)
bit_mask = (all_syms[:,np.newaxis] >> all_bits) & 1
mask0 = bit_mask == 0
mask1 = bit_mask == 1

Maybe there's a more straightforward way to generate these masks.  That's not my 
question. 

In [331]: mask1
Out[331]: 
array([[False, False],
       [ True, False],
       [False,  True],
       [ True,  True]], dtype=bool)

OK, now I want to use this mask on D
In [333]: D.shape
Out[333]: (32400, 4)

Just to simplify, let's just try the first row of D

In [336]: D[0]
Out[336]: array([ 0.,  2.,  2.,  4.])

In [335]: D[0][mask1[...,0]]
Out[335]: array([ 2.,  4.])

that worked fine.  But I want not just to apply one of the masks in the set 
(mask1 is [4,2], it has 2 masks), I want the results of applying all the masks 
(2 in this case)


In [334]: D[0][mask1]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-334-243c7a5e45a4> in <module>()
----> 1 D[0][mask1]

ValueError: boolean index array should have 1 dimension

Any ideas what's the best approach here?



More information about the NumPy-Discussion mailing list