pyts.metrics.dtw¶
-
pyts.metrics.dtw(x, y, dist='square', method='classic', options=None, return_cost=False, return_accumulated=False, return_path=False)[source]¶ Dynamic Time Warping (DTW) distance between two samples.
Parameters: - x : array-like, shape = (n_timestamps,)
First array.
- y : array-like, shape = (n_timestamps,)
Second array
- dist : ‘square’, ‘absolute’ or callable (default = ‘square’)
Distance used. If ‘square’, the squared difference is used. If ‘absolute’, the absolute difference is used. If callable, it must be a function with a numba.njit() decorator that takes as input two numbers (two arguments) and returns a number.
- method : str (default = ‘classic’)
Method used. Should be one of
- ‘classic’: Classic DTW
- ‘sakoechiba’: DTW with Sakoe-Chiba band constraint
- ‘itakura’: DTW with Itakura parallelogram constraint
- ‘multiscale’: MultiscaleDTW
- ‘fast’: FastDTW
- options : None or dict (default = None)
Dictionary of method options
- ‘classic’: None
- ‘sakoechiba’: window_size (int or float)
- ‘itakura’: max_slope (float)
- ‘multiscale’: resolution (int) and radius (int)
- ‘fast’: radius (int)
- return_cost : bool (default = False)
If True, the cost matrix is returned.
- return_accumulated : bool (default = False)
If True, the accumulated cost matrix is returned.
- return_path : bool (default = False)
If True, the optimal path is returned.
Returns: - dist : float
The DTW distance between the two arrays.
- cost_mat : ndarray, shape = (n_timestamps, n_timestamps)
Cost matrix. Only returned if
return_cost=True.- acc_cost_mat : ndarray, shape = (n_timestamps, n_timestamps)
Accumulated cost matrix. Only returned if
return_accumulated=True.- path : ndarray, shape = (2, path_length)
The optimal path along the cost matrix. The first row consists of the indices of the optimal path for x while the second row consists of the indices of the optimal path for y. Only returned if
return_path=True.
Examples
>>> from pyts.metrics import dtw >>> x = [0, 1, 1] >>> y = [2, 0, 1] >>> dtw(x, y, method='sakoechiba', options={'window_size': 2}) 2.0