tslearn.metrics.dtw_subsequence_path(subseq, longseq)[source]

Compute sub-sequence Dynamic Time Warping (DTW) similarity measure between a (possibly multidimensional) query and a long time series and return both the path and the similarity.

DTW is computed as the Euclidean distance between aligned time series, i.e., if \(\pi\) is the alignment path:

\[DTW(X, Y) = \sqrt{\sum_{(i, j) \in \pi} \|X_{i} - Y_{j}\|^2}\]

Compared to traditional DTW, here, border constraints on admissible paths \(\pi\) are relaxed such that \(\pi_0 = (0, ?)\) and \(\pi_L = (N-1, ?)\) where \(L\) is the length of the considered path and \(N\) is the length of the subsequence time series.

It is not required that both time series share the same size, but they must be the same dimension. This implementation finds the best matching starting and ending positions for subseq inside longseq.

subseq : array, shape = (sz1, d)

A query time series.

longseq : array, shape = (sz2, d)

A reference (supposed to be longer than subseq) time series.

list of integer pairs

Matching path represented as a list of index pairs. In each pair, the first index corresponds to subseq and the second one corresponds to longseq.


Similarity score

See also

Get the similarity score for DTW
Calculate the required cost matrix
Calculate a matching path manually


>>> path, dist = dtw_subsequence_path([2., 3.], [1., 2., 2., 3., 4.])
>>> path
[(0, 2), (1, 3)]
>>> dist