我不知道 Python 中有任何内置函数可以做到这一点。
我可以为您提供 Python 生态系统中您可以使用的可能功能的列表。这绝不是函数的完整列表,可能还有很多我不知道的方法。
如果数据是有序的,但你不知道哪个数据点是第一个,哪个数据点是最后一个:
- 使用有向 Hausdorff 距离
如果数据是有序的,并且你知道第一个和最后一个点是正确的:
- 离散 Fréchet 距离 *
- 动态时间规整 (DTW) *
- 部分曲线映射 (PCM) **
- 曲线长度距离度量(使用从开始到结束的弧长距离)**
- 两条曲线之间的面积**
* 用于各种机器学习任务的一般数学方法
**我用来识别独特材料滞后响应的方法
首先让我们假设我们有两个完全相同的随机 X Y 数据。请注意,所有这些方法都将返回零。如果没有,可以从 pip 安装相似度度量。
import numpy as np
from scipy.spatial.distance import directed_hausdorff
import similaritymeasures
import matplotlib.pyplot as plt
# Generate random experimental data
np.random.seed(121)
x = np.random.random(100)
y = np.random.random(100)
P = np.array([x, y]).T
# Generate an exact copy of P, Q, which we will use to compare
Q = P.copy()
dh, ind1, ind2 = directed_hausdorff(P, Q)
df = similaritymeasures.frechet_dist(P, Q)
dtw, d = similaritymeasures.dtw(P, Q)
pcm = similaritymeasures.pcm(P, Q)
area = similaritymeasures.area_between_two_curves(P, Q)
cl = similaritymeasures.curve_length_measure(P, Q)
# all methods will return 0.0 when P and Q are the same
print(dh, df, dtw, pcm, cl, area)
打印输出是
0.0, 0.0, 0.0, 0.0, 0.0, 0.0
这是因为曲线P和Q完全一样!
现在让我们假设 P 和 Q 不同。
# Generate random experimental data
np.random.seed(121)
x = np.random.random(100)
y = np.random.random(100)
P = np.array([x, y]).T
# Generate random Q
x = np.random.random(100)
y = np.random.random(100)
Q = np.array([x, y]).T
dh, ind1, ind2 = directed_hausdorff(P, Q)
df = similaritymeasures.frechet_dist(P, Q)
dtw, d = similaritymeasures.dtw(P, Q)
pcm = similaritymeasures.pcm(P, Q)
area = similaritymeasures.area_between_two_curves(P, Q)
cl = similaritymeasures.curve_length_measure(P, Q)
# all methods will return 0.0 when P and Q are the same
print(dh, df, dtw, pcm, cl, area)
打印输出是
0.107、0.743、37.69、21.5、6.86、11.8
根据每种方法量化 P 与 Q 的差异。
您现在有很多方法可以比较两条曲线。我将从 DTW 开始,因为它已在许多时间序列应用程序中使用,看起来像您上传的数据。
我们可以用下面的代码可视化 P 和 Q 的样子。
plt.figure()
plt.plot(P[:, 0], P[:, 1])
plt.plot(Q[:, 0], Q[:, 1])
plt.show()