numpy 获取值为 true 的索引 [英] numpy get index where value is true

查看:173
本文介绍了numpy 获取值为 true 的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预><代码>>>>ex=np.arange(30)>>>e=np.reshape(ex,[3,10])>>>电子数组([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])>>>e>15数组([[假,假,假,假,假,假,假,假,假,错误的],[假,假,假,假,假,假,真,真,真,真的],[ 对,对,对,对,对,对,对,对,对,真]],dtype=bool)

我需要在 e 中找到具有 true 或值大于 15 的行.我可以使用 for 循环进行迭代,但是,我想知道是否有办法numpy 可以更有效地做到这一点吗?

解决方案

获取至少一项大于 15 的行号:

<预><代码>>>>np.where(np.any(e>15,axis=1))(数组([1, 2], dtype=int64),)

>>> ex=np.arange(30)
>>> e=np.reshape(ex,[3,10])
>>> e
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])
>>> e>15
array([[False, False, False, False, False, False, False, False, False,
        False],
       [False, False, False, False, False, False,  True,  True,  True,
         True],
       [ True,  True,  True,  True,  True,  True,  True,  True,  True,
         True]], dtype=bool)

I need to find the rows that have true or rows in e whose value are more than 15. I could iterate using a for loop, however, I would like to know if there is a way numpy could do this more efficiently?

解决方案

To get the row numbers where at least one item is larger than 15:

>>> np.where(np.any(e>15, axis=1))
(array([1, 2], dtype=int64),)

这篇关于numpy 获取值为 true 的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆