在Python中查找给定数组中的最小值索引

时间:2022-12-12 21:28:52

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.

我需要找到数组中出现的多个最小值的索引。我很熟悉np.argmin,但它给了我一个数组中第一个最小值的索引。例如。

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.

这给了我0,而不是我期待的,0,5,7。

Thanks!

谢谢!

3 个解决方案

#1


21  

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.

在这种情况下,argmin不会像您期望的那样返回列表。

#2


3  

Maybe

也许

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

It will give [0,5,7].

它会给[0,5,7]。

#3


1  

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

我认为这将是最简单的方法,虽然它不使用任何花哨的numpy功能

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)

#1


21  

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.

在这种情况下,argmin不会像您期望的那样返回列表。

#2


3  

Maybe

也许

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

It will give [0,5,7].

它会给[0,5,7]。

#3


1  

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

我认为这将是最简单的方法,虽然它不使用任何花哨的numpy功能

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)