Python 我需要一个 numpy 数组中的 N 个最小(索引)值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16817948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I have need the N minimum (index) values in a numpy array
提问by astrochris
Hi I have an array with X amount of values in it I would like to locate the indexs of the ten smallest values. In this link they calculated the maximum effectively, How to get indices of N maximum values in a numpy array?however I cant comment on links yet so I'm having to repost the question.
嗨,我有一个包含 X 个值的数组,我想找到十个最小值的索引。在此链接中,他们有效地计算了最大值,如何在 numpy 数组中获取 N 个最大值的索引?但是我还不能对链接发表评论,所以我不得不重新发布这个问题。
I'm not sure which indices i need to change to achieve the minimum and not the maximum values. This is their code
我不确定我需要更改哪些索引才能达到最小值而不是最大值。这是他们的代码
In [1]: import numpy as np
In [2]: arr = np.array([1, 3, 2, 4, 5])
In [3]: arr.argsort()[-3:][::-1]
Out[3]: array([4, 3, 1])
采纳答案by petrichor
If you call
如果你打电话
arr.argsort()[:3]
It will give you the indices of the 3 smallest elements.
它将为您提供 3 个最小元素的索引。
array([0, 2, 1], dtype=int64)
So, for n, you should call
所以,对于n,你应该打电话
arr.argsort()[:n]
回答by mgilson
I don't guarantee that this will be faster, but a better algorithmwould rely on heapq.
我不保证这会更快,但更好的算法将依赖于heapq.
import heapq
indices = heapq.nsmallest(10,np.nditer(arr),key=arr.__getitem__)
This should work in approximately O(N)operations whereas using argsortwould take O(NlogN)operations. However, the other is pushed into highly optimized C, so it might still perform better. To know for sure, you'd need to run some tests on your actual data.
这应该适用于近似O(N)操作,而 usingargsort会进行O(NlogN)操作。然而,另一个被推入高度优化的 C,所以它可能仍然表现得更好。要确定,您需要对实际数据运行一些测试。
回答by Mike Müller
Just don't reverse the sort results.
只是不要反转排序结果。
In [164]: a = numpy.random.random(20)
In [165]: a
Out[165]:
array([ 0.63261763, 0.01718228, 0.42679479, 0.04449562, 0.19160089,
0.29653725, 0.93946388, 0.39915215, 0.56751034, 0.33210873,
0.17521395, 0.49573607, 0.84587652, 0.73638224, 0.36303797,
0.2150837 , 0.51665416, 0.47111993, 0.79984964, 0.89231776])
Sorted:
排序:
In [166]: a.argsort()
Out[166]:
array([ 1, 3, 10, 4, 15, 5, 9, 14, 7, 2, 17, 11, 16, 8, 0, 13, 18,
12, 19, 6])
First ten:
前十名:
In [168]: a.argsort()[:10]
Out[168]: array([ 1, 3, 10, 4, 15, 5, 9, 14, 7, 2])
回答by Alex
Since this question was posted, numpy has updated to include a faster way of selecting the smallest elements from an array using argpartition. It was first included in Numpy 1.8.
自从发布此问题以来,numpy 已更新为包含使用argpartition. 它首先包含在 Numpy 1.8 中。
Using snarly's answeras inspiration, we can quickly find the k=3smallest elements:
以snarly 的回答为灵感,我们可以快速找到k=3最小的元素:
In [1]: import numpy as np
In [2]: arr = np.array([1, 3, 2, 4, 5])
In [3]: k = 3
In [4]: ind = np.argpartition(arr, k)[:k]
In [5]: ind
Out[5]: array([0, 2, 1])
In [6]: arr[ind]
Out[6]: array([1, 2, 3])
This will run in O(n) time because it does not need to do a full sort. If you need your answers sorted (Note:in this case the output array was in sorted order but that is not guaranteed) you can sort the output:
这将在 O(n) 时间内运行,因为它不需要进行完整排序。如果您需要对答案进行排序(注意:在这种情况下,输出数组按排序顺序排列,但不能保证),您可以对输出进行排序:
In [7]: sorted(arr[ind])
Out[7]: array([1, 2, 3])
This runs on O(n + k log k) because the sorting takes place on the smaller output list.
这在 O(n + k log k) 上运行,因为排序发生在较小的输出列表上。
回答by mohammadali68
This code save 20 index of maximum element of split_listin Twenty_Maximum:
此代码保存split_listin的最大元素的 20 索引Twenty_Maximum:
Twenty_Maximum = split_list.argsort()[-20:]
against this code save 20 index of minimum element of split_listin Twenty_Minimum:
针对此代码保存split_listin的最小元素的 20 索引Twenty_Minimum:
Twenty_Minimum = split_list.argsort()[:20]

