简体   繁体   English

如何找到n维numpy数组的第i个最大元素的索引?

[英]How to find the indices of the i-th largest element of an n-dimensional numpy array?

I know how to find the indices of the maximum element of an n-dimensional array. 我知道如何找到n维数组的最大元素的索引。

Let's have for example: 让我们举个例子:

a=np.asarray([[1,7,-4],[9,-11,-17]])

Then ( source ): 然后( 来源 ):

from numpy import unravel_index
unravel_index(a.argmax(), a.shape)

returning: 返回:

(1, 0)

and indeed a[1,0] is 9 , which is the highest element in the array a , so we are good. 实际上a[1,0]9 ,这是数组a的最高元素,所以我们很好。


I am also able to figure out how to find the indices of the ith largest element of a one-dimensional numpy array ( using ): 我还能够弄清楚如何找到一维numpy数组的第i个最大元素的索引( 使用 ):

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

i=3 # we want the third largest element, for example
a.argsort()[-i]

This returns 1 , which is good, since a[1]=3 which is indeed the third largest element of a . 这将返回1 ,这是很好的,因为a[1]=3这的确是第三大元件a


I would like to combine these two. 我想将两者结合起来。 So if I have 所以如果我有

a=np.asarray([[1,7,-4],[9,-11,-17]])

I would like to get an output telling me the indices of the ith largest element of the array a , for example if i=3 , the output should be [0,0] , since a[0,0]=1 is the ith (third) largest element of a . 我想得到一个输出,告诉我数组a的第i个最大元素的索引,例如,如果i=3 ,则输出应为[0,0] ,因为a[0,0]=1是第i个(第三)的最大元素a

How can I do this? 我怎样才能做到这一点?

Well to get the index of some largest or whichever, you can use where: 要获取某个最大值或最大值的索引,可以在以下位置使用:

Adding to above answer by webDev : 通过webDev添加到上述答案:

import numpy as np
i=2

a=np.asarray([[1,7,-4],[9,-11,-17]])

flat=a.flatten()
flat.sort()
tryvalue= flat[-i]

i, j = np.where(a == tryvalue)
print(i,j)

This will give you: 这将为您提供:

[0] [1]

I mean you can make changes on your own that how you want these indexes to be like(tuple or whatever). 我的意思是,您可以自行更改这些索引的样式(元组或其他)。

This is a simple way to do so. 这是一种简单的方法。

import numpy as np
i=3
a=np.asarray([[1,7,-4],[9,-11,-17]])
flat=a.flatten()
flat.sort()
print(flat)
print(flat[-i])
i, j = np.where(a == flat[-i])
print(i,j)

You can flatten and then sort it. 您可以展平然后对其进行排序。 It will give you the output that you want based on your ith largest ie i=3 . 它将根据您的第i个最大值(即i=3提供所需的输出。 If you enter i=5, flat[-i] will give you -11 . 如果输入i = 5,则flat[-i]会给您-11

You can also use heapq.nlargest on the flattened array and get the minimum of those largest i elements. 您还可以在展平的数组上使用heapq.nlargest ,并获得最大的i元素中的最小值。 That should be more efficient than sorting for most cases: 在大多数情况下,这应该比排序更有效:

import numpy as np
import heapq

a = np.asarray([[1, 7, -4], [9, -11, -17]])
i = 2

ith_largest = min(heapq.nlargest(i, a.flatten()))
x, y = np.where(a == ith_largest)
print(x, y)  # [0] [1]

Amit Amola 's answer is perfectly good. 阿米特·阿莫拉Amit Amola )的回答非常好。 In case someone needs another one, I found this solution: 万一有人需要另一个,我找到了这个解决方案:

a=np.asarray([[1,7,-4],[9,-11,-17]])
flat=a.flatten()
flat.sort()
i=5

for k, p in enumerate(a):
    for j, q in enumerate(p):
        if q == flat[-i]:
            indices=[k,j]

print(indices)

Giving [1, 1] , which is good. [1, 1] ,这很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM