Empty masked array with the properties of an existing array in Numpy

To empty masked array with the properties of an existing array, use the ma.masked_all_like() method 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

Create a new array using the numpy.array() method in Python Numpy −

arr = np.array([[77, 51, 92], [56, 31, 69], [73, 88, 51], [62, 45, 67]], dtype=np.float32)

Displaying our array −

print("Array...<br>",arr)

Get the datatype −

print("\nArray datatype...<br>",arr.dtype)

To empty masked array with the properties of an existing array, use the ma.masked_all_like() −

arr = ma.masked_all_like(arr)

Displaying our array −

print("\nNew Array...<br>",arr)

Get the datatype −

print("\nNew Array datatype...<br>",arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...<br>",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...<br>",arr.shape)

Get the number of elements of the Array −

print("\nElements in the Array...<br>",arr.size)

Example

# Python ma.MaskedArray - Empty masked array with the properties of an existing array

import numpy as np
import numpy.ma as ma

# Create a new array using the numpy.array() method in Python Numpy
arr = np.array([[77, 51, 92], [56, 31, 69], [73, 88, 51], [62, 45, 67]], dtype=np.float32)

# Displaying our array
print("Array...<br>",arr)

# Get the datatype
print("\nArray datatype...<br>",arr.dtype)

# To empty masked array with the properties of an existing array, use the ma.masked_all_like() method in Python Numpy
arr = ma.masked_all_like(arr)

# Displaying our array
print("\nNew Array...<br>",arr)

# Get the datatype
print("\nNew Array datatype...<br>",arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...<br>",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...<br>",arr.shape)

# Get the number of elements of the Array
print("\nElements in the Array...<br>",arr.size)

Output

Array...
[[77. 51. 92.]
[56. 31. 69.]
[73. 88. 51.]
[62. 45. 67.]]

Array datatype...
float32

New Array...
[[-- -- --]
[-- -- --]
[-- -- --]
[-- -- --]]

New Array datatype...
float32

Array Dimensions...
2
Our Array Shape...
(4, 3)
Elements in the Array...
12
Updated on: 2022-02-03T11:20:28+05:30

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements