What is the numpy.ma.masked_all_like() function in Python?

Overview

The ma.masked_all_like() function in Python empties a masked array with the properties of an existing array.

Syntax

ma.masked_all_like(arr)

Parameter

The ma.masked_all_like() function takes the parameter value arr, which represents an input array with the same shape and data type as the required MaskedArray.

Return type

The ma.masked_all_like() function returns a masked array where all the data are masked.

Example

import numpy as np
import numpy.ma as ma
# creating an array
myarray = np.array([[1,2,3], [4,5,6]])
# implementing the ma.masked_all_like() function
newarray = ma.masked_all_like(myarray)
# printing the old array
print(myarray)
# printing the masked array
print(newarray)

Explanation

  • Line 1 and 2: We import the required modules.
  • Line 5: We create an input array, myarray, using the array() function.
  • Line 8: We implement the ma.masked_all_like() function on the input array. The result is assigned to a variable, newarray.
  • Line 11: We print the input array, myarray.
  • Line 14: We print the masked array newarray.

Free Resources