tslearn.clustering.KShape

class tslearn.clustering.KShape(n_clusters=3, max_iter=100, tol=1e-06, n_init=1, verbose=True, random_state=None)[source]

KShape clustering for time series.

KShape was originally presented in [1].

Parameters:
  • n_clusters (int (default: 3)) – Number of clusters to form.
  • max_iter (int (default: 100)) – Maximum number of iterations of the k-Shape algorithm.
  • tol (float (default: 1e-6)) – Inertia variation threshold. If at some point, inertia varies less than this threshold between two consecutive iterations, the model is considered to have converged and the algorithm stops.
  • n_init (int (default: 1)) – Number of time the k-Shape algorithm will be run with different centroid seeds. The final results will be the best output of n_init consecutive runs in terms of inertia.
  • verbose (bool (default: True)) – Whether or not to print information about the inertia while learning the model.
  • random_state (integer or numpy.RandomState, optional) – Generator used to initialize the centers. If an integer is given, it fixes the seed. Defaults to the global numpy random number generator.
cluster_centers_

Centroids

Type:numpy.ndarray of shape (sz, d)
labels_

Labels of each point

Type:numpy.ndarray of integers with shape (n_ts, )
inertia_

Sum of distances of samples to their closest cluster center.

Type:float

Note

This method requires a dataset of equal-sized time series.

Examples

>>> from tslearn.generators import random_walks
>>> X = random_walks(n_ts=50, sz=32, d=1)
>>> X = TimeSeriesScalerMeanVariance(mu=0., std=1.).fit_transform(X)
>>> ks = KShape(n_clusters=3, n_init=1, verbose=False, random_state=0).fit(X)
>>> ks.cluster_centers_.shape
(3, 32, 1)
>>> dists = ks._cross_dists(X)
>>> numpy.alltrue(ks.labels_ == dists.argmin(axis=1))
True
>>> numpy.alltrue(ks.labels_ == ks.predict(X))
True
>>> numpy.alltrue(ks.predict(X) == KShape(n_clusters=3, n_init=1, verbose=False, random_state=0).fit_predict(X))
True
>>> KShape(n_clusters=101, verbose=False, random_state=0).fit(X).X_fit_ is None
True

References

[1]J. Paparrizos & L. Gravano. k-Shape: Efficient and Accurate Clustering of Time Series. SIGMOD 2015. pp. 1855-1870.

Methods

__init__([n_clusters, max_iter, tol, …]) Initialize self.
fit(X[, y]) Compute k-Shape clustering.
fit_predict(X[, y]) Fit k-Shape clustering using X and then predict the closest cluster each time series in X belongs to.
get_params([deep]) Get parameters for this estimator.
predict(X) Predict the closest cluster each time series in X belongs to.
set_params(**params) Set the parameters of this estimator.

Examples using tslearn.clustering.KShape