获取N维numpy数组中值的首次出现 [英] Getting the first occurrence of a value in an N-dimensional numpy array
本文介绍了获取N维numpy数组中值的首次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看过结果
where(mask) = (array([0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2]),
array([0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1]),
array([0, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2]))
,我想将其简化为 True
array([[0, 1],
[1, 1],
[2, 1]])
,但无法从 numpy.where
的输出中找出解决方法.有人可以帮我吗?
but can't work out how to go about this from the output of numpy.where
. Can anyone help me out?
推荐答案
实际上,它是如此简单:
Actually, it's as simple as this:
np.argmax(mask, 2)
示例:
In [15]: %paste
mask = array([[[ True, True, True],
[False, True, True]],
[[False, True, True],
[False, True, True]],
[[False, False, True],
[False, True, True]]])
## -- End pasted text --
In [16]: np.argmax(mask, 2)
Out[16]:
array([[0, 1],
[1, 1],
[2, 1]], dtype=int64)
这篇关于获取N维numpy数组中值的首次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文