Friday, October 24, 2025
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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS