NumPy Boolean Indexing

In NumPy, boolean indexing allows us to filter elements from an array based on a specific condition.

We use boolean masks to specify the condition.

Before we learn about boolean indexing, we need to know about boolean masks.


Boolean Masks in NumPy

Boolean mask is a numpy array containing truth values (True/False) that correspond to each element in the array.

Suppose we have an array named array1.

array1 = np.array([12, 24, 16, 21, 32, 29, 7, 15])

Now let's create a mask that selects all elements of array1 that are greater than 20.

boolean_mask = array1 > 20

Here, array1 > 20 creates a boolean mask that evaluates to True for elements that are greater than 20, and False for elements that are less than or equal to 20.

The resulting mask is an array stored in the boolean_mask variable as:

[False, True, False, True, True, True, False, False]

1D Boolean Indexing in NumPy

Boolean Indexing allows us to create a filtered subset of an array by passing a boolean mask as an index.

The boolean mask selects only those elements in the array that have a True value at the corresponding index position.

Let's create a boolean indexing of the boolean mask in the above example.

array1[boolean_mask]

This results in

[24, 21, 32, 29]

Now let's see another example.

We'll use the boolean indexing to select only the odd numbers from an array.

import numpy as np

# create an array of numbers
array1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# create a boolean mask
boolean_mask = array1 % 2 != 0

# boolean indexing to filter the odd numbers
result = array1[boolean_mask]

print(result)

# Output: [ 1  3  5  7 9]

In this example, we have used the boolean indexing to select only the odd numbers from the array1 array.

Here, the expression numbers % 2 != 0 is a boolean mask. If the elements of array1 meet the condition specified in the boolean mask, it replaces the element (odd numbers) with True, and even numbers with False.

With boolean indexing, a filtered array with only the True valued elements is returned. Hence, we get an array with odd numbers.


Example: 1D Boolean Indexing in NumPy

import numpy as np

# create an array of integers
array1 = np.array([1, 2, 4, 9, 11, 16, 18, 22, 26, 31, 33, 47, 51, 52])

# create a boolean mask using combined logical operators
boolean_mask = (array1 < 10) | (array1 > 40)

# apply the boolean mask to the array 
result = array1[boolean_mask]

print(result)

# Output: [ 1  2  4  9 47 51 52]

Here, we have created a boolean mask using the | operator to select all the elements in array1 that are less than 10 or greater than 40.


Modify Elements Using Boolean Indexing

In NumPy, we can use boolean indexing to modify elements of the array. For example,

import numpy as np

# create an array of numbers
numbers = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# make a copy of the array
numbers_copy = numbers.copy()

# change all even numbers to 0 in the copy
numbers_copy[numbers % 2 == 0] = 0

# print the modified copy
print(numbers_copy)

# Output: [1 0 3 0 5 0 7 0 9 0]

Here, numbers_copy[numbers % 2 == 0] accesses all even numbers of the array and then we have assigned 0 to those numbers.


2D Boolean Indexing in NumPy

Boolean indexing can also be applied to multi-dimensional arrays in NumPy.

Let's see an example.

import numpy as np

# create a 2D  array
array1 = np.array([[1, 7, 9], 
                    [14, 19, 21], 
                    [25, 29, 35]])

# create a boolean mask based on the condition 
# that elements are greater than 9
boolean_mask = array1 > 9

# select only the elements that satisfy the condition
result = array1[boolean_mask]

print(result)

Output

[14 19 21 25 29 35]

In this example, we have applied boolean indexing to the 2D array named array1.

We then created boolean_mask based on the condition that elements are greater than 9. The resulting mask is,

[[False, False, False], 
 [ True,  True,  True], 
 [ True,  True,  True]]

We then use this boolean mask to index array1, which returns a flattened 1D array containing only the elements that satisfy the condition.

[14, 19, 21, 25, 29, 35]