# Return the underlying data as a view of the masked array in Numpy

NumpyServer Side ProgrammingProgramming

To return the underlying data, as a view of the masked array, use the ma.MaskedArray.data in Python Numpy.

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)

arr[3, 3] = ma.masked

Count the number of masked elements along specific axis −

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


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

Return the data of a masked array as an ndarray −

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


Determine whether input is an instance of masked array −

print("\nWhether input is an instance of masked array?\n",ma.isMaskedArray(arr))

To return the underlying data, as a view of the masked array, use the ma.MaskedArray.data −

print("\nResult...\n",arr.data)


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

# Get the number of elements of the Array

arr = ma.array(arr)

# Count the number of masked elements along specific axis

# Return the data of a masked array as an ndarray
print("\nData of a masked array as an ndarray...\n",ma.getdata(arr))

# Determine whether input is an instance of masked array

# To return the underlying data, as a view of the masked array, use the ma.MaskedArray.data in Python Numpy
print("\nResult...\n",arr.data)

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

(4, 4)

16

[1 1 2 3]

[[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 is an instance of masked array?
True

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