我有一个从数据文件创建NumPy数组的函数。我想获得数组中的最大值和该值的索引:
import numpy as np
def dostuff():
# open .txt file into lists
# copy lists into numpy array
# nested for loops and values copied into numpy array called a
print a
print np.max(a)
print np.argmax(a)
dostuff()
运行此命令:
[[ 0.64 0.47 0.22 0.1 0.05 0.02]
[ 2.19 9.13 10.68 6.44 3.36 1.77]
[ 1.84 8.81 12.6 8.31 4.45 2.35]]
2.35
0
显然np.max()和np.argmax()出了问题。这可以使用以下代码
显示def test():
a = np.array([[0.64, 0.47, 0.22, 0.1, 0.05, 0.02],
[2.19, 9.13, 10.68, 6.44, 3.36, 1.77],
[1.84, 8.81, 12.6, 8.31, 4.45, 2.35]])
print a
print np.max(a)
print np.argmax(a)
test()
这给出了:
[[ 0.64 0.47 0.22 0.1 0.05 0.02]
[ 2.19 9.13 10.68 6.44 3.36 1.77]
[ 1.84 8.81 12.6 8.31 4.45 2.35]]
12.6
14
......这是我所期待的。我不知道为什么这两个(显然)相同的数组在这里给出不同的结果。有谁知道我可能做了什么?