Loading

【人工智能】动态时间规整 DTW

动态时间规整 DTW

How DTW (Dynamic Time Warping) algorithm works

def dtw(seq_a: List, seq_b: List):
    match_table = [[0 for _ in range(len(seq_a))] for _ in range(len(seq_b))]
    for i in range(len(seq_b)):
        for j in range(len(seq_a)):
            if i == 0 and j == 0:
                match_table[i][j] = abs(seq_b[i] - seq_a[j])
            elif i == 0:
                match_table[i][j] = abs(seq_b[i] - seq_a[j]) + match_table[i][j-1]
            elif j == 0:
                match_table[i][j] = abs(seq_b[i] - seq_a[j]) + match_table[i-1][j]
            else:
                match_table[i][j] = abs(seq_b[i] - seq_a[j]) + min(match_table[i-1][j], match_table[i][j-1], match_table[i-1][j-1])
    return match_table
posted @ 2023-02-16 00:03  杨谖之  阅读(14)  评论(0编辑  收藏  举报