2

I created 2D array and I did boolean indexing with 2 bool index arrays. first one is for axis 0, next one is for axis 1.

I expected that values on cross True and True from each axis are selected like Pandas. but the result is not.

I wonder how it works that code below. and I want to get the link from official numpy site describing this question.

Thanks in advance.

a = np.arange(9).reshape(3,3)
a
----------------------------
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
a[ [True, False, True], [True, False, True] ]
--------------------------
array([0, 8])

My expectation is [0, 6, 2, 8]. (I know how to get the result that I expect.)

1
  • It's like arr[[0,2],[0,2]]. arr[[[0],[2]], [0,2]] for the (2,2) block. Commented Nov 3, 2021 at 14:34

3 Answers 3

2
In [20]: a = np.arange(9).reshape(3,3)

If the lists are passed to ix_, the result is 2 arrays that can be used, with broadcasting to index the desired block:

In [21]: np.ix_([True, False, True], [True, False, True] )
Out[21]: 
(array([[0],
        [2]]),
 array([[0, 2]]))
In [22]: a[_]
Out[22]: 
array([[0, 2],
       [6, 8]])

This isn't 1d, but can be easily raveled.

Trying to make equivalent boolean arrays does not work:

In [23]: a[[[True], [False], [True]], [True, False, True]]
Traceback (most recent call last):
  File "<ipython-input-23-26bc93cfc53a>", line 1, in <module>
    a[[[True], [False], [True]], [True, False, True]]
IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed

Boolean indexes must be either 1d, or nd matching the target, here (3,3).

In [26]: np.array([True, False, True])[:,None]& np.array([True, False, True])
Out[26]: 
array([[ True, False,  True],
       [False, False, False],
       [ True, False,  True]])
Sign up to request clarification or add additional context in comments.

Comments

0

What you want is consecutive slices: a[[True, False, True]][:,[True, False, True]]

a = np.arange(9).reshape(3,3)
x = [True, False, True]
y = [True, False, True]
a[x][:,y]

as flat array

a[[True, False, True]][:,[True, False, True]].flatten(order='F')

output: array([0, 6, 2, 8])

alternative

NB. this requires arrays for slicing

a = np.arange(9).reshape(3,3)
x = np.array([False, False, True])
y = np.array([True, False, True])
a.T[x&y[:,None]]

output: array([0, 6, 2, 8])

3 Comments

Thank you for your answer. but I already know how to get the result that I expected. I just want to know reason why the result comes out.
I am not sure what is your concern then, the syntax you used is roughly equivalent to a[[0,2],[0,2]], maybe the doc you're looking for is the numpy page on basics indexing?
Thanks very much for your reply. I agree that a[[True, False, True], [True, False, True]] is eqauivalent to a[[0,2], [0,2]], so the result can be selected (0,2), (0, 2) right? but when I change the code like this a[[True, False, False], [True, True, True]] it must be equivalent to a[[0], [0, 1, 2]] and it means (0, 0), (0, 1), (0, 2). I can't understand the way to select elements. (Sorry I am not English speaker.)
0

Here is my attempt to explain ... let's say 2 boolean arrays are used for the 2 axes, b1 and b2, i.e., x[b1,b2].

It appears that NumPy would construct (True, True) pairs from b1 and b2, one from b1 and the other from b2. It records which index in b1 and which in b2 that form the (True, True) pair. This continues until all (True, True) pairs have been formed. The index pairs are then used to extract elements from x[ : , : ].

If there is an unmatched (True, ?) or (?, True) pair, then an IndexError results.

E.g., b1 has [True, False, True, False] and b2 has [True, True, False, False]. The reference to x[b1,b2] would result in the following:

  • The 1st (True, True) pair comes from [0,0].
  • The 2nd (True, True) pair comes from [2,1].

x[b1,b2] is then the same as [ x[0,0] x[2,1] ].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.