← 开源

DTW (Dynamic Time Warping) python module

Model DevelopmentClassical MLPython
增长势头
+024 小时新增 Star0.0%
1.24k
Star
236
Fork
+0
本周
10
贡献者
创建于 2014-07-28 · 更新于 2026-11-26 · 今日第 13609 名
主要开发者
README

Dynamic Time Warping Python Module

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

  • the basic version (see here) for the algorithm
  • an accelerated version which relies on scipy cdist (see #8 for detail)
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

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

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

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:

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.

排名相邻
同类项目

AI Rank iPhone 版

随时追踪 AI 模型榜单

在 App Store 下载