How to Filter Arrays in NumPy: Simple Guide with Examples
To filter a NumPy array, use boolean indexing by creating a condition inside
array[condition]. This returns a new array with elements that meet the condition, like arr[arr > 5] to get values greater than 5.Syntax
The basic syntax to filter a NumPy array is filtered_array = array[condition]. Here, condition is a boolean array of the same shape as array, where True means keep the element and False means discard it.
For example, array > 5 creates a boolean array marking elements greater than 5.
python
filtered_array = array[condition]
Example
This example shows how to filter elements greater than 5 from a NumPy array.
python
import numpy as np arr = np.array([1, 3, 7, 9, 2, 5, 8]) filtered = arr[arr > 5] print(filtered)
Output
[7 9 8]
Common Pitfalls
One common mistake is trying to filter using a condition without using boolean indexing, like arr > 5 alone, which only returns a boolean array, not the filtered values.
Another error is mixing data types or shapes, which causes errors when applying conditions.
python
import numpy as np arr = np.array([1, 2, 3, 4]) # Wrong: This returns a boolean array, not filtered values print(arr > 2) # Right: Use boolean indexing to get filtered values print(arr[arr > 2])
Output
[False False True True]
[3 4]
Quick Reference
- Boolean indexing: Use
array[condition]to filter. - Conditions: Use comparison operators like
>,<,==,!=. - Combine conditions: Use
&(and),|(or) with parentheses, e.g.arr[(arr > 2) & (arr < 8)].
Key Takeaways
Use boolean indexing with conditions to filter NumPy arrays.
Conditions must produce boolean arrays matching the original array shape.
Combine multiple conditions with & and | inside parentheses.
Filtering returns a new array with only elements meeting the condition.
Avoid using conditions alone without indexing, as they return booleans, not filtered data.