Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 05 May, 2020

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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :