Ersi Ago
Ersi Ago

Reputation: 35

How to apply/implement Dynamic Time Warping (DTW) or Fast Dynamic Time Warping (FastDTW) in python between 3 or more signals?

In time series analysis, dynamic time warping (DTW) is one of the algorithms for measuring similarity between two temporal sequences, which may vary in speed. Fast DTW is a more faster method. I would like to know how to implement this method not only between 2 signals but 3 or more.

distance, warp_path = fastdtw(series2, series1, dist=euclidean)

Upvotes: 3

Views: 9819

Answers (2)

ah bon
ah bon

Reputation: 10051

I modified the code based on @Julian'. If you want to calculate the dtw of several columns in the data frame df and one target column, you can use the following code:

import fastdtw

def cal_fastdtw(series1, series2):
    return fastdtw.fastdtw(series1, series2)[0]

df.apply(lambda x: cal_fastdtw(df['target'].values, x.values) if x.name != 'target' else None)

Upvotes: 0

Julian
Julian

Reputation: 116

You essentially need to construct a matrix, evaluating the FastDTW algorithm on all possible combinations of the series.

import fastdtw
import scipy.spatial.distance as sd

def my_fastdtw(sales1, sales2):
    return fastdtw.fastdtw(sales1,sales2)[0]

distance_matrix = sd.pdist(sales, my_fastdtw)

You can see this thread for a reference on how to do it, as well as other possibilities: Efficient pairwise DTW calculation using numpy or cython

Upvotes: 4

Related Questions