Saturday, July 25, 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
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6972 POSTS0 COMMENTS