What is Numpy Filtering ?
NumPy filtering uses boolean arrays (True/False) to decide which elements to keep.
The boolean array must be the same size as the original array.
Filtering using Boolean Indexing :
Python
import numpy as np arr = np.array([10, 20, 30, 40, 50]) filtered = arr[arr > 25] print(filtered) #output [30 40 50] Filtering with Multiple Conditions :
Use logical operators:
-
&→ AND -
|→ OR -
~→ NOT
Python
import numpy as np arr = np.array([10, 20, 30, 40, 50]) filtered = arr[(arr > 20) & (arr < 50)] print(filtered) #output [30 40] Filtering 2D Arrays :
Python
import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60]]) filtered = arr[arr > 25] print(filtered) #output [30 40 50 60] Filtering using np.where( ) :
Returns indices instead of values
Python
import numpy as np arr = np.array([10, 20, 30, 40, 50]) filtered = np.where(arr > 25) print(filtered) #output [2 3 4]