如果您的真值文件和测量文件包含足够接近的时间点,您可以使用这些时间点配对您的数据:
import numpy as np
# set up dummy data
truthdat = np.arange(25)[:,None]*[0.1,1]
measdat = np.array([[0.01, 0.01], [0.99, 9.99], [2.01,20.05]])
# find the temporal indices which correspond to one another
i_meas,i_truth = np.where(np.isclose(measdat[:,None,0],truthdat[:,0],atol=0.05))
我们所做的是利用数组广播,允许我们以矢量化的方式比较measdat 中的每个时间与truthdat 中的每个时间。另请注意,我也允许时间数据有一些变化。如果它们完全相同,您可以在np.where 中使用measdat[:,None,0]==truthdat[:,0]。
生成的索引为我们提供了成对的数据点:
>>> measdat[i_meas]
array([[ 1.00000000e-02, 1.00000000e-02],
[ 9.90000000e-01, 9.99000000e+00],
[ 2.01000000e+00, 2.00500000e+01]])
>>> truthdat[i_truth]
array([[ 0., 0.],
[ 1., 10.],
[ 2., 20.]])
现在您可以类似地使用 np.isclose 和您选择的容差来比较这些数据对的第二列:
# tell if all values are within atol=0.05 absolute error
are_close = np.allclose(measdat[i_meas,1],truthdat[i_truth,1],atol=0.05)
# compute the error for each measured point
abserrors = measdat[i_meas,1] - truthdat[i_truth,1]
并根据需要继续进行任何其他后期处理。