pairwise_distance¶
- pairwise_distance(x: ndarray, y: ndarray | None = None, method: str | Callable[[ndarray, ndarray, Any], float] | None = None, symmetric: bool = True, **kwargs: Unpack[DistanceKwargs]) ndarray [source]¶
Compute the pairwise distance matrix between two time series.
- Parameters:
- Xnp.ndarray
- A collection of time series instances of shape
(n_cases, n_timepoints)
or
(n_cases, n_channels, n_timepoints)
.
- A collection of time series instances of shape
- ynp.ndarray or None, default=None
A single series or a collection of time series of shape
(m_timepoints,)
or(m_cases, m_timepoints)
or(m_cases, m_channels, m_timepoints)
- methodstr or Callable
The distance to use. A list of valid distance can be found in the documentation for
aeon.distances.get_distance_function
or by calling the functionaeon.distances.get_distance_function_names
.- symmetricbool, default=True
If True and a function is provided as the “method” paramter, then it will compute a symmetric distance matrix where d(x, y) = d(y, x). Only the lower triangle is calculated, and the upper triangle is ignored. If False and a function is provided as the “method” parameter, then it will compute an asymmetric distance matrix, and the entire matrix (including both upper and lower triangles) is returned.
- kwargsAny
Extra arguments for distance. Refer to each distance documentation for a list of possible arguments.
- Returns:
- np.ndarray (n_cases, n_cases)
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 distance 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, method='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, method='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]) >>> pairwise_distance(X, y_univariate, method='dtw') array([[300.], [147.], [ 48.]])