numpy.ma.count() method in Python counts the non-masked elements of an array along a given axis.
ma.count() method takes the syntax below:
ma.count(self, axis=None, keepdims=<no value>)
ma.count() takes the following parameter values:
axis: This is the axis on which the count is done. It is an optional parameter.
keepdims: This takes a Boolean value indicating whether or not the axes reduced are left in the result as dimensions with size one. This is an optional parameter.
ma.count()method returns an array having the same shape as the input array, with the specified axis removed.
import numpy.ma as ma # creating a masked array a = ma.arange(8).reshape(4,2) # masking the elements in the third row a[2, :] = ma.masked # taking counts of non-masked elements along rows b = a.count(axis=1) # taking count of non-masked elements along columns c = a.count(axis=0) print(a) print("Taking count along row: ", b) print("Taking count along columns: ", c)
axis 1(rows) of the input array. The result is assigned to a variable,
axis 0(columns) of the input array. The result is assigned to a variable,
a, and the arrays
View all Courses