我想从 numpy 二维数组中找到大于 2 的元素的索引。
像这样
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
# find indices of element that bigger than 2
# result = [[0,2],[[1,0],[1,1],[1,2]]
最佳答案
您可以使用np.where()
,它将为您提供元组模式(单独轴)中的预期索引:
In [6]: np.where(a>2)
Out[6]: (array([0, 1, 1, 1]), array([2, 0, 1, 2]))
或者直接使用np.argwhere()
:
In [5]: np.argwhere(a>2)
Out[5]:
array([[0, 2],
[1, 0],
[1, 1],
[1, 2]])
关于python - 如何从 numpy 二维数组中找到大于特殊数字的元素索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41136864/