我见过^{
我也试过用R:
def getDTW(A, B):
""" Calculate the distance of A and B by greedy dynamic time warping.
@param list A list of points
@param list B list of points
@return float Minimal distance you have to move points from A to get B
>>> '%.2f' % greedyMatchingDTW([{'x': 0, 'y': 0}, {'x': 1, 'y': 1}], \
[{'x': 0, 'y': 0}, {'x': 0, 'y': 5}])
'4.12'
>>> '%.2f' % greedyMatchingDTW([{'x': 0, 'y': 0}, {'x':0, 'y': 10}, \
{'x': 1, 'y': 22}, {'x': 2, 'y': 2}], \
[{'x': 0, 'y': 0}, {'x': 0, 'y': 5}])
'30.63'
>>> '%.2f' % greedyMatchingDTW( [{'x': 0, 'y': 0}, {'x': 0, 'y': 5}], \
[{'x': 0, 'y': 0}, {'x':0, 'y': 10}, \
{'x': 1, 'y': 22}, {'x': 2, 'y': 2}])
'30.63'
"""
global logging
import numpy as np
import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr
rpy2.robjects.numpy2ri.activate()
# Set up our R namespaces
R = rpy2.robjects.r
DTW = importr('dtw')
An, Bn = [], []
for p in A:
An.append([p['x'], p['y']])
for p in B:
Bn.append([p['x'], p['y']])
alignment = R.dtw(np.array(An), np.array(Bn), keep=True)
dist = alignment.rx('distance')[0][0]
return dist
# I would expect 0 + sqrt(1**2 + (-4)**1) = sqrt(17) = 4.123105625617661
print(getDTW([{'x': 0, 'y': 0}, {'x': 1, 'y': 1}],
[{'x': 0, 'y': 0}, {'x': 0, 'y': 5}]))
# prints 5.53731918799 - why?
但正如我在底部指出的,R并没有给出预期的解。
那么:如何计算Python中两个2D点列表之间的DTW?
DTW python库的比较及使用方法
dtaidistance是目前最快的。在
以下是dtaidistance git:
https://github.com/wannesm/dtaidistance
要安装,只需:
^{pr2}$你的期望似乎没有考虑到阶梯模式。如果在R中运行以下命令
你得到了你期望的结果。默认的步骤模式是
^{pr2}$symetric2
所以我很确定R计算的值是正确的,只是你的期望值可能与那个函数的默认值不一致。在
对于第二个示例,symmetric2似乎符合您的期望
我没能达到你的第三个期望。我建议您阅读package documentation了解更多细节。在
相关问题 更多 >
编程相关推荐