现在关闭的discussion显示了如何在python中使用R dtw软件包。这有点笨拙,但是R dtw软件包很棒并且比当前可用的python dtw实现更好。不幸的是,当尝试指定“ window.size”时,像Sakoe-Chiba波段之类的窗口功能不起作用。到参数的映射似乎存在问题。注意 ”。”使用rpy2时,应将参数中的in替换为“ _”。但是遵循此约定,出于某种原因未使用该参数。
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')
# Generate our data
idx = np.linspace(0, 2*np.pi, 100)
template = np.cos(idx)
query = np.sin(idx) + np.array(R.runif(100))/10
# Calculate the alignment vector and corresponding distance
alignment = R.dtw(query, template, keep=True,window_type='sakoechiba',
window_size=5)
>>> RRuntimeError: Error in window.function(row(wm), col(wm), query.size= n, reference.size = m, :
argument "window.size" is missing, with no default
您可以看到,尽管以rpy2方式明确指定了“ window_size”,但错误状态“ window.size”仍然丢失。
答案 0 :(得分:0)
回答我自己的问题,以防有人遇到相同的问题。问题在于参数映射和R三个点省略号“ ...”。可以通过手动指定映射来解决。
from rpy2.robjects.functions import SignatureTranslatedFunction
R.dtw = SignatureTranslatedFunction(R.dtw,
init_prm_translate={'window_size': 'window.size'})
因此,在此规范中,正确使用了window_size参数。
import numpy as np
import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr
from rpy2.robjects.functions import SignatureTranslatedFunction
rpy2.robjects.numpy2ri.activate()
# Set up our R namespaces
R = rpy2.robjects.r
DTW = importr('dtw')
R.dtw = SignatureTranslatedFunction(R.dtw,
init_prm_translate={'window_size': 'window.size'})
# Generate our data
idx = np.linspace(0, 2*np.pi, 100)
template = np.cos(idx)
query = np.sin(idx) + np.array(R.runif(100))/10
# Calculate the alignment vector and corresponding distance
alignment = R.dtw(query, template, keep=True,window_type='sakoechiba',
window_size=10)
dist = alignment.rx('distance')[0][0]
print(dist)
>>> 117.348292359
答案 1 :(得分:0)
这只是将来的注释:feature-equivalent dtw-python
package(也可以找到on PyPI)取代了这个问题。不再需要rpy2-R-dtw网桥。