# 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...",arr)

Get the datatype −

print("Array datatype...",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("New Array...",arr)


Get the datatype −

print("New Array datatype...",arr.dtype)

Get the dimensions of the Array −

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


Get the shape of the Array −

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

Get the number of elements of the Array −

print("Elements in the Array...",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...",arr)

# Get the datatype
print("Array datatype...",arr.dtype)

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

# Displaying our array
print("New Array...",arr)

# Get the datatype
print("New Array datatype...",arr.dtype)

# Get the dimensions of the Array
print("Array Dimensions...",arr.ndim)

# Get the shape of the Array
print("Our Array Shape...",arr.shape)

# Get the number of elements of the Array
print("Elements in the Array...",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: 03-Feb-2022

149 Views