Quantcast
Channel: A fast way to find the largest N elements in an numpy array - Stack Overflow
Browsing index pages (8 articles)
↧

A fast way to find the largest N elements in an numpy array

I know I can do it like the following:import numpy as npN=10a=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...

View Article


Answer by Akavall for A fast way to find the largest N elements in an numpy...

Perhaps heapq.nlargestimport numpy as npimport heapqx = np.array([1,-5,4,6,-3,3])z = heapq.nlargest(3,x)Result:>>> z[6, 4, 3]If you want to find the indices of the n largest elements using...

View Article


Answer by Mike Graham for A fast way to find the largest N elements in an...

If storing the array as a list of numbers isn't problematic, you can use import heapqheapq.nlargest(N, a)to get the N largest members.

View Article

Answer by NPE for A fast way to find the largest N elements in an numpy array

The bottleneck module 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...

View Article

Answer by kwgoodman for A fast way to find the largest N elements in an numpy...

Each negative sign in the proposed bottleneck solution-bottleneck.partsort(-a, 10)[:10]makes a copy of the data. We can remove the copies by doingbottleneck.partsort(a, a.size-10)[-10:]Also the...

View Article


Answer by Akavall for A fast way to find the largest N elements in an numpy...

numpy 1.8 implements partition and argpartition that perform partial sort ( in O(n) time as opposed to full sort that is O(n) * log(n)).import numpy as nptest = np.array([9,1,3,4,8,7,2,5,6,0])temp =...

View Article

Answer by hennyk for A fast way to find the largest N elements in an numpy array

You can also use numpy's percentile function. In my case it was slightly faster then bottleneck.partsort():import timeitimport bottleneck as bnN,M,K = 10,1000000,100start = timeit.default_timer()for k...

View Article

Answer by Tacio Medeiros for A fast way to find the largest N elements in an...

I had this problem and, since this question is 5 years old, I had to redo all benchmarks and change the syntax of bottleneck (there is no partsort anymore, it's partition now).I used the same arguments...

View Article

Browsing index pages (8 articles)


Latest Images