FastDTW
FastDTW is a Python implementation of the FastDTW algorithm, which provides optimal or near-optimal alignments with an O(N) time and memory complexity for Dynamic Time Warping (DTW) of time series. The current version is 0.3.4, with its last release in October 2019, suggesting a maintenance rather than active development phase.
Warnings
- gotcha Contrary to its name and common perception, recent research suggests that 'fastdtw' can be significantly slower than standard Dynamic Time Warping (DTW) implementations in many realistic data mining applications, while also providing only an approximate result. Users should benchmark performance against exact DTW algorithms for their specific use case.
- gotcha The `fastdtw` algorithm is an approximation, and its accuracy is controlled by the `radius` parameter. A larger `radius` value generally leads to a more accurate, but computationally more expensive, result. If the `radius` is set sufficiently large, FastDTW effectively generalizes to the O(N^2) standard DTW algorithm, losing its linear time complexity advantage.
- gotcha While the PyPI metadata for `fastdtw` doesn't explicitly declare `requires_python`, the GitHub repository's `Pipfile` suggests Python 3.7. Given the library's last update in 2019, compatibility with very recent Python versions (e.g., 3.10+) should be verified, as dependencies might have evolved.
Install
-
pip install fastdtw
Imports
- fastdtw
from fastdtw import fastdtw
Quickstart
import numpy as np
from scipy.spatial.distance import euclidean
from fastdtw import fastdtw
x = np.array([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]], dtype=float)
y = np.array([[2, 2], [3, 3], [4, 4]], dtype=float)
distance, path = fastdtw(x, y, dist=euclidean)
print(f"DTW Distance: {distance}")
print(f"Warping Path: {path}")