查找满足条件的二维numpy数组的索引

7

我有一个大的二维numpy数组,想要找到满足条件的1D数组的索引,例如至少有一个值大于给定阈值x。

我已经可以用以下方法来实现,但是否有更短、更高效的方法呢?

import numpy

a = numpy.array([[1,2,3,4,5], [1,2,3,4,20], [1,2,2,4,5]])

indices = []
i = 0
x = 10
for item in a:
    if any(j > x for j in item):
        indices.append(i)
    i += 1

print(indices) # gives [1]

更简单的索引检索案例 [1](https://dev59.com/ZV4d5IYBdhLWcg3wG_WH),[2](https://dev59.com/lXRC5IYBdhLWcg3wCMg6),[3](https://dev59.com/NmMl5IYBdhLWcg3w1593) - Reveille
1个回答

8
你可以使用numpy内置的布尔运算:
import numpy as np
a = np.array([[1,2,3,4,5], [1,2,3,4,20], [1,2,2,4,5]])

indices = np.argwhere(np.any(a > 10, axis=1))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接