dtw-python is a comprehensive Python library that provides a faithful, functionally equivalent implementation of Dynamic Time Warping (DTW) algorithms, mirroring the capabilities of the popular R 'dtw' package. It enables optimal alignment between two time series, even if they have different lengths or time axes, and supports various local (slope) and global (window) constraints. The library is currently at version 1.7.4 and maintains an active, though irregular, release cadence, with several updates per year. It is widely used for classification and clustering tasks across domains like bioinformatics, econometrics, and general time series analysis.
pip install dtw-pythonVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to compute Dynamic Time Warping between two NumPy arrays. It defines a query and a template time series, then uses the `dtw()` function to find their optimal alignment. The resulting `alignment` object contains the DTW distance and can be used for plotting the warping path (if `matplotlib` is installed).
Adjust all indices (e.g., `index1`, `index2`, `index1s`, `index2s` attributes of alignment objects) to be 0-based when working in Python.
Use Pythonic underscore-separated full argument names in function calls (e.g., `dtw(..., keep_internals=True)`).
Install `matplotlib` separately (`pip install matplotlib`) or install `dtw-python` with the `plots` extra: `pip install dtw-python[plots]`.
Review your `dist_method` choice and input data. Consider using a more robust distance metric like 'euclidean' if `NaN`s appear unexpectedly, or preprocess data to handle potential division by zero or undefined values if using cosine distance.
Ensure your system's C compiler is up-to-date. If using Conda, `conda install gcc_linux-64` before `pip install dtw-python` may resolve the issue.
The correct import statement is `import dtw` or `from dtw import dtw` (or `from dtw import *`). The package name `dtw-python` is for `pip install`.
Inspect the input time series for `NaN`s, `inf`s, or problematic values (e.g., all zeros if using cosine distance). Consider changing the `dist_method` parameter to a more numerically stable option like 'euclidean' if the current metric is not essential.
Explicitly set the `step_pattern`, `dist_method`, and `window_type` arguments in `dtw-python` to match the configuration used in the other library for a fair comparison. Pay attention to 0-based vs 1-based indexing conventions.