numpy.argmin给了我最大的元素索引

时间:2018-11-12 13:14:23

标签: numpy min

我在使用numpy和argmin时遇到了麻烦。似乎argmin返回最大元素的索引。此示例代码可能会澄清这种情况:

for i in range(20):                
            indmin = np.argmin(M[n-1, 1:-1])
            print("M[n-1, indmin] = ", M[n-1, indmin])

            print("indmin = ", indmin)

            M[n-1, indmin] = inf

这段代码的输出是:

M[n-1, indmin] =  5.806069439930625
indmin =  1150
M[n-1, indmin] =  100000000.0
indmin =  1150
M[n-1, indmin] =  100000000.0
indmin =  1150
M[n-1, indmin] =  100000000.0
indmin =  1150
...

M [n-1,-1:1]包含大约从0到15的值。

编辑: n = 813; inf = 100000000

链接到M转储: [http://www.mediafire.com/file/wjbk11tiafjo3do/probarray/file][1]

1 个答案:

答案 0 :(得分:2)

尝试:

for i in range(20):                
            indmin = np.argmin(M[n-1, 1:-1])
            print("M[n-1, indmin] = ", M[n-1, indmin])

            print("indmin = ", indmin)

            M[n-1, indmin+1] = inf # correction, the argmin was taken with an offset of 1

argmin的参数为M[n-1, 1:-1],因此您获得的索引不是与M的索引匹配的索引。相移1。