Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
numpy
communitycreator

How to return the mask of a masked array in NumPy

Onyejiaku Theophilus Chidalu

Overview

In NumPy, the ma.getmaskarray() method is used to obtain the mask of a masked array by returning the Boolean values indicating mask (True) or no mask (False).

Syntax

The ma.getmaskarray() method takes the syntax below:

ma.getmaskarray(arr) 
Syntax for the ma.getmaskarray() method

Parameter value

The ma.getmaskarray() method takes a single parameter value, arr, which represents the input array (which is possibly masked) for which the mask is to be obtained.

Return value

The ma.getmaskarray() method returns an array of Boolean values.

Example

import numpy.ma as ma
# creating a masked array
a = ma.arange(8).reshape(4,2)

# masking the element in the second row but the second column
a[1, 1] = ma.masked

# masking the elements in the third row
a[2, :] = ma.masked

# implementing the ma.getmaskarray() method
b = ma.getmaskarray(a)

print(a)
print(b)
Implementing the ma.getmaskarray() method

Explanation

  • Line 1: We import the numpy.ma module.
  • Line 3: We create an input array, a.
  • Line 6: We masked the element in the second row on the second column of the input array.
  • Line 9: We masked the elements of the third row of the input array.
  • Line 12: We obtain the masked and not masked values of the input array by using the ma.getmaskarray() function. The result is assigned to a variable, b.
  • Lines 14–15: We print the input array, a, and the obtained array, b.

RELATED TAGS

function
numpy
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring