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