3

I have this [116, 116] array, and I would like to get the coordinates/indices of the 10 maximum values present in that array.

How can I achieve that?

Thanks!

Hugo Teixeira
  • 65
  • 1
  • 4

2 Answers2

3

Let's create a test array arr as:

array([[  1,   2, 141,   4,   5,   6],
       [  7, 143,   9,  10,  11,  12],
       [ 13,  14,  15, 145,  17,  18],
       [ 19,  20,  21,  22,  23,  24],
       [ 25,  26,  27,  28,  29,  30]])

To find cordinates of e.g. 3 maximum values, run:

ind = np.divmod(np.flip(np.argsort(arr, axis=None)[-3:]), arr.shape[1])

The result is a 2-tuple with row and column coordinates:

(array([2, 1, 0], dtype=int64), array([3, 1, 2], dtype=int64))

To test it, you can print indicated elements:

arr[ind]

getting:

array([145, 143, 141])

Now replace -3 with -10 and you will get coordinates of 10 max elements.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
1

See this answer using np.argpartition.