tslearn is a Python package providing a comprehensive machine learning toolkit specifically designed for the analysis of time-series data. It offers various algorithms for clustering, classification, and regression on time series, building upon the `scikit-learn`, `numpy`, and `scipy` libraries. The current version is 0.8.1, and the library is under active development and maintenance.
pip install tslearnVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to prepare time-series data for `tslearn` using `to_time_series_dataset` and then perform clustering with `TimeSeriesKMeans`. The data is formatted into a 3D NumPy array, which is the standard input format for `tslearn` estimators.
Upgrade your Python environment to version 3.10 or higher (e.g., `python -m venv .venv` followed by `source .venv/bin/activate` and `pip install tslearn`).
Always use `tslearn.utils.to_time_series_dataset` to convert your list of 1D or 2D time series into the expected 3D format. For example: `X = to_time_series_dataset([my_series_1, my_series_2])`.
If using the `shapelets` module, ensure `keras` (Keras3+) and your desired backend (`tensorflow`, `torch`, or `jax`) are installed: `pip install tslearn[shapelets] keras tensorflow` (or `torch`, `jax`). You might also need to set `os.environ['KERAS_BACKEND'] = 'tensorflow'` (or 'torch', 'jax') before importing `keras` or `tslearn.shapelets`.
Wrap your single 2D time series in a list (e.g., `[my_single_time_series]`) or, preferably, use `tslearn.utils.to_time_series_dataset` to ensure correct formatting for both single and multiple time series: `X_formatted = to_time_series_dataset([my_single_time_series])` or `X_formatted = to_time_series_dataset(list_of_time_series)`.
Install Keras3+: `pip install keras` (or use `pip install tslearn[shapelets]`). If you intend to use a specific backend, install it too (e.g., `pip install tensorflow`).
Refer to the `tslearn` documentation or API reference to find the correct submodule for the desired function or class. For example, `TimeSeriesKMeans` is in `tslearn.clustering`, not directly under `tslearn`. Correct: `from tslearn.clustering import TimeSeriesKMeans`.