fuzzyclust.relational.dtw_distance_matrix#
- fuzzyclust.relational.dtw_distance_matrix(X, Y=None, window=None)[source]#
Dynamic time warping distances between all pairs of (equal-length) series.
Dynamic time warping aligns two series by a monotone, continuous path through the matrix of local costs \(c(t,s)=\norm{x_t-y_s}^2\) so that the summed cost along the path is minimal (Sakoe and Chiba, 1978),
\[\mathrm{DTW}(\mathbf{x},\mathbf{y})^2=\min_{\text{paths}}\sum_{(t,s)\in\text{path}} \norm{x_t-y_s}^2 ,\]with the symmetric step pattern \((t-1,s),(t,s-1),(t-1,s-1)\) and the Sakoe–Chiba band \(|t-s|\le w\) as the global constraint. The square root of the minimal cost is returned, so that for
window=0the result is the Euclidean distance of the two series. The recursion runs over all pairs simultaneously (vectorized over the pairs), so the cost is \(O(N^2\,T\,w)\) withn_series\(N\), length \(T\) and band width \(w\).- Parameters:
- Xarray_like
ofshape(n_series,length)or(n_series,length,n_channels) The series (multichannel series use the squared Euclidean norm of the channel difference as local cost).
- Yarray_like
ofthesamekind,optional Second set of series (same length); the result is then rectangular.
- window
int,floatin(0, 1)orNone, default=None Half width \(w\) of the Sakoe–Chiba band in samples, or as a fraction of the length;
Noneimposes no constraint,0gives the Euclidean distance.
- Xarray_like
- Returns:
References
[1]H. Sakoe, S. Chiba, “Dynamic programming algorithm optimization for spoken word recognition”, IEEE Transactions on Acoustics, Speech, and Signal Processing 26(1), 43–49, 1978.
Examples
>>> import numpy as np >>> from fuzzyclust.relational import dtw_distance_matrix >>> t = np.linspace(0, 1, 30) >>> X = np.vstack([np.sin(2 * np.pi * t), np.sin(2 * np.pi * (t - 0.1)), np.cos(2 * np.pi * t)]) >>> D = dtw_distance_matrix(X, window=5) >>> bool(D[0, 1] < D[0, 2]), D.shape (True, (3, 3))
Examples using fuzzyclust.relational.dtw_distance_matrix#
Fuzzy c-medoids on dynamic-time-warping dissimilarities of time series