rpy2:DTW包无法使用提供的步骤模式

时间:2017-03-23 09:12:23

标签: python r python-2.7 rpy2

我目前正在尝试使用rpy2访问" R" DTW(动态时间扭曲)包计算多变量时间序列之间的距离。也许因为时间序列真的不同,我得到错误"No warping path exists that is allowed by costraints"

我认为库中的默认步骤模式是对称的,但我想用非对称步骤模式测试它,但是当我尝试将步骤模式作为"非对称"时,玩具代码不起作用。 。以下是代码

import numpy as np
from sklearn import cluster
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()
from rpy2.robjects.packages import importr
import rpy2.robjects as robj


"""Example of DTW calculation, it's 2 variables , 5 timestamps and 16 timestamps each"""
R = rpy2.robjects.r
DTW = importr('dtw')

# Generate our data
template = np.array([[1,2,3,4,5],[1,2,3,4,5]]).transpose()
rt,ct = template.shape
query = np.array([[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]]).transpose()
rq,cq = query.shape

templateR=R.matrix(template,nrow=rt,ncol=ct)
queryR=R.matrix(query,nrow=rq,ncol=cq)
alignment = R.dtw(templateR,queryR,keep=True, step_pattern=R.asymmetric)
alignment = R.dtw(templateR, queryR, keep=True)
dist = alignment.rx('distance')[0][0]

print dist

我也查看过以下示例,但它不起作用:

rpy2 dtw missing argument window.size

谢谢!

1 个答案:

答案 0 :(得分:0)

库中的默认步骤模式是symmetric2。与其他人一起测试:

alignment = R.dtw(templateR,queryR,keep = True,step_pattern = DTW.asymmetric)

不是R.asymmetric

希望有所帮助