Skip to content

Instantly share code, notes, and snippets.

@MJeremy2017
Created January 31, 2020 04:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MJeremy2017/8ea397091a62cbaba7cd5eeb314e169a to your computer and use it in GitHub Desktop.
Save MJeremy2017/8ea397091a62cbaba7cd5eeb314e169a to your computer and use it in GitHub Desktop.
def dtw(s, t):
n, m = len(s), len(t)
dtw_matrix = np.zeros((n+1, m+1))
for i in range(n+1):
for j in range(m+1):
dtw_matrix[i, j] = np.inf
dtw_matrix[0, 0] = 0
for i in range(1, n+1):
for j in range(1, m+1):
cost = abs(s[i-1] - t[j-1])
# take last min from a square box
last_min = np.min([dtw_matrix[i-1, j], dtw_matrix[i, j-1], dtw_matrix[i-1, j-1]])
dtw_matrix[i, j] = cost + last_min
return dtw_matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment