Search code examples
pythonnumpyarray-broadcasting

Boolean Indexing in Numpy Arrays


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]])

Solution

  • 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]]])