Indexing with boolean arrays into multidimensional arrays using numpy
Your array consists of:
0 1 2 34 5 6 78 9 10 11
One way of indexing it would be using a list of integers, specifying which rows/columns to include:
>>> i1 = [1,2]>>> i2 = [0,2]>>> a[i1,i2]array([ 4, 10])
Meaning: row 1 column 0, row 2 column 2
When you're using boolean indices, you're telling which rows/columns to include and which ones not to:
>>> b1 = [False,True,True] # 0:no, 1:yes, 2:yes ==> [1,2]>>> b2 = [True,False,True,False] # 0:yes, 1:no, 2:yes, 3:no ==> [0,2]
As you can see, this is equivalent to the i1
and i2
shown above. Hence, a[b1,b2]
will have the same result.
Note also that the operation above is only possible because both b1
and b2
have the same number of True
values (so, they represent two arrays of the same length when expressed in the integer form).