gpt4 book ai didi

python - numpy 获取值为真的索引

转载 作者:IT老高 更新时间:2023-10-28 20:29:47 25 4
gpt4 key购买 nike

>>> 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)

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

最佳答案

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

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

关于python - numpy 获取值为真的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16094563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com