简体   繁体   English

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

[英]Find the index of minimum values in given array in Python

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. 我很熟悉np.argmin但它给了我一个数组中第一个最小值的索引。 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. 这给了我0,而不是我期待的,0,5,7。

Thanks! 谢谢!

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不会像您期望的那样返回列表。

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]。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM