Determine whether input has masked values

To determine whether input has masked values, use the ma.is_masked() method in Python Numpy. Accepts any object as input, but always returns False unless the input is a MaskedArray containing masked values. Returns True if the array is a MaskedArray with masked values, False otherwise.

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...
", arr) print("\nArray type...
", arr.dtype)

Get the dimensions of the Array −

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

Get the shape of the Array −

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

Get the number of elements of the Array −

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

Create a masked array −

arr = ma.array(arr)
arr[0, 1] = ma.masked
arr[1, 1] = ma.masked
arr[2, 1] = ma.masked
arr[2, 2] = ma.masked
arr[3, 0] = ma.masked
arr[3, 2] = ma.masked
arr[3, 3] = ma.masked

Count the number of masked elements along specific axis −

print("\nThe number of masked elements...
",ma.count_masked(arr, axis = 1))

Return the mask of a masked array −

print("\nThe mask of a masked array)...
",ma.getmask(arr))

Return the data of a masked array as an ndarray −

print("\nData of a masked array as an ndarray...
",ma.getdata(arr))

To determine whether input has masked values, use the ma.is_masked() method in Python Numpy −

print("\nWhether input has masked values?
",ma.is_masked(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...
", arr) print("\nArray type...
", arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...
",arr.ndim) print("\nOur Array type...
", arr.dtype) # Get the shape of the Array print("\nOur Masked Array Shape...
",arr.shape) # Get the number of elements of the Array print("\nElements in the Masked Array...
",arr.size) # Create a masked array arr = ma.array(arr) arr[0, 1] = ma.masked arr[1, 1] = ma.masked arr[2, 1] = ma.masked arr[2, 2] = ma.masked arr[3, 0] = ma.masked arr[3, 2] = ma.masked arr[3, 3] = ma.masked # Count the number of masked elements along specific axis print("\nThe number of masked elements...
",ma.count_masked(arr, axis = 1)) # Return the mask of a masked array print("\nThe mask of a masked array)...
",ma.getmask(arr)) # Return the data of a masked array as an ndarray print("\nData of a masked array as an ndarray...
",ma.getdata(arr)) # To determine whether input has masked values, use the ma.is_masked() method in Python Numpy print("\nWhether input has masked values?
",ma.is_masked(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...
[1 1 2 3]

The mask of a masked array)...
[[False True False False]
[False True False False]
[False True True False]
[ True False True True]]

Data of a masked array as an ndarray...
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]

Whether input has masked values?
True
Updated on: 2022-02-18T11:33:02+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements