dtw

Python DTW Module


Keywords
distance, distance-measures, distance-metric, dtw, python
License
GPL-3.0
Install
pip install dtw==1.3.1

Documentation

Dynamic Time Warping Python Module

Build Status

Dynamic time warping is used as a similarity measured between temporal sequences. This package provides two implementations:

import numpy as np

# We define two sequences x, y as numpy array
# where y is actually a sub-sequence from x
x = np.array([2, 0, 1, 1, 2, 4, 2, 1, 2, 0]).reshape(-1, 1)
y = np.array([1, 1, 2, 4, 2, 1, 2, 0]).reshape(-1, 1)

from dtw import dtw

euclidean_norm = lambda x, y: np.abs(x - y)

d, cost_matrix, acc_cost_matrix, path = dtw(x, y, dist=euclidean_norm)

print(d)
>>> 2.0 # Only the cost for the insertions is kept

# You can also visualise the accumulated cost and the shortest path
import matplotlib.pyplot as plt

plt.imshow(acc_cost_matrix.T, origin='lower', cmap='gray', interpolation='nearest')
plt.plot(path[0], path[1], 'w')
plt.show()

Result of the accumulated cost matrix and the shortest path (in white) found: Acc cost matrix and shortest path

Other examples are available as notebook

Installation

python -m pip install dtw

It is tested on Python 2.7, 3.4, 3.5 and 3.6. It requires numpy and scipy.