在Python中查找给定数组中最小值的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19546863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the index of minimum values in given array in Python
提问by user2766019
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!
谢谢!
采纳答案by Tom Swifty
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 不会像您期望的那样返回列表。
回答by tonjo
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]。
回答by jrk0414
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)