Python numpy.ma.MaskedArray.nonzero()函数
numpy.ma.MaskedArray.nonzero()函数返回不为零的非屏蔽元素的索引。这个函数返回一个数组的元组,每个维度一个,包含该维度的非零元素的索引。
语法: numpy.ma.MaskedArray.nonzero(self)
返回 : [元组] 非零的元素的索引。
代码#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]))