How can I filter a NumPy array?

How can I filter a NumPy array?

Filtering a NumPy array involves selecting specific elements from the array based on certain criteria. This can be achieved by using the built-in filter function in NumPy or by writing custom functions to apply the desired filtering conditions. The filter function takes in an array and a boolean mask as inputs and returns a new array containing only the elements that satisfy the mask. This allows for efficient and precise filtering of large arrays, making it a useful tool in data manipulation and analysis.

Filter a NumPy Array (4 Examples)


You can use the following methods to filter the values in a NumPy array:

Method 1: Filter Values Based on One Condition

#filter for values less than 5
my_array[my_array < 5]

Method 2: Filter Values Using “OR” Condition

#filter for values less than 5 or greater than 9
my_array[(my_array < 5) | (my_array > 9)]

Method 3: Filter Values Using “AND” Condition

#filter for values greater than 5 and less than 9
my_array[(my_array > 5) & (my_array < 9)]

Method 4: Filter Values Contained in List

#filter for values that are equal to 2, 3, 5, or 12
my_array[np.in1d(my_array, [2, 3, 5, 12])]

This tutorial explains how to use each method in practice with the following NumPy array:

import numpy as np

#create NumPy array
my_array = np.array([1, 2, 2, 3, 5, 6, 7, 10, 12, 14])

#view NumPy array
my_array

array([ 1,  2,  2,  3,  5,  6,  7, 10, 12, 14])

Example 1: Filter Values Based on One Condition

The following code shows how to filter values in the NumPy array based on just one condition:

#filter for values less than 5
my_array[(my_array < 5)]

array([1, 2, 2, 3])

#filter for values greater than 5
my_array[(my_array > 5)]

array([ 6,  7, 10, 12, 14])

#filter for values equal to 5
my_array[(my_array == 5)]

array([5])

Example 2: Filter Values Using “OR” Condition

The following code shows how to filter values in the NumPy array using an “OR” condition:

#filter for values less than 5 or greater than 9
my_array[(my_array < 5) | (my_array > 9)]

array([ 1,  2,  2,  3, 10, 12, 14])

Example 3: Filter Values Using “AND” Condition

The following code shows how to filter values in the NumPy array using an “AND” condition:

#filter for values greater than 5 and less than 9
my_array[(my_array > 5) & (my_array < 9)]

array([6, 7])

This filter returns the values in the NumPy array that are greater than 5 and less than 9.

Example 4: Filter Values Contained in List

The following code shows how to filter values in the NumPy array that are contained in a list:

#filter for values that are equal to 2, 3, 5, or 12
my_array[np.in1d(my_array, [2, 3, 5, 12])]

array([ 2,  2,  3,  5, 12])

This filter returns only the values that are equal to 2, 3, 5, or 12.

Note: You can find the complete documentation for the NumPy in1d() function .

Additional Resources

The following tutorials explain how to perform other common filtering operations in Python:

Cite this article

stats writer (2024). How can I filter a NumPy array?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-filter-a-numpy-array/

stats writer. "How can I filter a NumPy array?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-filter-a-numpy-array/.

stats writer. "How can I filter a NumPy array?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-filter-a-numpy-array/.

stats writer (2024) 'How can I filter a NumPy array?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-filter-a-numpy-array/.

[1] stats writer, "How can I filter a NumPy array?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I filter a NumPy array?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top