在numpy数组中查找第n个最小元素 [英] Find nth smallest element in numpy array
问题描述
我只需要找到1D numpy.array
中最小的第n个元素.
I need to find just the the smallest nth element in a 1D numpy.array
.
例如:
a = np.array([90,10,30,40,80,70,20,50,60,0])
我想获得第五个最小的元素,所以我想要的输出是40
.
I want to get 5th smallest element, so my desired output is 40
.
我当前的解决方案是:
result = np.max(np.partition(a, 5)[:5])
但是,找到5个最小的元素,然后选择最大的5个元素,对我来说似乎有点笨拙.有更好的方法吗?我是否缺少一个可以实现目标的功能?
However, finding 5 smallest elements and then taking the largest one them seems little clumsy to me. Is there a better way to do it? Am I missing a single function that would achieve my goal?
有些问题的标题与此相似,但是我没有看到任何答案.
There are questions with similar titles to this one, but I did not see anything that answered my question.
我本来应该提到它,但是性能对我来说非常重要;因此,heapq
解决方案虽然不错,但对我来说不起作用.
I should've mentioned it originally, but performance is very important for me; therefore, heapq
solution though nice would not work for me.
import numpy as np
import heapq
def find_nth_smallest_old_way(a, n):
return np.max(np.partition(a, n)[:n])
# Solution suggested by Jaime and HYRY
def find_nth_smallest_proper_way(a, n):
return np.partition(a, n-1)[n-1]
def find_nth_smallest_heapq(a, n):
return heapq.nsmallest(n, a)[-1]
#
n_iterations = 10000
a = np.arange(1000)
np.random.shuffle(a)
t1 = timeit('find_nth_smallest_old_way(a, 100)', 'from __main__ import find_nth_smallest_old_way, a', number = n_iterations)
print 'time taken using partition old_way: {}'.format(t1)
t2 = timeit('find_nth_smallest_proper_way(a, 100)', 'from __main__ import find_nth_smallest_proper_way, a', number = n_iterations)
print 'time taken using partition proper way: {}'.format(t2)
t3 = timeit('find_nth_smallest_heapq(a, 100)', 'from __main__ import find_nth_smallest_heapq, a', number = n_iterations)
print 'time taken using heapq : {}'.format(t3)
结果:
time taken using partition old_way: 0.255564928055
time taken using partition proper way: 0.129678010941
time taken using heapq : 7.81094002724
推荐答案
除非我丢失了某些内容,否则您要做的是:
Unless I am missing something, what you want to do is:
>>> a = np.array([90,10,30,40,80,70,20,50,60,0])
>>> np.partition(a, 4)[4]
40
np.partition(a, k)
将a
的k
最小元素放置在a[k]
,a[:k]
中的值较小,而a[k+1:]
中的值较大.唯一需要注意的是,由于索引为0,因此第五个元素位于索引4.
np.partition(a, k)
will place the k
-th smallest element of a
at a[k]
, smaller values in a[:k]
and larger values in a[k+1:]
. The only thing to be aware of is that, because of the 0 indexing, the fifth element is at index 4.
这篇关于在numpy数组中查找第n个最小元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!