tslearn
The machine learning toolkit for time series analysis in Python
What is tslearn?
The machine learning toolkit for time series analysis in Python
Overview of tslearn
What it is
tslearn is a Python machine learning toolkit specifically designed for time series analysis. It provides a comprehensive set of tools for preprocessing, clustering, classification, regression, and other common tasks on time series data. The library is built to be compatible with scikit-learn, allowing users to integrate it seamlessly into existing machine learning workflows. tslearn supports variable-length time series and offers utilities for data conversion, scaling, and transformation.
Key features
- Clustering algorithms: Includes TimeSeriesKMeans, KShape, and KernelKmeans for grouping time series data.
- Classification and regression: Provides K-nearest neighbors classifier/regressor, TimeSeriesSVC, TimeSeriesSVR, and LearningShapelets.
- Distance metrics: Supports Dynamic Time Warping (DTW), Global Alignment Kernel, and Matrix Profile for measuring similarity between time series.
- Preprocessing tools: Offers scaling (e.g., TimeSeriesScalerMinMax), piecewise transformation, and resampling to prepare data for analysis.
- Barycenter calculation: Enables computing the average of a set of time series using various methods.
- Data handling: Includes utilities for loading UCR datasets, generating synthetic time series, and converting data from other popular Python toolkits.
Use cases
tslearn is suitable for any application involving time series data, such as:
- Anomaly detection: Identifying unusual patterns in sensor readings or financial data.
- Clustering customer behavior: Grouping users based on their activity sequences over time.
- Classification of signals: Recognizing different types of gestures, speech patterns, or biomedical signals.
- Forecasting and regression: Predicting future values of a time series based on historical data.
- Shapelet discovery: Finding discriminative subsequences that characterize classes of time series.
Getting started
Install tslearn via pip or conda:python -m pip install tslearnconda install -c conda-forge tslearnAfter installation, format your data as a 3D numpy array (n_ts, max_sz, d). For example:python3>>> from tslearn.utils import to_time_series_dataset>>> my_first_time_series = [1, 3, 4, 2]>>> my_second_time_series = [1, 2, 4, 2]>>> my_third_time_series = [1, 2, 4, 2, 2]>>> X = to_time_series_dataset([my_first_time_series, my_second_time_series, my_third_time_series])>>> y = [0, 1, 1]Then preprocess and train a model, e.g., a K-nearest neighbors classifier:python3>>> from tslearn.preprocessing import TimeSeriesScalerMinMax>>> X_scaled = TimeSeriesScalerMinMax().fit_transform(X)>>> from tslearn.neighbors import KNeighborsTimeSeriesClassifier>>> knn = KNeighborsTimeSeriesClassifier(n_neighbors=1)>>> knn.fit(X_scaled, y)>>> print(knn.predict(X_scaled))[0 1 1]
License and pricing
tslearn is open-source software released under the BSD-2-Clause license. It is free to use, modify, and distribute.
Based on the project's public README and repository metadata.