pairwise_distance#
- pairwise_distance(x: ndarray, y: Optional[ndarray] = None, metric: Optional[Union[str, Callable[[ndarray, ndarray, Any], float]]] = None, **kwargs: Any) ndarray [source]#
Compute the pairwise distance matrix between two time series.
- Parameters:
- X: np.ndarray, of shape (n_instances, n_channels, n_timepoints) or
(n_instances, n_timepoints)
A collection of time series instances.
- y: np.ndarray, of shape (m_instances, m_channels, m_timepoints) or
(m_instances, m_timepoints) or (m_timepoints,), default=None
A collection of time series instances.
- metric: str or Callable
The distance metric to use. If a string is given, the value must be one of the following strings: ‘euclidean’, ‘squared’, ‘dtw’, ‘ddtw’, ‘wdtw’, ‘wddtw’, ‘lcss’, ‘edr’, ‘erp’, ‘msm’ If a callable is given, the value must be a function that accepts two numpy arrays and **kwargs returns a float.
- kwargs: Any
Extra arguments for metric. Refer to each metric documentation for a list of possible arguments.
- Returns:
- np.ndarray (n_instances, n_instances)
pairwise matrix between the instances of X.
- Raises:
- ValueError
If X is not 2D or 3D array when only passing X. If X and y are not 1D, 2D or 3D arrays when passing both X and y. If metric is not a valid string or callable.
Examples
>>> import numpy as np >>> from aeon.distances import pairwise_distance >>> # Distance between each time series in a collection of time series >>> X = np.array([[[1, 2, 3]],[[4, 5, 6]], [[7, 8, 9]]]) >>> pairwise_distance(X, metric='dtw') array([[ 0., 26., 108.], [ 26., 0., 26.], [108., 26., 0.]])
>>> # Distance between two collections of time series >>> X = np.array([[[1, 2, 3]],[[4, 5, 6]], [[7, 8, 9]]]) >>> y = np.array([[[11, 12, 13]],[[14, 15, 16]], [[17, 18, 19]]]) >>> pairwise_distance(X, y, metric='dtw') array([[300., 507., 768.], [147., 300., 507.], [ 48., 147., 300.]])
>>> X = np.array([[[1, 2, 3]],[[4, 5, 6]], [[7, 8, 9]]]) >>> y_univariate = np.array([[11, 12, 13],[14, 15, 16], [17, 18, 19]]) >>> pairwise_distance(X, y_univariate, metric='dtw') array([[300.], [147.], [ 48.]])