Return the common filling value of two masked arrays in Numpy

NumpyServer Side ProgrammingProgramming

To return the common filling value of two masked arrays, use the ma.common_fill_value() method in Python Numpy. If maskArray1.fill_value == maskArray2.fill_value, return the fill value, otherwise return None.

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 an array with int elements using the numpy.array() method −

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

Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Create a masked array 1 and mask some of them as invalid. The fill value is set using the "fill_value" parameter −

maskArr1 = ma.masked_array(arr, mask =[[1, 1, 0], [ 0, 0, 0], [0, 1, 0], [0, 1, 0]], fill_value = 9999)
print("\nOur Masked Array 1\n", maskArr1)

Create a masked array 2 and mask some of them as invalid. The fill value is set using the "fill_value" parameter −

maskArr2 = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0], [0, 1, 0], [0, 1, 0]], fill_value = 9999)
print("\nOur Masked Array 2\n", maskArr2)

To return the common filling value of two masked arrays, use the ma.common_fill_value() method in Python Numpy. If maskArray1.fill_value == maskArray2.fill_value, return the fill value, otherwise return None −

print("\nResult ( common filling value)\n", np.ma.common_fill_value(maskArr1, maskArr2))

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, 76], [73, 88, 51], [62, 45, 67]])
print("Array...\n", arr)

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

# Create a masked array 1 and mask some of them as invalid
# The fill value is set using the "fill_value" parameter
maskArr1 = ma.masked_array(arr, mask =[[1, 1, 0], [ 0, 0, 0], [0, 1, 0], [0, 1, 0]], fill_value = 9999)
print("\nOur Masked Array 1\n", maskArr1)

# Create a masked array 2 and mask some of them as invalid
# The fill value is set using the "fill_value" parameter
maskArr2 = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0], [0, 1, 0], [0, 1, 0]], fill_value = 9999)
print("\nOur Masked Array 2\n", maskArr2)

# To return the common filling value of two masked arrays, use the ma.common_fill_value() method in Python Numpy
# If maskArray1.fill_value == maskArray2.fill_value, return the fill value, otherwise return None
print("\nResult ( common filling value)\n", np.ma.common_fill_value(maskArr1, maskArr2))

Output

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

Array Dimensions...
2

Our Masked Array 1
[[-- -- 81]
[93 33 76]
[73 -- 51]
[62 -- 67]]

Our Masked Array 2
[[65 68 --]
[93 -- 76]
[73 -- 51]
[62 -- 67]]

Result ( common filling value)
9999
raja
Updated on 04-Feb-2022 12:02:02

Advertisements