def load_tslearn_data(): """ Time series data with variable length """ X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace") X_train = X_train[y_train < 4] # Keep first 3 classes np.random.shuffle(X_train) X_train = TimeSeriesScalerMeanVariance().fit_transform(X_train[:50]) # Keep only 50 time series X_train = TimeSeriesResampler(sz=40).fit_transform(X_train) # Make time series shorter X_train = X_train.reshape(50,-1) return X_train
import matplotlib #matplotlib.use('Agg') import matplotlib.pyplot as plt from tslearn.clustering import GlobalAlignmentKernelKMeans from tslearn.metrics import sigma_gak, cdist_gak from tslearn.datasets import CachedDatasets from tslearn.preprocessing import TimeSeriesScalerMeanVariance import pandas as pd import numpy as np from Sloth import cluster seed = 0 numpy.random.seed(seed) X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace") X_train = X_train[y_train < 4] # Keep first 3 classes numpy.random.shuffle(X_train) #X_train = TimeSeriesScalerMeanVariance().fit_transform(X_train[:50]) # Keep only 50 time series X_train = X_train[:50] sz = X_train.shape[1] X_train = X_train.reshape((X_train.shape[0], X_train.shape[1])) #Sloth = Sloth() eps = 20 min_samples = 2 LOAD = False # Flag for loading similarity matrix from file if it has been computed before
This example uses the KShape clustering method that is based on cross-correlation to cluster time series. """ # Author: Romain Tavenard # License: BSD 3 clause import numpy import matplotlib.pyplot as plt from tslearn.clustering import KShape from tslearn.datasets import CachedDatasets from tslearn.preprocessing import TimeSeriesScalerMeanVariance seed = 0 numpy.random.seed(seed) X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace") X_train = X_train[y_train < 4] # Keep first 3 classes numpy.random.shuffle(X_train) X_train = TimeSeriesScalerMeanVariance().fit_transform( X_train[:50]) # Keep only 50 time series sz = X_train.shape[1] # Euclidean k-means ks = KShape(n_clusters=3, verbose=True, random_state=seed) y_pred = ks.fit_predict(X_train) plt.figure() for yi in range(3): plt.subplot(3, 1, 1 + yi) for xx in X_train[y_pred == yi]: plt.plot(xx.ravel(), "k-", alpha=.2)
# Author: Romain Tavenard, Felix Divo # License: BSD 3 clause import numpy import matplotlib.pyplot as plt from tslearn.barycenters import \ euclidean_barycenter, \ dtw_barycenter_averaging, \ dtw_barycenter_averaging_subgradient, \ softdtw_barycenter from tslearn.datasets import CachedDatasets # fetch the example data set numpy.random.seed(0) X_train, y_train, _, _ = CachedDatasets().load_dataset("Trace") X = X_train[y_train == 2] length_of_sequence = X.shape[1] def plot_helper(barycenter): # plot all points of the data set for series in X: plt.plot(series.ravel(), "k-", alpha=.2) # plot the given barycenter of them plt.plot(barycenter.ravel(), "r-", linewidth=2) # plot the four variants with the same number of iterations and a tolerance of # 1e-3 where applicable ax1 = plt.subplot(4, 1, 1)