Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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]))

Last Updated : 05 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads