The NumPy library in Python is a popular library for working with arrays. Boolean masking, also called boolean indexing, is a feature in Python NumPy that allows for the filtering of values in numpy
arrays.
There are two main ways to carry out boolean masking:
The first method returns an array with the required results. In this method, we pass a condition in the indexing brackets, []
, of an array. The condition can be any comparison, like arr > 5
, for the array arr
.
The code snippet given below shows us how we can use this method.
arr[arr > 5]
arr
: This is the array that we are querying.arr > 5
is the criterion with which values in the arr
array will be filtered.This method returns a NumPy array, ndarray
, with values that satisfy the given condition. The line in the example given above will return all the values in arr
that are greater than 5
.
Let's try out this method in the following example:
# importing NumPyimport numpy as np# Creating a NumPy arrayarr = np.arange(15)# Printing our array to observeprint(arr)# Using boolean masking to filter elements greater than or equal to 8print(arr[arr >= 8])# Using boolean masking to filter elements equal to 12print(arr[arr == 12])
numpy
library.numpy
array that contains integers from 1 to 15 using the arange()
function, and then store it in the arr
array.arr
array.arr
that are greater than or equal to eight. Then, we print the resulting array.arr
that are equal to 12
. Then, we print the resulting array.The second method returns a boolean array that has the same size as the array it represents. A boolean array only contains the boolean values of either True
or False
. This boolean array is also called a mask array, or simply a mask. We'll discuss boolean arrays in more detail in the "Return value" section.
The code snippet given below shows us how to use this method:
mask = arr > 5
arr
: This is the array that we are querying.arr > 5
is our condition.The line in the code snippet given above will:
arr
. This array will only contain the values True
and False
. All the True
values represent elements in the same position in arr
that satisfy our condition, and all the False
values represent elements in the same position in arr
that do not satisfy our condition.mask
array.The mask
array can be passed in the index brackets of arr
to return the values that satisfy our condition. We will see how this works in our coding example.
Let's try out this method in the following example:
# importing NumPyimport numpy as np# Creating a NumPy arrayarr = np.array([[ 0, 9, 0],[ 0, 7, 8],[ 6, 0, 1]])# Printing our array to observeprint(arr)# Creating a mask arraymask = arr > 5# Printing the mask arrayprint(mask)# Printing the filtered array using both methodsprint(arr[mask])print(arr[arr > 5])
numpy
library.numpy
array that contains some integers and store it in the arr
array.arr
array.arr
that are greater than 5
. Then, we store this boolean array in a mask
array.mask
array.mask
array to filter the elements in arr
that are greater than 5
.arr
that are greater than 5
.Note: The results from both the methods are the same.
Free Resources