.. _sphx_glr_auto_examples_plot_fastdtw.py: ========================= Fast Dynamic Time Warping ========================= This example shows how to compute and visualize the optimal path when computing the Fast Dynamic Time Warping distance between two time series. It is implemented as :func:`pyts.utils.fast_dtw`. .. image:: /auto_examples/images/sphx_glr_plot_fastdtw_001.png :align: center .. code-block:: python import numpy as np import matplotlib.pyplot as plt from pyts.utils import fast_dtw # Parameters n_samples, n_features = 2, 48 # Toy dataset rng = np.random.RandomState(41) x, y = rng.randn(n_samples, n_features) # Dynamic Time Warping region, D, path = fast_dtw(x, y, dist='absolute', window_size=6, approximation=False, return_path=True) # Visualize the result timestamps = np.arange(n_features + 1) matrix = np.zeros([n_features + 1, n_features + 1]) for i in range(n_features): for j in region[i]: matrix[j, i] = 0.5 for i in range(len(path)): matrix[path[i][0], path[i][1]] = 1 plt.figure(figsize=(8, 8)) plt.pcolor(timestamps, timestamps, matrix, edgecolors='k', cmap='Greys') plt.xlabel('x', fontsize=20) plt.ylabel('y', fontsize=20) plt.title("Path along the constrained cost matrix", fontsize=16) plt.suptitle("Fast Dynamic Time Warping", fontsize=22) plt.show() **Total running time of the script:** ( 0 minutes 0.137 seconds) .. only :: html .. container:: sphx-glr-footer .. container:: sphx-glr-download :download:`Download Python source code: plot_fastdtw.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_fastdtw.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_