两台计算机上相同的numpy均值计算结果不同

Mad*_*sen 5 numpy python-2.7

我有两台带有 python 2.7.2(MSC v.1500 32 位(英特尔)] on win32)和 numpy 1.6.1 的计算机。但

numpy.mean(data)
Run Code Online (Sandbox Code Playgroud)

返回

1.13595094681 on my old computer 
Run Code Online (Sandbox Code Playgroud)

1.13595104218 on my new computer
Run Code Online (Sandbox Code Playgroud)

在哪里

Data = [ 0.20227873 -0.02738848  0.59413314  0.88547146  1.26513398  1.21090782
1.62445402  1.80423951  1.58545554  1.26801944  1.22551131  1.16882968
1.19972098  1.41940248  1.75620842  1.28139281  0.91190684  0.83705413
1.19861531  1.30767155]
Run Code Online (Sandbox Code Playgroud)

在这两种情况下

s=0
for n in data[:20]:
  s+=n
print s/20
Run Code Online (Sandbox Code Playgroud)

1.1359509334
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么以及如何避免?

疯子

bog*_*ron 3

如果您想避免两者之间的任何差异,请明确将它们设置为 32 位或 64 位浮点数组。NumPy 使用其他几个可能是 32 位或 64 位的库。请注意,四舍五入也可能出现在打印语句中:

>>> import numpy as np
>>> a = [0.20227873, -0.02738848,  0.59413314,  0.88547146,  1.26513398,
         1.21090782, 1.62445402,  1.80423951,  1.58545554,  1.26801944,
         1.22551131,  1.16882968, 1.19972098,  1.41940248,  1.75620842,
         1.28139281,  0.91190684,  0.83705413, 1.19861531,  1.30767155]
>>> x32 = np.array(a, np.float32)
>>> x64 = np.array(a, np.float64)
>>> x32.mean()
1.135951042175293
>>> x64.mean()
1.1359509335
>>> print x32.mean()
1.13595104218
>>> print x64.mean()
1.1359509335
Run Code Online (Sandbox Code Playgroud)

另一点需要注意的是,如果您有多线程的较低级别的库(例如 atlas、lapack),那么对于大型数组,由于操作顺序和浮点精度可能存在变量,您的结果可能会有所不同。

此外,您还处于 32 位数字的精度限制:

>>> x32.sum()
22.719021
>>> np.array(sorted(x32)).sum()
22.719019
Run Code Online (Sandbox Code Playgroud)