DTW(动态时间归整)算法与DTW,fastDTW的python算例

文章目录

  • DTW算法思路
  • DTW算例
  • fastDTW算例
  • 参考资料

DTW算法思路

论文地址:https://irit.fr/~Julien.Pinquier/Docs/TP_MABS/res/dtw-sakoe-chiba78.pdf
算法思路:

  1. 计算两个序列不同位置点对应的距离,构成一个矩阵
  2. 在矩阵中找到序列的起始位置对应的单元格,与最后一个单元格
  3. 从这个格子开始,找一条路径,从开始到结尾,使路径上单元格值加总最小

DTW算例

Github地址:https://github.com/pierre-rouanet/dtw

import numpy as np
from dtw import dtw

x = np.array([2, 0, 1, 1, 2, 4, 2, 1, 2, 0])
y = np.array([1, 1, 2, 4, 2, 1, 4, 2])
manhattan_distance = lambda x, y: np.abs(x - y)

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

返回值中:
d:总距离值
cost_matrix:所有点的距离矩阵
acc_cost_matrix:累加的距离值
path:最短路径对应的坐标,为tuple类型的数据

fastDTW算例

paper:http://cs.fit.edu/~pkc/papers/tdm04.pdf
算例来自官网:

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]])
y = np.array([[2, 2], [3, 3], [4, 4]])
distance, path = fastdtw(x, y, dist=euclidean)
print(distance)

返回值中:
distance:距离值
path:最短路径对应的坐标,为list(tuple)类型的数据

参考资料

WikiMapia :https://en.wikipedia.org/wiki/Dynamic_time_warping
时间序列相似性度量综述:https://zhuanlan.zhihu.com/p/69170491
HMM学习笔记_1(从一个实例中学习DTW算法):https://www.cnblogs.com/tornadomeet/archive/2012/03/23/2413363.html

你可能感兴趣的:(python,python)