The ma.nonzero()
method in NumPy returns the indices, or index positions, of non-zero unmasked elements of an array.
The ma.nonzero()
method takes the following syntax:
ma.nonzero(self)
The ma.nonzero()
method takes a single parameter: self
. This represents the input array, which may be masked.
The ma.nonzero()
method returns the index position of unmasked elements that are not zero.
import numpy as npimport numpy.ma as ma# Creating an input arraya = ma.array(np.eye(4))# Implementing the ma.nonzero() methodb = ma.nonzero(a)print(a)print(b)
Line 1: We import the numpy
module.
Line 2: We import the numpy.ma
module.
Line 4: We create an input array, a
.
Line 7: We call the ma.nonzero()
method and pass the a
input array as the argument. We assign the result to the variable b
.
Line 9: We print the a
input array.
Line 10: We print b
.