I know I can do it like the following:
import numpy as np
N=10
a=np.arange(1,100,1)
np.argsort()[-N:]
However, it is very slow since it did a full sort.
I wonder whether numpy provide some methods the do it fast.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
bottleneckmodule has a fast partial sort method that works directly with Numpy arrays:bottleneck.partition().Note that
bottleneck.partition()returns the actual values sorted, if you want the indexes of the sorted values (whatnumpy.argsort()returns) you should usebottleneck.argpartition().I’ve benchmarked:
z = -bottleneck.partition(-a, 10)[:10]z = a.argsort()[-10:]z = heapq.nlargest(10, a)where
ais a random 1,000,000-element array.The timings were as follows:
bottleneck.partition(): 25.6 ms per loopnp.argsort(): 198 ms per loopheapq.nlargest(): 358 ms per loop