# Return the mask of a masked array when mask is equal to nomask

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

To return the mask of a masked array, use the ma.getmaskarray() method in Python Numpy. Returns the mask of arr as an ndarray if arr is a MaskedArray and the mask is not nomask, else return a full boolean array of False of the same shape as arr.

A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.

## Steps

At first, import the required library −

import numpy as np
import numpy.ma as ma

Creating a 4x4 array with int elements using the numpy.arange() method −

arr = np.arange(16).reshape((4,4))
print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)


Get the shape of the Array −

print("\nOur Masked Array Shape...\n",arr.shape)

Get the number of elements of the Array −

print("\nElements in the Masked Array...\n",arr.size)


arr = ma.array(arr)

To count the number of masked elements along specific axis, use the ma.MaskedArray.count_masked() method −

print("\nThe number of masked elements...\n",ma.count_masked(arr))


To return the mask of a masked array, or full boolean array of False, use the ma.getmaskarray() method in Python Numpy −

print("\nResult (mask of a masked array)...\n",ma.getmaskarray(arr))

## Example

import numpy as np
import numpy.ma as ma

# Creating a 4x4 array with int elements using the numpy.arange() method
arr = np.arange(16).reshape((4,4))
print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)
print("\nOur Array type...\n", arr.dtype)

# Get the shape of the Array
print("\nOur Masked Array Shape...\n",arr.shape)

# Get the number of elements of the Array
print("\nElements in the Masked Array...\n",arr.size)

# Create a masked array
arr = ma.array(arr)

# To count the number of masked elements along specific axis, use the ma.MaskedArray.count_masked()

# To return the mask of a masked array, or full boolean array of False, use the ma.getmaskarray() method in Python Numpy
print("\nResult (mask of a masked array)...\n",ma.getmaskarray(arr))

## Output

Array...
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]

Array type...
int64

Array Dimensions...
2

Our Array type...
int64

Our Masked Array Shape...
(4, 4)

Elements in the Masked Array...
16

The number of masked elements...
0

[False False False False]]