2

I am learning numpy from the start page : https://numpy.org/devdocs/user/quickstart.html There is a confusing part that makes me stop.

>>> a = np.arange(12).reshape(3, 4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> b1 = np.array([False, True, True])         # first dim selection
>>> b2 = np.array([True, False, True, False])  # second dim selection
>>> a[b1, b2]
array([ 4, 10])

Could you please provide any hints or explains to help me understand this logic? The output that I expect is

array([[ 4,  6],
       [ 8, 10]])
Z.Lun
  • 247
  • 1
  • 2
  • 20

1 Answers1

1

An easy way to achieve what you want is to apply your masks sequentially. In other words,

a[b1,:][:,b2]

# [[ 4  6]
# [ 8 10]]

It seems like you are assuming that b1 determines which rows must be selected and b2 determines which columns must be selected. This is a fair assumption, but numpy masks broadcast differently. What is in fact happening when you pass both at the same time, is that they get converted to indices. So

b1 = np.array([False, True, True]) -> [1,2]
b2 = np.array([True, False, True, False]) -> [0,2]

Which indeed gives the "weird" behavior:

b1 = [1,2]
b2 = [0,2]

a[b1,b2] # [ 4 10]
Thomas Wagenaar
  • 6,489
  • 5
  • 30
  • 73