我在 numpy.ma 中有一个屏蔽数组,所有值都被屏蔽了:
import numpy.ma as ma
arr = ma.array([3,4,10], mask=[True, True, True])
我希望对这个数组的操作,例如 ma.sum
应该返回 masked
:
>>> ma.sum(arr) is ma.masked
>>> True
这确实是 True
。
但是当我在同一个数组上使用 ma.argmax()
时,结果不是 ma.masked
而是 0
>>> ma.argmax(arr) is ma.masked
>>> False
>>> ma.argmax(arr)
>>> 0
有什么想法吗?这是一个错误,还是预期的行为?理想情况下,这将返回 masked
。谁能想出一个好的解决方法,或者我是不是太傻了……谢谢!
最佳答案
>>> arr[ma.argmax(arr)]
masked
argmax
返回最大值的索引。您可以使用索引来获取值。 值 被屏蔽了。
因为所有值都被屏蔽了,所以它们被认为是相等的(等于 fill_value
),并且 argmax
返回记录的第一个索引(在 the docs of numpy.argmax
中)。
关于python - Numpy 屏蔽数组 argmax 在完全屏蔽数组上不返回 'masked'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16207352/