0

I would like to find the N biggest elements and their arguments in a numpy array.

I use partitions that i found but i cant get the 5 biggest in element in an numpy array. Here is an example:

a =[ 0.409,  0.355, -0.403,  0.307, -0.826, -0.66,  -1.582, -0.112, -0.244, -0.954]    
z = bn.argpartition(-a, 5)[:5]         
z = apodosi.argsort()[-5:]         
z = heapq.nlargest(5, a)   

I get back the array a while I would like to get 0,1,3,5,7 as arguments and if possible their values. I am still new in programming!

hemanta
  • 1,405
  • 2
  • 13
  • 23
r4diohead
  • 1
  • 3
  • Possible duplicate of [A fast way to find the largest N elements in an numpy array](https://stackoverflow.com/questions/10337533/a-fast-way-to-find-the-largest-n-elements-in-an-numpy-array) – iz_ Feb 01 '19 at 02:44
  • *"...I would like to get 0,1,3,5,7"* I think you mean [0, 1, 3, 7, 8]. I assume `bn` is `numpy`. What is wrong with `z = bn.argpartition(-a, 5)[:5]` to get the indices of the five largest values? – Warren Weckesser Feb 01 '19 at 06:30

1 Answers1

0

The easiest way is probably this:

array = [5,3,1,2,4,7,9,8,6]
array.sort(reverse=True) # [9,8,7,...,2,1]
greatest = array[0:5] # [9,8,7,6,5]

I imagine numpy arrays would work the same way.

ardee
  • 7
  • 1
  • 5