Boolean index did not match indexed array along dimension 0

avatar
Borislav Hadzhiev

Last updated: Jul 6, 2023
4 min

banner

# Boolean index did not match indexed array along dimension 0

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

index error boolean index did not match indexed array along dimension

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]

make sure boolean array has same length as other array

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.

# Using the boolean array to slice a part of the other array

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.

# Conditionally creating the boolean array

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.

# Additional Resources

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

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev