Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the index of minimum values in given array in Python

Tags:

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!

like image 509
user2766019 Avatar asked Oct 23 '13 16:10

user2766019


People also ask

How do you find the index of the minimum of an array?

Use the Array. indexOf() method to get the index of the min value. The indexOf method returns the index of the first occurrence of the value in the array.

How do you find the index of minimum value in numpy array?

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.

How do you return the index of the smallest value in a list in Python?

Use min() and list. index() Functions to Get the Smallest Index of the List. In Python, the min() method will return the smallest value of the defined list.

How do you find the index of the maximum value in an array in Python?

Use the enumerate() function to find out the index of the maximum value in a list. Use the numpy. argmax() function of the NumPy library to find out the index of the maximum value in a list.


2 Answers

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.

like image 81
Tom Swifty Avatar answered Dec 02 '22 01:12

Tom Swifty


Maybe

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

It will give [0,5,7].

like image 41
tonjo Avatar answered Dec 01 '22 23:12

tonjo