I am trying to create a pairwise DTW (Dynamic Time Warping) matrix in python. I have the below code already, but it is incorrect somehow. My current output is a matrix full of infinity, which is incorrect. I cannot figure out what I am doing incorrectly.
def calc_pairwise_dtw_cost(x, y):
cost_matrix = np.zeros((len(y), len(x)))
dtw_cost = None
for i in range(0, len(y)):
for j in range(0, len(x)):
cost_matrix[i, j] = float('inf')
for i in range(1, len(y)):
for j in range(1, len(x)):
dtw_cost = cost_matrix[-1,-1]
cost_matrix[i, j] = dtw_cost + min(
cost_matrix[i-1, j], # insertion
cost_matrix[i, j-1], # deletion
cost_matrix[i-1, j-1]) # match
return cost_matrix
Current output is:
array([[inf, inf, inf, ..., inf, inf, inf],
[inf, inf, inf, ..., inf, inf, inf],
[inf, inf, inf, ..., inf, inf, inf],
...,
[inf, inf, inf, ..., inf, inf, inf],
[inf, inf, inf, ..., inf, inf, inf],
[inf, inf, inf, ..., inf, inf, inf]])
In your implementation there are several errors including:
Dynamic Time Warping: Explanation and Code Implementation provides an implementation of DTW that follows the Wikipedia pseudo-code closely as follows.
Code