25

I need to find the index of more than one minimum values that occur in an array. I am pretty known with np.argmin but it gives me the index of very first minimum value in a array. For example.

a = np.array([1,2,3,4,5,1,6,1])    
print np.argmin(a)

This gives me 0, instead I am expecting, 0,5,7.

Thanks!

2

3 Answers 3

29

This should do the trick:

a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())

argmin doesn't return a list like you expect it to in this case.

Sign up to request clarification or add additional context in comments.

2 Comments

No, it doesnt. Moreover, you are overwriting a built-in function min.
@BasSwinckels you missed my edit. regular min works fine as you pointed out.
4

Maybe

mymin = np.min(a)
min_positions = [i for i, x in enumerate(a) if x == mymin]

It will give [0,5,7].

2 Comments

No, it doesn't, since you are comparing to the index of the min, which is 0.
EDITED, use min instead of argmin, sorry.
1

I think this would be the easiest way, although it doesn't use any fancy numpy function

a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.