[英]Get the indices of N highest values in an ndarray
Considering an histogram of shape 100x100x100, I would like to find the 2 highest values a and b, and their indices (a1, a2, a3) and (b1, b2, b3), such as: 考虑到形状100x100x100的直方图,我想找到2个最高值a和b,以及它们的索引(a1,a2,a3)和(b1,b2,b3),例如:
hist[a1][a2][a3] = a
hist[b1][b2][b3] = b
We can easily get the highest value with hist.max(), but how can we get the X highest values in a ndarray? 我们可以使用hist.max()轻松获得最高值,但是如何在ndarray中获得X最高值?
I understand that one normally uses np.argmax to retrieve the value indices, but in that case: 我知道通常使用np.argmax来检索值索引,但在这种情况下:
hist.argmax().shape = () # single value
for i in range(3):
hist.argmax(i).shape = (100, 100)
How can I get a shape (3), a tuple with one value per dimension? 我怎样才能得到一个形状(3),一个每个维度有一个值的元组?
You can use numpy.argpartition
on flattened version of array first to get the indices of top k
items, and then you can convert those 1D indices as per the array's shape using numpy.unravel_index
: 您可以
numpy.argpartition
在平顶版本的数组上使用numpy.argpartition
来获取前k
项的索引,然后您可以使用numpy.unravel_index
按照数组的形状转换这些1D索引:
>>> arr = np.arange(100*100*100).reshape(100, 100, 100)
>>> np.random.shuffle(arr)
>>> indices = np.argpartition(arr.flatten(), -2)[-2:]
>>> np.vstack(np.unravel_index(indices, arr.shape)).T
array([[97, 99, 98],
[97, 99, 99]])
)
>>> arr[97][99][98]
999998
>>> arr[97][99][99]
999999
You could use where : 你可以在哪里使用:
a=np.random.random((100,100,100))
np.where(a==a.max())
(array([46]), array([62]), array([61]))
to get in a single array: 进入一个阵列:
np.hstack(np.where(a==a.max()))
array([46, 62, 61])
and, as the OP asked for a tuple: 并且,当OP要求一个元组时:
tuple(np.hstack(np.where(a==a.max())))
(46, 62, 61)
EDIT: 编辑:
To get the indices of the N
largest sets you could use the nlargest
function from the heapq
module: 要获取
N
最大集的索引,可以使用heapq
模块中的nlargest
函数:
N=3
np.where(a>=heapq.nlargest(3,a.flatten())[-1])
(array([46, 62, 61]), array([95, 85, 97]), array([70, 35, 2]))
I suppose you can do this: 我想你可以这样做:
(pseudocode) (伪代码)
#work on a copy
working_hist = copy(hist)
greatest = []
min_value = hist.argmin().shape
#while searching for the N greatest values, do N times
for i in range(N):
#get the current max value
max_value = hist.argmax().shape
#save it
greatest.append(max_value)
#and then replace it by the minimum value
hist(max_value.shape)= min_value
I haven't used numpy for years, so I'm not sure of the syntax. 多年来我没有使用numpy,所以我不确定语法。 The code is just here in order to give you a pseudo-code like answer.
代码就在这里,以便为您提供类似答案的伪代码。
If you retain also the position of the value you extract you can avoid to work on a copy of the item by using the extracted informations to restore the matrix at the end. 如果还保留了提取值的位置,则可以通过使用提取的信息在末尾恢复矩阵来避免处理项目的副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.