The np.ma.masked_where function in NumPy is used to create a masked array. It masks (hides) elements of an array based on a specified condition. The elements that satisfy the condition are masked, meaning they will not be included in any calculations or visualizations.
Syntax
np.ma.masked_where(condition, a)
- condition: A boolean array or condition that determines which elements to mask.
- a: The input array from which elements will be masked.
Example
import numpy as np
# Create an array
data = np.array([1, 2, 3, 4, 5])
# Mask elements greater than 3
masked_data = np.ma.masked_where(data > 3, data)
print(masked_data)
Output
[1 2 3 -- --]
In this example, the elements greater than 3 are masked (represented by --), while the others remain visible.
