Boolean index did not match indexed array along dimension 0

Borislav Hadzhiev

Last updated: Jul 6, 2023

Reading timeยท4 min

The NumPy **"IndexError: boolean index did not match indexed array along
dimension 0; dimension is X but corresponding boolean dimension is Y"** occurs
when you try to index an array using a boolean array of a different length.

To solve the error, make sure the boolean array has the same length as the array you're trying to index.

Here is an example of how the error occurs.

main.py

`import numpy as np array = np.array([1, 2, 3, 4]) bool_array = np.array([True, False, True]) # โ๏ธ IndexError: boolean index did not match indexed array along dimension 0; dimension is 4 but corresponding boolean dimension is 3 print(array[bool_array])`

Notice that the first array stores 4 elements, however, the boolean array stores only 3 boolean values.

The mismatch in the length of the arrays causes the error.

**NumPy expects that the boolean array will have exactly as many elements as the
array that we're trying to index.**

To solve the error, you have to track down where the boolean array got assigned fewer/more values than the other array and correct the assignment.

main.py

`import numpy as np array = np.array([1, 2, 3, 4]) bool_array = np.array([True, False, True, False]) print(array[bool_array]) # ๐๏ธ [1 3]`

The boolean array has the same length as the other array, so everything works as expected.

If you need to check the length of the arrays, use the len function.

main.py

`import numpy as np array = np.array([1, 2, 3, 4]) print(len(array)) # ๐๏ธ 4 bool_array = np.array([True, False, True, False]) print(len(bool_array)) # ๐๏ธ 4 print(array[bool_array]) # ๐๏ธ [1 3]`

When you index an array with a boolean array, a new array is returned containing
only the `True`

elements.

In other words, NumPy checks if the boolean at index

`0`

is `True`

, if it is, then the element at index `0`

of the other array is returned, otherwise, nothing is returned.The process is repeated for each element of the boolean array.

Therefore, it has to contain exactly as many elements as the array we're trying to index.

If you have a shorter boolean array, you can use it to slice a part of the other array.

For example, the following boolean array has a length of `3`

whereas the array
we want to slice has a length of `4`

.

main.py

`import numpy as np array = np.array([1, 2, 3, 4]) array2 = array[:-1] print(array2) # ๐๏ธ [1 2 3] bool_array = np.array([True, False, True]) print(array2[bool_array]) # ๐๏ธ [1 3]`

We used array slicing to exclude the last element of the array.

Now the new array and the boolean array contain 3 elements, so slicing is allowed.

We could also get a slice of the array that starts at the second element (index
`1`

).

main.py

`import numpy as np array = np.array([1, 2, 3, 4]) array2 = array[1:] print(array2) # ๐๏ธ [2 3 4] bool_array = np.array([True, False, True]) print(array2[bool_array]) # ๐๏ธ [2 4]`

The new array excludes the first element and only contains 3 elements, so everything works as expected.

Either way, what's important is that the two arrays have the same number of elements.

In most cases, you will have to conditionally create the boolean array.

main.py

`import numpy as np array = np.array([1, 2, 3, 4]) bool_array = array % 2 == 0 print(bool_array) # ๐๏ธ [False True False True] print(array[bool_array]) # ๐๏ธ [2 4]`

We used the modulo operator to check if each number in the array is even.

The modulo (%) operator returns the remainder from the division of the first value by the second.

The boolean array contains a

`False`

value for each odd number and `True`

for each even number.We can then safely index the other array using the boolean array because both arrays contain 4 elements.

You can also use a list comprehension to achieve the same result.

main.py

`import numpy as np array = np.array([1, 2, 3, 4]) bool_array = [element % 2 == 0 for element in array] print(bool_array) # ๐๏ธ [False, True, False, True] print(array[bool_array]) # ๐๏ธ [2 4]`

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current element is an even number and return the result.

The boolean array contains a `True`

or `False`

value for each element in the
original array, so everything works as expected.

You can learn more about the related topics by checking out the following tutorials:

- Only one element tensors can be converted to Python scalars
- ValueError: cannot reshape array of size X into shape Y
- TypeError: Image data cannot be converted to float [Solved]
- OverflowError: Python int too large to convert to C long
- ValueError: DataFrame constructor not properly called [Fix]
- ufunc 'add' did not contain loop with signature matching types
- TypeError: Field elements must be 2- or 3-tuples, got 1
- ValueError: Expected 2D array, got 1D array instead [Fixed]
- ValueError: Found array with dim 3. Estimator expected 2
- AttributeError: Can only use .dt accessor with datetimelike values
- Get the column names of a NumPy ndarray in Python
- Finding the Range of NumPy Array elements in Python
- Convert Epoch to Datetime in a Pandas DataFrame
- Pandas: Strip whitespace from Column Headers in DataFrame