damon
damon

Reputation: 8477

using boolean array indexing in numpy causes ValueError

I was trying out indexing using boolean arrays

def boolean_array_indexing_each_dim1():
    a = np.array(
                 [
                  ['a','b','c','d'],
                  ['e','f','g','h'],
                  ['i','j','k','l'],
                  ['m','n','o','p']
                  ]
                 )
    print('a=\n',a.shape,'\n',a)
    b1 = np.array([True,True,True,False]) #gives error
    #b1 = np.array([True,False,True,False]) #works
    print('b1=\n',b1.shape,'\n',b1)
    b2 = np.array([True,False,True,False])
    print('b2=\n',b2.shape,'\n',b2)  
    selected = a[b1,b2]
    print('selected=\n',selected.shape,'\n',selected)

the array b1 = np.array([True,True,True,False]) causes a 'ValueError shape mismatch: objects cannot be broadcast to a single shape'

The array b1 = np.array([True,False,True,False]) however works and produces a result ' ['a' 'k']'

why does this error happen? can someone please tell ?

Upvotes: 0

Views: 249

Answers (1)

Frank M
Frank M

Reputation: 1580

The reason is your first b1 array has 3 True values and the second one has 2 True values. These are equivalent to indexing by [0,1,2], [0,2] respectively. Numpy's indexing "works" by constructing pairs of indexes from the sequence of positions in the b1 and b2 arrays. For the case of [0,1,2], [0,2] it constructs index pairs (0,0), (1,2) but then there's no partner for the final 2 in b1, so it raises ValueError. Your alternate b1 works because it happens to have the same number of True values as your b2.

I suspect what you intended to accomplish is

selected = a[b1,:][:,b2]

This would consistently slice the array with b1 along axis 0, and then slice it with b2 along axis 1.

Upvotes: 2

Related Questions