DTW(动态时间调整)算法原理

一、介绍

定义:动态时间规整(Dynamic Time Warping,DTW)算法是一种用于计算两个时间序列之间距离的方法。该算法可以测量两个序列之间的相似度,即使它们的长度和比例不同,并且可以在一些应用场景中对于时间序列的匹配、识别、分类等任务有很好的效果。

基本思想:将两个时间序列按照最优路径进行对齐,即找到一条“弯曲”的路径,让这条路径上的所有点之间的距离之和最小。

在进行动态时间规整计算时,需要执行以下步骤:
1.定义距离度量:定义两个时间序列之间的距离度量方式,例如欧氏距离、曼哈顿距离、余弦相似度等。
2.创建距离矩阵:通过计算两个时间序列中任意两个点之间的距离,创建一个距离矩阵。
3.计算累积距离矩阵:从距离矩阵的左上角开始,沿着最短路径逐步计算出到达每个点时的最小距离,形成一个累积距离矩阵。
4.选择最佳路径:从累积距离矩阵的右下角开始,回溯出一条最优路径,即最小距离的路径。

按照距离最近的原则,构建两个序列元素之间的对应的关系,评估两个长度不同的序列的相似性。

主要用于解决同一句话但说话速度不同的识别问题。

要求:(1)单项对应,不能回头

(2)一一对应,不能有空

(3)对应之后,距离最近

上面这个正确的,第一个一定对应第一个,最后一个一定对应最后一个。

下面两个错误,不能回头,不能有空。

二、实现过程

对于A和B两个长度为10的一维序列,通过使用欧式距离dis计算累计距离矩阵。

三、代码实现

# from dtw import dtw

import numpy as np

def dis_abs(x, y):
    return abs(x-y)[0]
    
def estimate_twf(A,B,dis_func=dis_abs):
    
    N_A = len(A)
    N_B = len(B)
    
    D = np.zeros([N_A,N_B])
    D[0,0] = dis_func(A[0],B[0])
    
    # 左边一列
    for i in range(1,N_A):
        D[i,0] = D[i-1,0]+dis_func(A[i],B[0])
    # 下边一行
    for j in range(1,N_B):
        D[0,j] = D[0,j-1]+dis_func(A[0],B[j])
    # 中间部分
    for i in range(1,N_A):
        for j in range(1,N_B):        
            D[i,j] = dis_func(A[i],B[j])+min(D[i-1,j],D[i,j-1],D[i-1,j-1])
            
    # 路径回溯
    i = N_A-1
    j = N_B-1
    count =0
    d = np.zeros(max(N_A,N_B)*3)
    path = []
    while True:
        if i>0 and j>0:
            path.append((i,j))
            m = min(D[i-1, j],D[i, j-1],D[i-1,j-1])
            if m == D[i-1,j-1]:
                d[count] = D[i,j] - D[i-1,j-1]
                i = i-1
                j = j-1
                count = count+1
                
            elif m == D[i,j-1]:
                d[count] = D[i,j] - D[i,j-1]
                j = j-1
                count = count+1
    
            elif m == D[i-1, j]:
                d[count] = D[i,j] - D[i-1,j]
                i = i-1
                count = count+1
                
        elif i == 0 and j == 0:
            path.append((i,j))
            d[count] = D[i,j]
            count = count+1
            break
        
        elif i == 0:
            path.append((i,j))
            d[count] = D[i,j] - D[i,j-1]
            j = j-1
            count = count+1
        
        elif j == 0:
            path.append((i,j))
            d[count] = D[i,j] - D[i-1,j]
            i = i-1
            count = count+1
            
    mean = np.sum(d) / count
    return mean, path[::-1],D
    
    
if __name__=="__main__":
    a = np.array([1,3,4,9,8,2,1,5,7,3])
    b = np.array([1,6,2,3,0,9,4,1,6,3])
    a = a[:,np.newaxis]
    b = b[:,np.newaxis]
    dis,path,D = estimate_twf(a,b,dis_func=dis_abs)
    print(dis)
    print(path)
    print(D)
    
    # print(a.shape)
    # print(dtw.__file__)
    # print("-------")
    # f = dis_abs
    # out = f(a,b)
    # print("-------")
    # print(out)

参考文献

DTW(动态时间规整)算法原理与应用_哔哩哔哩_bilibili

Java实现DTW时间序列算法可以使用第三方库,例如tslearn和jmotif。以下是使用tslearn库实现DTW算法的步骤: 1. 安装tslearn库:可以使用pip install tslearn命令进行安装。 2. 导入tslearn库:在Java代码中导入tslearn库。 3. 加载时间序列数据:将需要比较的时间序列数据加载到Java程序中。 4. 计算DTW距离:使用tslearn库中的dtw方法计算两个时间序列之间的DTW距离。 5. 输出结果:将计算结果输出到控制台或者文件中。 以下是一个简单的Java程序示例,演示如何使用tslearn库实现DTW算法: ``` import java.util.ArrayList; import java.util.List; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; import org.apache.commons.math3.ml.distance.DistanceMeasure; import org.apache.commons.math3.ml.distance.EuclideanDistance; import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.stat.descriptive.rank.Percentile; import org.apache.commons.math3.stat.descriptive.rank.Median; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math3.stat.descriptive.rank.Max; import org.apache.commons.math3.stat.descriptive.rank.Min; import org.apache.commons.math3.stat.descriptive.rank.Sum; import org.apache.commons.math3.stat.descriptive.rank.Product; import org.apache.commons.math3.stat.descriptive.rank.Variance; import org.apache.commons.math3.stat.descriptive.rank.Skewness; import org.apache.commons.math3.stat.descriptive.rank.Kurtosis; import org.apache.commons.math3.stat.descriptive.moment.GeometricMean; import org.apache.commons.math3.stat.descriptive.moment.HarmonicMean; import org.apache.commons.math3.stat.descriptive.moment.SecondMoment; import org.apache.commons.math3.stat.descriptive.moment.ThirdMoment; import org.apache.commons.math3.stat.descriptive.moment.FourthMoment; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovariance; import org.apache.commons.math3.stat.descriptive.moment.VectorialMean;import org.apache.commons.math3.stat.descriptive.moment.VectorialProduct; import org.apache.commons.math3.stat.descriptive.moment.VectorialSummaryStatistics; import org.apache.commons.math3.stat.descriptive.moment.VectorialVariance; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewness; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosis; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrix; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVector; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMoment; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMoment; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMoment; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMoment; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值