logo

tslearn

预测

wangzf / 2022-05-04


目录

tslearn is a Python package that provides machine learning tools for the analysis of time series.

tslearn 安装

conda

requirements

installation

$ conda install -c conda-forge tslearn

PyPI

requirements

installation

$ python -m pip install tslearn

Latest GitHub-hosted Version

requirements

$ python -m pip install cython

installation

$ python -m pip install https://github.com/tslearn-team/tslearn/archive/main.zip

Requirements

tslearn 快速开始

时间序列格式

tslearn.utils.to_time_series

from tslearn.utils import to_time_series

my_first_time_series = [1, 3, 4, 2]
formatted_time_series = to_time_series(my_first_time_series)
print(formatted_time_series.shape)  # (4, 1)

tslearn.utils.to_time_series_dataset

from tslearn.utils import to_time_series_dataset

my_first_time_series = [1, 3, 4, 2]
my_second_time_series = [1, 2, 4, 2]
formatted_dataset = to_time_series_dataset([
    my_first_time_series, 
    my_second_time_series
])
print(formatted_dataset.shape)  ## (2, 4, 1)

my_third_time_series = [1, 2 4, 2, 2]
formatted_dataset = to_time_series_dataset([
    my_first_time_series,
    my_second_time_series,
    my_third_time_series,
])
print(formatted_dataset.shape)  # (3, 5, 1)

载入标准时间序列数据集

tslearn datasets

from tslearn.datasets import UCR_UEA_datasets

X_train, y_train, X_test, y_test = UCR_UEA_datasets().load_dataset("TwoPatterns")
print(X_train.shape)
print(y_train.shape)

text 格式数据

text 格式数据示例:

1.0 0.0 2.5|3.0 2.0 1.0
1.0 2.0|4.333 2.12

text 格式数据读写:

from tslearn.utils import save_time_series_txt, load_time_series_txt

time_series_dataset = load_time_series_txt("path/to/your/file.txt")
save_time_series_txt("path/to/another/file.txt", dataset_to_be_saved)

使用示例

from tslearn.clustering import TimeSeriesKMeans

km = TimeSeriesKMeans(n_clusters = 3, metric = "dtw")
km = fit(X_train)

tslearn 任务

Clustering

Classification

Regression

tslearn 和其他包

cesium

from tslearn.utils import from_cesium_dataset, to_cesium_dataset
from cesium.data_management import Timeseries

from_cesium_dataset([
    TimeSeries(m = [1, 2])],
    TimeSeries(m = [1, 4, 3])
)

len(to_cesium_dataset(
    [[[1],
      [2],
      [None]],
     [[1],
      [4],
      [3]]]  
))

参考