What is the ma.masked_greater() function in NumPy?

Overview

The masked_greater() function in NumPy is used to mask an array, where the value of the element of the array is greater than the specified value in the function.

Syntax

ma.masked_greater(x, value)
Syntax for the masked_greater() function

Parameters

This function takes the following parameter values:

  • x: This is the input array. This is a required parameter.
  • value This is the value to which elements of the array are masked if they are greater. This is a required parameter.

Return value

This function returns a masked array.

Example

# A code to illustrate the masked_greater() function
# 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 greater than 2
mask_array =ma.masked_greater(my_array, 2)
print(mask_array)

Explanation

  • Line 4–5: We import the necessary library and module.
  • Line 8: We create an input array, my_array.
  • Line 11: We mask the values greater than 2 in the input array using the masked_greater() function. The result is assigned to a variable, mask_array.
  • Line 13: We print the masked array, mask_array.

Free Resources