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

2

14

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.

Outsail answered 8/12, 2012 at 23:33 Comment(0)
F
15

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])
Fulmis answered 8/12, 2012 at 23:42 Comment(1)
OOOOOH I spent sooo long working on that. I knew there had to have been an easy way. Thank you so much!Outsail
S
4

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

Smarm answered 8/5, 2016 at 0:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.