pycvi.dist.time_series_metric_with_sklearn

pycvi.dist.time_series_metric_with_sklearn(X, dist_kwargs={}, d=1, T=None)

Allow to use time-series metrics with (some) sklearn models.

Some sklearn models have a "metric" parameter that accepts a callable, see for example sklearn.cluster.AgglomerativeClustering. We can then use a metric specifically designed for time-series such as those defined in aeon, provided that we call the distance function on a reshaped version of the data. Indeed, sklearn only allows data of shape (N, d) (or (N, d*T)) while time-series distances in aeon require data of shape (N, T, d).

Thus, this present function reshapes the data accordingling on the fly such that one can use time series distances with (some) sklearn models.

To be able to do the reshaping, it is important to correctly provide the original N and d values, as if the following happened:

  1. The data X was originally of shape (N, T, d) (Starting point)

  2. X was reshaped to (N, T*d) to match sklearn requirements (typically using X = np.reshape(X, (N, -1))) (To be done by the user before using the sklearn (or sklearn-like) model)

  3. Inside the call of the sklearn-like model, X is reshaped back to (N, T, d) to match aeon requirements (part that is done by this function)

See pycvi.config.default_ts_distance_kwargs() for more information about default distance kwargs used in PyCVI and see pycvi.dist.f_pdist() for more information about distances with time series data in PyCVI.

For an example of this function, see Time-Series metric with Sklearn

Parameters:
  • X (np.ndarray, shape (N, T*d)) – The data to be clustered, reshaped to match sklearn requirements.

  • dist_kwargs (dict, optional) – Additional kwargs for the distance function.

  • d (int, optional) – The number of variables in the time series, by default 1.

  • T (int, optional) – The number of time steps in the time series, by default None.

Returns:

A callable that can be used as a metric in sklearn models.

Return type:

callable