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)

### 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
a = ma.arange(8).reshape(4,2)

# masking the element in the second row but the second column

# masking the elements in the third row

print(a)
print(b)

### 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

Learn in-demand tech skills in half the time 