tslearn.metrics.soft_dtw_alignment

tslearn.metrics.soft_dtw_alignment(ts1, ts2, gamma=1.0)[source]

Compute Soft-DTW metric between two time series and return both the similarity measure and the alignment matrix.

Soft-DTW was originally presented in [1] and is discussed in more details in our user-guide page on DTW and its variants.

Soft-DTW is computed as:

\[\text{soft-DTW}_{\gamma}(X, Y) = \min_{\pi}{}^\gamma \sum_{(i, j) \in \pi} \|X_i, Y_j\|^2\]

where \(\min^\gamma\) is the soft-min operator of parameter \(\gamma\).

In the limit case \(\gamma = 0\), \(\min^\gamma\) reduces to a hard-min operator and soft-DTW is defined as the square of the DTW similarity measure.

Parameters:
ts1

A time series

ts2

Another time series

gamma : float (default 1.)

Gamma paraneter for Soft-DTW

Returns:
numpy.ndarray

Soft-alignment matrix

float

Similarity

See also

soft_dtw
Returns soft-DTW score alone

References

[1]M. Cuturi, M. Blondel “Soft-DTW: a Differentiable Loss Function for Time-Series,” ICML 2017.

Examples

>>> a, dist = soft_dtw_alignment([1, 2, 2, 3],
...                              [1., 2., 3., 4.],
...                              gamma=1.)  # doctest: +ELLIPSIS
>>> dist
-0.89...
>>> a  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
array([[1.00...e+00, 1.88...e-01, 2.83...e-04, 4.19...e-11],
       [3.40...e-01, 8.17...e-01, 8.87...e-02, 3.94...e-05],
       [5.05...e-02, 7.09...e-01, 5.30...e-01, 6.98...e-03],
       [1.37...e-04, 1.31...e-01, 7.30...e-01, 1.00...e+00]])

Examples using tslearn.metrics.soft_dtw_alignment