The ma.count_masked()
method in NumPy is used to take count of the masked elements along the given axis of an array.
The ma.count_masked()
method takes the following syntax:
ma.count_masked(arr, axis=None)
The ma.count_masked()
function takes the following parameter values:
arr
: This is the input array with possibly masked elements. It is a required parameter value.
axis
: This is the axes along which the count is to be done. It is an optional parameter value.
The ma.count_masked()
function returns a count of the total number of masked elements along a given axis of an array.
import numpy.ma as ma# creating a masked arraya = ma.arange(8).reshape(4,2)# masking the elements in the third rowa[2, :] = ma.masked# taking counts of non-masked elements along rowsb = ma.count_masked(a, axis=1)# taking count of non-masked elements along columnsc = ma.count_masked(a, axis=0)print(a)print("Taking count along row: ", b)print("Taking count along columns: ", c);
Line 1: We import the numpy.ma
module.
Line 3: We create an input masked array, a
.
Line 6: We mask the elements present in the third row of the input array.
Line 9: We take count of the masked elements in axis 1
(rows) of the input array. We assign the result to a variable, b
.
Line 12: We take count of the masked elements in axis 0
(columns) of the input array. We assign the result to a variable, c
.
Lines 14–16: We print the input array, a
, and the arrays b
and c
to the console.