Thursday, February 5, 2026
HomeLanguagesnumpy.ma.MaskedArray.nonzero() function – Python

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

numpy.ma.MaskedArray.nonzero() function return the indices of unmasked elements that are not zero. This function returns a tuple of arrays, one for each dimension, containing the indices of the non-zero elements in that dimension.

Syntax : numpy.ma.MaskedArray.nonzero(self)

Return : [tuple] Indices of elements that are non-zero.

Code #1 :




# Python program explaining
# numpy.ma.MaskedArray.nonzero() function
    
# importing numpy as geek   
# and numpy.ma module as ma  
import numpy as geek  
import numpy.ma as ma
   
arr = ma.array(geek.eye(5))
  
gfg = arr.nonzero()
  
print (gfg)


Output :

(array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]))

 
Code #2 :




# Python program explaining
# numpy.ma.MaskedArray.nonzero() function
    
# importing numpy as geek   
# and numpy.ma module as ma  
import numpy as geek  
import numpy.ma as ma
   
arr = ma.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  
gfg = ma.nonzero(arr > 3)
  
print (gfg)


Output :

(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32479 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11980 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6988 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6936 POSTS0 COMMENTS
Umr Jansen
6919 POSTS0 COMMENTS