What is the numpy.ma.getmask() function in Python?

Overview

Python’s ma.getmask() function returns the mask of a masked array (i.e, the nomask).

Syntax

ma.getmask(a)

Parameter value

The ma.getmask() function takes the a value as a parameter. This represents an input MaskedArray for which the mask is required.

Return value

The ma.getmask() function returns an array of Boolean values indicating which input array elements are nomask or mask. It returns True if the element is mask and False if the element is nomask.

Example

import numpy as np
import numpy.ma as ma
# creating a MaskedArray with the element 3 as the mask
myarray = ma.masked_equal([[1,2],[3,4]], 3)
# implementing the ma.getmask() function
newarray = ma.getmask(myarray)
print(newarray)

Explanation

  • Line 1-2: We import the required module and library.
  • Line 5: We create a MaskedArray، myarray، which has its element 3 as the mask.
  • Line 8: We implement the ma.getmask() function on the input MaskedArray. The result is assigned to a variable newarray.
  • Line 10: We print the variable، newarray.

Free Resources