I have:
import numpy as np
a = np.array([[ 4, 99, 2],
[ 3, 4, 99],
[ 1, 8, 7],
[ 8, 6, 8]])
Why is
a[[True, True, False, False], [1,2]]
Equal to
array([99, 99])
And not
array([99, 2],
[4, 99])
Since I am selecting the first two rows using a boolean mask and the 2nd and 3rd columns using fancy indexing? Especially since calling
a[[True, True, False, False],:][:, [1,2]]
gives me my expected result. Im guessing its some sort of broadcasting rule but it isn't apparent to me. Thanks!
A boolean array or list evaluates as though
where
had converted it to an index array:So this is picking
a[0,1]
anda[1,2]
, a 'pair-wise' selection.The block is indexed with arrays (or list equivalents) that broadcast against each other to produce a (2,2) array:
This case is equivalent to a 2 stage indexing:
a[[0,1],:][:,[1,2]]
I'm using np version 12. There have been some changes in boolean index over the recent releases. For example, if the length of the boolean isn't right, it runs, but gives a warning (this part is new).
Changes for v 13 are described in:
https://docs.scipy.org/doc/numpy-dev/release.html#boolean-indexing-changes
I think it works like the following:
Now, once we index
a
with the boolean maskbo
, we get:Since,
bo
evaluates to[1, 1, 0, 0]
, this will just select first two rows ofa
.Now, we apply
boc
i.e.[1, 2]
in combination with the row selecting maskbo
.Here, the mask
boc
is applied to the already fetched rows. And it selects second element from first row, third element from second row yielding[99, 99]
.But, interestingly if you do something like:
In this case, numpy broadcasts yielding the indices
[(1,1), (1,2)]