Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the indices of the top three values via argmin() or min() in python/numpy without mutation of list?

So I have this list called sumErrors that's 16000 rows and 1 column, and this list is already presorted into 5 different clusters. And what I'm doing is slicing the list for each cluster and finding the index of the minimum value in each slice.

However, I can only find the first minimum index using argmin(). I don't think I can just delete the value, because otherwise it would shift the slices over and the indices is what I have to recover the original ID. Does anyone know how to get argmin() to spit out indices for the lowest three?

Or perhaps a more optimal method? Maybe I should just assign ID numbers, but I feel like there maybe a more elegant method.

like image 318
Shinjitsu Avatar asked Dec 08 '12 23:12

Shinjitsu


People also ask

What does argmin do in Numpy?

argmin. Returns the indices of the minimum values along an axis.

How do you find argmin in Python?

The numpy argmin() function takes arr, axis, and out as parameters and returns the array. To find the index of a minimum element from the array, use the np. argmin() function.


2 Answers

Numpy includes an argsort function which will return all the indices. If I understand your requirement correctly, you should be able to do:

minidx = []
for cluster in sumErrors:
    minidx.append(np.argsort(cluster)[:3])
like image 63
mtrw Avatar answered Sep 17 '22 00:09

mtrw


numpy.argpartition(cluster, 3) would be much more effective.

like image 28
ton4eg Avatar answered Sep 19 '22 00:09

ton4eg