numpy.ma.MaskedArray.nonzero()函数– Python
numpy.ma.MaskedArray.nonzero()
函数返回不为零的未屏蔽元素的索引。此函数返回一个数组元组,每个维度一个,包含该维度中非零元素的索引。
Syntax : numpy.ma.MaskedArray.nonzero(self)
Return : [tuple] Indices of elements that are non-zero.
代码#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)
输出 :
(array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]))
代码#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)
输出 :
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))