Machine Learning/Dynamic Time Warping

From Federal Burro of Information
Revision as of 14:30, 24 July 2024 by David (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

https://en.wikipedia.org/wiki/Dynamic_time_warping

sample script:

import numpy as np
import mlpy

# Example sequences
sequence1 = np.array([1, 3, 4, 9, 8, 2, 1])
sequence2 = np.array([1, 2, 4, 7, 8, 2, 1])
sequence3 = np.array([1, 2, 4, 7, 8, 2, 5])

# Compute DTW distances between sequences
dist12, cost12, path12 = mlpy.dtw_std(sequence1, sequence2, dist_only=False)
dist13, cost13, path13 = mlpy.dtw_std(sequence1, sequence3, dist_only=False)

print("DTW distance between sequence1 and sequence2:", dist12)
print("DTW distance between sequence1 and sequence3:", dist13)

# Predict which sequence is closer based on DTW distance
if dist12 < dist13:
    print("Sequence 2 is predicted to be closer to sequence 1.")
else:
    print("Sequence 3 is predicted to be closer to sequence 1.")