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 ma in the ma.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 standard numpy.ndarray and 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)
Syntax for the masked_less_equal() function

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 libraries
import numpy as np
import numpy.ma as ma
# creating an input array
my_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# masking the values that are less than or equal to 5
mask_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 to 5 in the input array. We assign the result to the variable, mask_array.
  • Line 10: We print the masked array, mask_array.