Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn Array Filter

In NumPy, filtering refers to the process of selecting elements from an array that satisfy certain conditions. NumPy provides several methods for filtering arrays:


Boolean Indexing: :Boolean indexing involves using a boolean mask—an array of True and False values—to select elements from another array. This is one of the most common and flexible ways to filter arrays in NumPy.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# Create a boolean mask based on a condition
mask = arr > 2
# Use the mask to filter the array
filtered_arr = arr[mask]
print(filtered_arr)

Output

[3 4 5]


Using Numpy Function: NumPy provides functions like numpy.where() and numpy.nonzero() to find the indices of elements that meet certain conditions.




Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java