python - 在二维数组/矩阵中查找 k 个最高值的索引

标签 python numpy

我有一个包含值的二维矩阵,我想找到前 5 个值的索引。 例如对于

matrix([[0.17542851, 0.13199346, 0.01579704, 0.01429822, 0.01302919],
        [0.13279703, 0.12444886, 0.04742024, 0.03114371, 0.02623729],
        [0.13502306, 0.07815065, 0.07291175, 0.03690815, 0.02163695],
        [0.19032505, 0.15853737, 0.05889324, 0.02791679, 0.02699252],
        [0.1695696 , 0.14538635, 0.07127667, 0.04997876, 0.02580234]])

我想得到 (0,3), (0,1), (0,4), (3,1), (4,1)

我搜索并尝试了很多解决方法,包括 np.argmax()、np.argsort()、np.argpartition(),但没有任何好的结果。 例如:

>>np.dstack(np.unravel_index(np.argsort(a.ravel(),axis=None), a.shape))

array([[[0, 4],
        [0, 3],
        [0, 2],
        [2, 4],
        [4, 4],
        [1, 4],
        [3, 4],
        [3, 3],
        [1, 3],
        [2, 3],
        [1, 2],
        [4, 3],
        [3, 2],
        [4, 2],
        [2, 2],
        [2, 1],
        [1, 1],
        [0, 1],
        [1, 0],
        [2, 0],
        [4, 1],
        [3, 1],
        [4, 0],
        [0, 0],
        [3, 0]]], dtype=int64)

这个结果毫无意义。 请注意,我想要原始索引,我不关心顺序(只想要任何顺序的前 5 个,不过升序会更好)

最佳答案

np.argpartition应该是一个很好的工具(高效的工具)来获得那些顶级 k 索引而不需要维持秩序。因此,对于数组数据 a,它将是 -

In [43]: np.c_[np.unravel_index(np.argpartition(a.ravel(),-5)[-5:],a.shape)]
Out[43]: 
array([[4, 1],
       [3, 1],
       [4, 0],
       [0, 0],
       [3, 0]])

为了解释,让我们将其分解为单个流程步骤 -

# Get partitioned indices such that the last 5 indices refer to the top 5
# values taken globally from the input array. Refer to docs for more info
# Note that it's global because we will flatten it. 
In [9]: np.argpartition(a.ravel(),-5)
Out[9]: 
array([14, 24,  2,  3,  4, 23, 22,  7,  8,  9, 19, 18, 17, 13, 12, 11,  6,
        1,  5, 10, 21, 16, 20,  0, 15])

# Get last 5 indices, which are the top 5 valued indices
In [10]: np.argpartition(a.ravel(),-5)[-5:]
Out[10]: array([21, 16, 20,  0, 15])

# Convert the global indices back to row-col format
In [11]: np.unravel_index(np.argpartition(a.ravel(),-5)[-5:],a.shape)
Out[11]: (array([4, 3, 4, 0, 3]), array([1, 1, 0, 0, 0]))

# Stack into two-columnar array
In [12]: np.c_[np.unravel_index(np.argpartition(a.ravel(),-5)[-5:],a.shape)]
Out[12]: 
array([[4, 1],
       [3, 1],
       [4, 0],
       [0, 0],
       [3, 0]])

对于 a 中的矩阵数据,它将是 -

In [48]: np.dstack(np.unravel_index(np.argpartition(a.ravel(),-5)[:,-5:],a.shape))
Out[48]: 
array([[[4, 1],
        [3, 1],
        [4, 0],
        [0, 0],
        [3, 0]]])

因此,与数组相比,唯一的区别是 np.dstack 的使用,因为对于矩阵数据,数据始终保持为二维。

请注意,这些是最后 5 行的结果。

关于python - 在二维数组/矩阵中查找 k 个最高值的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57103908/

相关文章:

python - lgb.train ValueError : The truth value of an array with more than one element is ambiguous. 使用 a.any() 或 a.all()

python - scipy 中正常值的数值积分

python tkinter : displays only a portion of an image

python - 是什么导致 Python 套接字错误?

python - 如何使用任意长度的键更新 python 字典?

python - 如何将此 python 循环转换为矢量编码?

Python:从 DataFrame 中的两列创建结构化 numpy 结构化数组

python - numpy 数组列表格式

python - 在 Python/Pandas 中确定一天是否是工作日

python - 绘制/转换来自 sympy : Taylor series with matplotlib 的表达式