numpy:查找包含浮点值的两个文件之间的差异 [英] Numpy:Finding difference between two files containing float values
问题描述
我正在尝试使用Python计算两个文本文件之间的差异,并打印第一个值和它们开始发散的位置.
我不确定如何使用loadtxt
:
import numpy as np
a = np.loadtxt("path/to/file", float)
b = np.loadtxt("path/to/file2", float)
while np.absolute(a - b) !=0:
1
2
3
...
不确定如何完成此操作?开始正确吗?
您可以使用
idx = np.where(np.abs(a-b) > 1e-6)[0]
firstidx = idx[0]
查找第一个索引,其中a
和b
中的值相差超过某些标称值,例如1e-6
:
import numpy as np
a = np.loadtxt("path/to/file", float)
b = np.loadtxt("path/to/file2", float)
idx = np.where(np.abs(a-b) > 1e-6)[0]
firstidx = idx[0]
print(firstidx, a[firstidx], b[firstidx])
请注意,在处理浮点数时,很少会想与相等进行比较,例如
np.abs(a-b) == 0
或相反,
np.abs(a-b) != 0
因为浮点表示的不准确性可能会导致a
和b
略有不同,即使它们的值应精确地表示为 时也是如此.>
所以使用类似的东西
np.abs(a-b) > 1e-6
相反. (请注意,您必须选择容忍度,例如1e-6).
这是一个简单示例使用相等性比较浮点数的陷阱:
In [10]: 1.2-1.0 == 0.2
Out[10]: False
I am trying to use Python to compute the difference between two text files and print the first value and location where they start to diverge.
I am not sure how to use loadtxt
:
import numpy as np
a = np.loadtxt("path/to/file", float)
b = np.loadtxt("path/to/file2", float)
while np.absolute(a - b) !=0:
1
2
3
...
Not sure how to finish this? Is the start correct?
You could use
idx = np.where(np.abs(a-b) > 1e-6)[0]
firstidx = idx[0]
to find the first index where the values in a
and b
differ by more than some nominal amount like 1e-6
:
import numpy as np
a = np.loadtxt("path/to/file", float)
b = np.loadtxt("path/to/file2", float)
idx = np.where(np.abs(a-b) > 1e-6)[0]
firstidx = idx[0]
print(firstidx, a[firstidx], b[firstidx])
Note that when dealing with floats, you rarely if ever want to compare with equality, such as with
np.abs(a-b) == 0
or the converse,
np.abs(a-b) != 0
because the inaccuracy of floating point representations can cause a
and b
to be slightly different even when their values should be exactly the same if their values were represented with infinite precision.
So use something like
np.abs(a-b) > 1e-6
instead. (Note that you have to choose a level of tolerance, e.g. 1e-6).
Here is a simple example demonstrating the pitfall of comparing floats using equality:
In [10]: 1.2-1.0 == 0.2
Out[10]: False
这篇关于numpy:查找包含浮点值的两个文件之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!