2

I was learning boolean indexing in numpy and came across this. How is the indexing below not producing a Index Error as for axis 0 as there are only two blocks?

x = np.arange(30).reshape(2, 3, 5)
x
array([[[ 0,  1,  2,  3,  4],
    [ 5,  6,  7,  8,  9],
    [10, 11, 12, 13, 14]],

   [[15, 16, 17, 18, 19],
    [20, 21, 22, 23, 24],
    [25, 26, 27, 28, 29]]])

x[[[True, True, False], [False, True, True]]]
array([[ 0,  1,  2,  3,  4],
   [ 5,  6,  7,  8,  9],
   [20, 21, 22, 23, 24],
   [25, 26, 27, 28, 29]])
1
  • The boolean indexing docs suggest using np.nonzero(...) to see the equivalent advanced indexing arrays, e.g. (array([0, 0, 1, 1]), array([0, 1, 1, 2])). So the equivalent operation is x[[0,0,1,1], [0,1,1,2], :] Commented Feb 27 at 18:41

1 Answer 1

3

You are performing boolean array indexing, which is fine.

You would have an indexing error with:

#  first dimension      second dimension
x[[True, True, False], [False, True, True]]
# IndexError: boolean index did not match indexed array along dimension 0;
# dimension is 2 but corresponding boolean dimension is 3

However, in your case, you have an extra set of brackets, which makes it index only the first dimension, using an array:

# [             first dimension            ]
#  [ second dimension], [ second dimension]
x[[[True, True, False], [False, True, True]]]

This means, that using a 2x3 array, you request in the second dimension, for the first "row" [True, True, False], and for the second "row" [False, True, True].

Since your array shape matches the first two dimensions, this is valid, and more or less equivalent to:

np.concatenate([x[0][[True, True, False]],
                x[1][[False, True, True]]])
Sign up to request clarification or add additional context in comments.

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.