What is the ma.masked_less_equal() function in NumPy?
Overview
The masked_less_equal() function in numpy.ma (MaskedArray class) is used to mask an array, where the value of the elements of the array is less than or equal to the specified value in the function.
Note: The
main thema.masked_less_equal()function specifies that we’re going to mask a masked array. A masked array is different from a regular array in that it has one, some, or all of its elements hidden or masked with--. A masked array is a blend of a standardnumpy.ndarrayand a mask. When a mask is not masked, it indicates that the array under consideration has all valid values, or that it is an array of booleans that represent each value of the array under consideration, whether the value is valid or invalid.
Syntax
ma.masked_less_equal(x, value)
Parameters
x: This is the input array. This is a mandatory parameter.value: This is the value to which elements of the array are masked if they are less than or equal to it. This is a mandatory parameter.
Return value
This function returns a masked array.
Example
# importing the necessary librariesimport numpy as npimport numpy.ma as ma# creating an input arraymy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])# masking the values that are less than or equal to 5mask_array = ma.masked_less(my_array, 5)print(mask_array)
Explanation
- Line 2–3: We import the necessary library and module.
- Line 6: We create an input array,
my_array. - Line 9: We use the
masked_less_equal()function to mask the values greater than or equal to5in the input array. We assign the result to the variable,mask_array. - Line 10: We print the masked array,
mask_array.