Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Diagnosing and tuning warping#
How much warping does a data set need? Path statistics (the share of diagonal steps, the longest horizontal or vertical run, the area between the path and the diagonal) and the tightness of warping (ToW) describe the alignments; the window that maximizes the leave-one-out accuracy can be learned (Ratanamahatana and Keogh, 2004); and an elastic ensemble (Lines and Bagnall, 2015) combines several measures.
Path statistics of a pair#
res = wp.dtw_path(C[0], C[1])
print({k: round(v, 3) for k, v in wp.path_statistics(res.path).items()})
paths = [wp.dtw_path(C[0], C[j]).path for j in (1, 2, 3)]
viz.plot_warping_function(paths, labels=["pair 0-1", "pair 0-2", "pair 0-3"])

{'length': 124.0, 'tightness': 1.292, 'warping_amount': 0.455, 'max_horizontal': 6.0, 'max_vertical': 6.0, 'area': 0.028, 'pathology': 0.062}
<Axes: xlabel='time of x (normalized)', ylabel='time of y (normalized)'>
Learning the warping window#
best, scores = wp.learn_window(
C, y, candidates=(0.0, 0.02, 0.05, 0.1, 0.2, None)
)
print(
"accuracy by window:",
{str(k): round(v, 3) for k, v in scores.items()},
"-> best",
best,
)
accuracy by window: {'0.0': 0.889, '0.02': 0.917, '0.05': 0.944, '0.1': 0.972, '0.2': 0.944, 'None': 0.944} -> best 0.1
An elastic ensemble#
rng = np.random.default_rng(0)
idx = rng.permutation(len(C))
train, test = idx[:24], idx[24:]
ee = wp.ElasticEnsemble(
metrics=("dtw", "ddtw", "msm", ("lcss", {"epsilon": 0.5}))
).fit([C[i] for i in train], y[train])
pred = ee.predict([C[i] for i in test])
print(
"weights:",
{k: round(v, 2) for k, v in ee.weights_.items()},
"| test accuracy:",
np.mean(pred == y[test]),
)
plt.show()
weights: {'dtw': 0.88, 'ddtw': 0.71, 'msm': 1.0, 'lcss': 1.0} | test accuracy: 1.0
Total running time of the script: (0 minutes 0.378 seconds)