tslearn.metrics.dtw_subsequence_path(subseq, longseq, be=None)[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.

subseqarray-like, shape=(sz1, d) or (sz1,)

A query time series. If shape is (sz1,), the time series is assumed to be univariate.

longseqarray-like, shape=(sz2, d) or (sz2,)

A reference (supposed to be longer than subseq) time series. If shape is (sz2,), the time series is assumed to be univariate.

beBackend object or string or None

Backend. If be is an instance of the class NumPyBackend or the string “numpy”, the NumPy backend is used. If be is an instance of the class PyTorchBackend or the string “pytorch”, the PyTorch backend is used. If be is None, the backend is determined by the input arrays. See our dedicated user-guide page for more information.

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