修复*显示*在Python中浮动时的精度问题 [英] Fix precision issues when *displaying* floats in python
问题描述
我正在使用 np.loadtxt
读出一些浮点数的文本文件。这是我的numpy数组看起来像:
x = np.loadtxt(t2)
print(x)
array([[1.00000000e + 00,6.61560000e-13],
[2.00000000e + 00,3.05350000e-13],
[3.00000000e + 00,6.22240000e -13],
[4.00000000e + 00,3.08850000e-13],
[5.00000000e + 00,1.1170000e-10],
[6.00000000e + 00,3.82440000e-11 ],
[7.00000000e + 00,5.39160000e-11],
[8.00000000e + 00,1.75910000e-11],
[9.00000000e + 00,2.27330000e-10]] )
我将第一列从第二列中分离出来:
idx,coeffs = zip(* x)
现在,我想创建一个id:coeff的映射,如下所示:
$ b $ $ $ $ $ code mapping = dict zip(map(int,idx),coeffs))
print(mapping)
{1:6.6155999999999996e-13,
2:3.0535000000000001e-13,
3:6.2223999999999998e-13,
4:3.0884999999999999e-13,
5:1.1117e-10,
6:3.8243999999999997e-11,
7:5.3915999999999998e-11,
8:正如你所看到的,你可以看到,你可以看到,你可以看到,精度错误已经被引入。例如, 6.61560000e-13
变成 6.6155999999999996e-13
。
这就是我最喜欢的:
{1:6.61560000e-13,
2 :3.05350000e-13,
3:6.22240000e-13,
4:3.08850000e-13,
...
}
我该怎么做?我正在研究IPython3,如果有帮助的话。
解决方案 {1:6.6156e-13,
2:3.0535e-13,
3:6.2224e-13,
4:3.0885e-13,
5:1.1117e-10,
6:3.8244e- 11,
7:5.3916e-11,
8:1.7591e-11,
9:2.2733e-10}
$ b $ p 原因是因为 x
的类型是 np.float64
。调用 .tolist()
将 x
转换为列表列表,其中每个元素的类型都是 double。
np.float64
和 double
有不同的 __ repr__
的实现。 double
使用 David Gay算法来正确表示这些花车,而numpy有一个更简单的实现(仅截断)。
I'm reading out a text file with some float numbers using np.loadtxt
. This is what my numpy array looks like:
x = np.loadtxt(t2)
print(x)
array([[ 1.00000000e+00, 6.61560000e-13],
[ 2.00000000e+00, 3.05350000e-13],
[ 3.00000000e+00, 6.22240000e-13],
[ 4.00000000e+00, 3.08850000e-13],
[ 5.00000000e+00, 1.11170000e-10],
[ 6.00000000e+00, 3.82440000e-11],
[ 7.00000000e+00, 5.39160000e-11],
[ 8.00000000e+00, 1.75910000e-11],
[ 9.00000000e+00, 2.27330000e-10]])
I separate out the first column from the second by doing this:
idx, coeffs = zip(*x)
Now, I want to create a mapping of id : coeff, something like this:
mapping = dict(zip(map(int, idx), coeffs))
print(mapping)
{1: 6.6155999999999996e-13,
2: 3.0535000000000001e-13,
3: 6.2223999999999998e-13,
4: 3.0884999999999999e-13,
5: 1.1117e-10,
6: 3.8243999999999997e-11,
7: 5.3915999999999998e-11,
8: 1.7591e-11,
9: 2.2733e-10}
As you can see, precision errors have been introduced. For example, 6.61560000e-13
became 6.6155999999999996e-13
.
This is what I would like, preferrably:
{1: 6.61560000e-13,
2: 3.05350000e-13,
3: 6.22240000e-13,
4: 3.08850000e-13,
...
}
How can I do this? I am working on IPython3, if that helps.
Jean-François Fabre's comment gave me an idea, and I tried it out. Taking into consideration Alexander's suggestion to use a dict comprehension, this worked for me:
x = np.loadtxt(t2)
mapping = {int(k) : v for k, v in x.tolist()}
print (mapping)
Output:
{1: 6.6156e-13,
2: 3.0535e-13,
3: 6.2224e-13,
4: 3.0885e-13,
5: 1.1117e-10,
6: 3.8244e-11,
7: 5.3916e-11,
8: 1.7591e-11,
9: 2.2733e-10}
The reason this works is because x
is of type np.float64
. Calling .tolist()
converts x
to a list of lists, where each element is of type double.
np.float64
and double
have different __repr__
implementations. The double
uses the David Gay Algorithm to correctly represent these floats, while numpy has a much simpler implementation (mere truncation).
这篇关于修复*显示*在Python中浮动时的精度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!