Python中的多维/多元动态时间规整(DTW)库/代码 [英] Multidimensional/multivariate dynamic time warping (DTW) library/code in Python

查看:820
本文介绍了Python中的多维/多元动态时间规整(DTW)库/代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理时间序列数据。可用数据是多变量的。因此,对于每个时间实例,都有三个数据点可用。
格式:

I am working on a time series data. The data available is multi-variate. So for every instance of time there are three data points available. Format:


| X | Y | Z |

| X | Y | Z |

因此将实时生成上述格式的一个时间序列数据。我试图在另一个已存储的时间序列基础数据中找到此实时生成的时间序列的良好匹配项(它的大小要大得多,并且是以不同的频率收集的)。如果我对每个系列(X,Y,Z)分别应用标准DTW,则它们最终可能会在基础数据库中的不同点得到匹配,这是不利的。因此,我需要在基础数据库中找到一个点,所有三个分量(X,Y,Z)在同一点上都匹配得很好。

So one time series data in above format would be generated real time. I am trying to find a good match of this real time generated time series within another time series base data, which is already stored (which is much larger in size and was collected at a different frequency). If I apply standard DTW to each of the series (X,Y,Z) individually they might end up getting a match at different points within the base database, which is unfavorable. So I need to find a point in base database where all three components (X,Y,Z) match well and at the same point.

我已经研究了此问题并发现多维DTW是解决此问题的完美解决方案。在R中,dtw软件包确实包含多维DTW,但是我必须在Python中实现它。 R-Python桥接程序包 rpy2在这里可能会有所帮助,但是我没有R的经验。我浏览了Python中可用的DTW程序包,例如mlpy,dtw,但没有帮助。谁能建议使用Python的软件包使用rpy2做相同的事情或使用多维DTW的代码。

I have researched into the matter and found out that multidimensional DTW is a perfect solution to such a problem. In R the dtw package does include multidimensional DTW but I have to implement it in Python. The R-Python bridging package namely "rpy2" can probably of help here but I have no experience in R. I have looked through available DTW packages in Python like mlpy, dtw but are not help. Can anyone suggest a package in Python to do the same or the code for multi-dimensional DTW using rpy2.

预先感谢!

推荐答案

感谢@lgautier我更深入地研究,发现在Python中使用rpy2实现了多变量DTW的实现。只需将模板和查询作为2D矩阵(R中的矩阵)进行传递,将允许rpy2 dtw包执行多元DTW。另外,如果您已经安装了R,则加载R dtw库和?dtw将可以访问该库的文档以及该库提供的各种功能。

Thanks @lgautier I dug deeper and found implementation of multivariate DTW using rpy2 in Python. Just passing the template and query as 2D matrices (matrices as in R) would allow rpy2 dtw package to do a multivariate DTW. Also if you have R installed, loading the R dtw library and "?dtw" would give access to the library's documentation and different functionalities available with the library.

以供将来参考其他有类似问题的用户:
R dtw软件包的正式文档: https://cran.r-project.org/web/packages/dtw/dtw.pdf
示例代码,为多元DTW传递两个二维矩阵,open_begin和open_end参数启用子序列匹配:

For future reference to other users with similar questions: Official documentation of R dtw package: https://cran.r-project.org/web/packages/dtw/dtw.pdf Sample code, passing two 2-D matrices for multivariate DTW, the open_begin and open_end arguments enable subsequence matching:

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

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

#converting numpy matrices to R matrices
templateR=R.matrix(template,nrow=rt,ncol=ct)
queryR=R.matrix(query,nrow=rq,ncol=cq)

# Calculate the alignment vector and corresponding distance
alignment = R.dtw(templateR,queryR,keep=True, step_pattern=R.rabinerJuangStepPattern(4,"c"),open_begin=True,open_end=True)

dist = alignment.rx('distance')[0][0]

print dist

这篇关于Python中的多维/多元动态时间规整(DTW)库/代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆