Monday, June 15, 2026
HomeLanguagesnumpy.ma.MaskedArray.count() function – Python

numpy.ma.MaskedArray.count() function – Python

numpy.ma.MaskedArray.count() function count the non-masked elements of the array along the given axis.

Syntax : numpy.ma.MaskedArray.count(self, axis=None, keepdims = no value)

Parameters :
axis : [None or int or tuple of ints, optional] Axis along which the count is performed. The default axis is None, performs the count over all the dimensions of the input array. axis may be negative, in which case it counts from the last to the first axis.
keepdims : [bool, optional] If this is set to True, the axis which is reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the array.

Return : [ndarray or scalar] An array with the same shape as the input array, with the specified axis removed. If the array is a 0-d array, or if axis is None, a scalar is returned.

Code #1 :




# Python program explaining
# numpy.ma.MaskedArray.count() function
    
# importing numpy as geek   
# and numpy.ma module as ma  
import numpy as geek  
import numpy.ma as ma
   
arr = ma.arange(6).reshape((2, 3))
arr[1, :] = ma.masked
  
gfg = arr.count(axis = 0)
  
print (gfg)


Output :

[1 1 1]

 
Code #2 :




# Python program explaining
# numpy.ma.MaskedArray.count() function
    
# importing numpy as geek   
# and numpy.ma module as ma  
import numpy as geek  
import numpy.ma as ma
   
arr = ma.arange(6).reshape((2, 3))
arr[1, :] = ma.masked
  
gfg = arr.count()
  
print (gfg)


Output :

3
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS