Convert the input to a masked array conserving subclasses in Numpy


To convert the input to a masked array conserving subclasses, use the numpy.ma.asanyarray() method in Python Numpy. The function returns the MaskedArray interpretation of the input.

If the input is a subclass of MaskedArray, its class is conserved. No copy is performed if the input is already an ndarray. The first parameter is the input data, in any form that can be converted to an array. The order parameter suggests whether to use row-major ('C') or column-major ('FORTRAN') memory representation. Default is 'C'.

Steps

At first, import the required library −

import numpy as np
import numpy.ma as ma

Create an array with int elements using the numpy.array() method −

arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]])
print("Array...
", arr) print("
Array type...
", arr.dtype)

Get the dimensions of the Array −

print("
Array Dimensions...
",arr.ndim)

Create a masked array and mask some of them as invalid −

maskArr = ma.masked_array(arr, mask =[[1, 1, 0], [ 0, 0, 0], [0, 1, 0], [0, 1, 0]])
print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)

Get the dimensions of the Array −

print("
Our Masked Array Dimensions...
",arr.ndim)

Get the shape of the Array −

print("
Our Masked Array Shape...
",arr.shape)

Get the number of elements of the Array −

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

To convert the input to a masked array conserving subclasses, use the numpy.ma.asanyarray() method −

print("
Masked Array...
",np.ma.asanyarray(arr))

Check the type −

print("
Type...
",type(np.ma.asanyarray(arr)))

Example

import numpy as np
import numpy.ma as ma

# Create an array with int elements using the numpy.array() method
arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]])
print("Array...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[[1, 1, 0], [ 0, 0, 0], [0, 1, 0], [0, 1, 0]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the dimensions of the Array print("
Our Masked Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Masked Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Masked Array...
",arr.size) # To convert the input to a masked array conserving subclasses, use the numpy.ma.asanyarray() method in Python Numpy print("
Masked Array...
",np.ma.asanyarray(arr)) # Check the type print("
Type...
",type(np.ma.asanyarray(arr)))

Output

Array...
[[65 68 81]
[93 33 39]
[73 88 51]
[62 45 67]]

Array type...
int64

Array Dimensions...
2
Our Masked Array
[[-- -- 81]
[93 33 39]
[73 -- 51]
[62 -- 67]]

Our Masked Array type...
int64

Our Masked Array Dimensions...
2

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

Elements in the Masked Array...
12

Masked Array...
[[65 68 81]
[93 33 39]
[73 88 51]
[62 45 67]]

Type...
<class 'numpy.ma.core.MaskedArray'>

Updated on: 04-Feb-2022

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements