ADVERTISEMENT
ADVERTISEMENT

How to Filter NumPy Array?

Array filtering in NumPy is a process by which you can select elements from an array that meet certain conditions. Array filtering in NumPy is a process by which you can select elements from an array that meet certain conditions. This is often achieved by applying a Boolean mask to the array. A Boolean mask is an array of the same shape as the original array that contains only True and False values to indicate whether the element should be included or not.

Types of Array Filtering in NumPy

Array filtering in NumPy can be categorized based on the type of condition you apply:

  • Simple Condition Filtering: Involves applying a single condition to the array.
  • Multiple Condition Filtering (AND/OR): You can apply multiple conditions to an array using logical operators like & (AND) or | (OR).
  • Custom Function Filtering: You can create a custom function that checks for a certain condition, and apply this function to the array.

Let's illustrate each type with an example:

Simple Condition Filtering:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Create a boolean mask where each value corresponds to whether the condition is met
mask = arr > 5

# Apply the mask to the array
filtered_arr = arr[mask]
print(filtered_arr)  # Outputs: array([6, 7, 8, 9])

Multiple Condition Filtering (AND/OR):

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Create a boolean mask where each value corresponds to whether the conditions are met
mask = (arr > 5) & (arr < 9)

# Apply the mask to the array
filtered_arr = arr[mask]
print(filtered_arr)  # Outputs: array([6, 7, 8])

Custom Function Filtering:

import numpy as np

# Define a custom function to check if a grade corresponds to an "A"
def is_a(grade):
    return grade >= 90

# Create an array of grades
grades = np.array([85, 90, 78, 92, 88, 76, 95, 89])

# Apply the custom function to create a Boolean mask
mask = np.vectorize(is_a)(grades)

# Apply the mask to the original array
a_grades = grades[mask]

print(a_grades)  # Outputs: [90 92 95]

In this example, np.vectorize(is_a)(grades) applies the is_a function to every element in the grades array. This results in a new Boolean array (mask) where each element indicates whether the corresponding grade in grades is 90 or above. The grades[mask] operation then filters the grades array based on this mask, resulting in a new array that only includes grades that are 90 or above.


ADVERTISEMENT

ADVERTISEMENT