NumPy Array Filter
NumPy provides powerful capabilities to filter elements within arrays based on specified conditions. This chapter covers how to filter arrays using Boolean indexing and the numpy.where()
function.
Filtering Arrays with Boolean Indexing
You can filter elements in an array using Boolean indexing, which involves creating a Boolean array that specifies which elements to include.
Example: Boolean Indexing
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Create a Boolean array where elements are greater than 5
filter_arr = arr > 5
# Use the Boolean array to filter the original array
filtered_arr = arr[filter_arr]
print(filtered_arr)
This code filters the array to include only elements greater than 5.
Using numpy.where()
The numpy.where()
function can be used to filter elements based on a condition and can also be used to return indices where the condition is met.
Example: Using numpy.where()
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Get indices where elements are greater than 5
indices = np.where(arr > 5)
# Use the indices to filter the original array
filtered_arr = arr[indices]
print(filtered_arr)
This code filters the array to include only elements greater than 5 using the numpy.where()
function.
Combining Multiple Conditions
You can combine multiple conditions using logical operators to filter elements that meet all specified conditions.
Example: Combining Multiple Conditions
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Filter elements that are greater than 3 and less than 8
filtered_arr = arr[(arr > 3) & (arr < 8)]
print(filtered_arr)
This code filters the array to include only elements that are greater than 3 and less than 8.
Filtering Multi-Dimensional Arrays
Filtering can also be applied to multi-dimensional arrays by specifying the condition for each element in the array.
Example: Filtering Multi-Dimensional Arrays
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Filter elements greater than 4
filtered_arr = arr[arr > 4]
print(filtered_arr)
This code filters the multi-dimensional array to include only elements greater than 4.
Conclusion
NumPy provides efficient ways to filter elements within arrays using Boolean indexing and the numpy.where()
function. These tools are essential for data analysis and manipulation tasks.