0

so I was using this answer on 'How do I get indices of N maximum values in a NumPy array?' question. I used it in my ML model in which it outputs Logsoftmax layer values and I was thinking to get top 4 classes in each. In most of the cases, it sorted and gave values correctly but in a very few cases, I see partially unsorted results like this

arr = np.array([-3.0302, -2.7103, -7.4844, -3.4761, -5.3009, -5.2121, -3.7549, -4.7834,
     -5.8870, -3.4839, -5.0104, -3.0992, -4.8823, -0.3319, -6.8084])
ind = np.argpartition(arr, -4)[-4:]
print(arr[ind])

and the output is

[-3.0992 -3.0302 -0.3319 -2.7103]

which is unsorted, it has to output the maximum values at last but it is not seen in this case. I checked with other examples and it is doing all fine. Like

arr = np.array([45, 35, 67.345, -34.5555, 66, -0.23655, 11.0001, 0.234444444])
ind = np.argpartition(arr, -4)[-4:]
print(arr[ind])

output

[35.    45.    66.    67.345]

What could be the reason? Did I miss anything?

1 Answers1

1

If you're not planning on actually utilizing the sorted indices, why not just use np.sort?

>>> arr = np.array([-3.0302, -2.7103, -7.4844, -3.4761, -5.3009, -5.2121, -3.7549, 
              -4.7834, -5.8870, -3.4839, -5.0104, -3.0992, -4.8823, -0.3319, -6.8084])

>>> np.sort(arr)[-4:]
array([-3.0992, -3.0302, -2.7103, -0.3319])

Alternatively, as read here you could use a range for your kth option on np.argpartition:

np.argpartition(arr, range(0, -4, -1))[-4:]
array([-3.0992, -3.0302, -2.7103, -0.3319])
Ivan
  • 34,531
  • 8
  • 55
  • 100
  • Hey, sorry that I didn't mention this. I am actually planning to use the sorted indices as well to map with the array of class names associated with it. Finally I am printing the top k classified classes with scores. Example output: Dog: -0.3319 Cat: -2.7103 Cow: -3.0302 So I need indices along with values. – Senthil Vikram Vodapalli Aug 04 '21 at 10:57