Multidimensional/multivariate dynamic time warping (DTW) library/code in Python

前端 未结 3 1584
温柔的废话
温柔的废话 2021-02-08 17:31

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:

|

3条回答
  •  灰色年华
    2021-02-08 18:26

    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.

    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
    

提交回复
热议问题