Skip to content

TimeSeriesKMeansTslearn

TimeSeriesKMeansTslearn

class TimeSeriesKMeansTslearn(n_clusters=3, max_iter=50, tol=1e-06, n_init=1, metric='euclidean', max_iter_barycenter=100, metric_params=None, n_jobs=None, dtw_inertia=False, verbose=0, random_state=None, init='random')[source]

K-means clustering for time-series data, from tslearn.

Direct interface to tslearn.clustering.TimeSeriesKMeans.

Parameters:
n_clustersint (default: 3)

Number of clusters to form.

max_iterint (default: 50)

Maximum number of iterations of the k-means algorithm for a single run.

tolfloat (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_initint (default: 1)

Number of time the k-means 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.

metric{“euclidean”, “dtw”, “softdtw”} (default: “euclidean”)

Metric to be used for both cluster assignment and barycenter computation. If “dtw”, DBA is used for barycenter computation.

max_iter_barycenterint (default: 100)

Number of iterations for the barycenter computation process. Only used if metric="dtw" or metric="softdtw".

metric_paramsdict or None (default: None)

Parameter values for the chosen metric. For metrics that accept parallelization of the cross-distance matrix computations, n_jobs key passed in metric_params is overridden by the n_jobs argument.

n_jobsint or None, optional (default=None)

The number of jobs to run in parallel for cross-distance matrix computations. Ignored if the cross-distance matrix cannot be computed using parallelization. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See scikit-learns’ Glossary for more details.

dtw_inertia: bool (default: False)

Whether to compute DTW inertia even if DTW is not the chosen metric.

verboseint (default: 0)

If nonzero, print information about the inertia while learning the model and joblib progress messages are printed.

random_stateinteger 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.

init{‘k-means++’, ‘random’ or an ndarray} (default: ‘random’)

Method for initialization: ‘k-means++’ : use k-means++ heuristic. See scikit-learn’s k_init_ for more. ‘random’: choose k observations (rows) at random from data for the initial centroids. If an ndarray is passed, it should be of shape (n_clusters, ts_size, d) and gives the initial centers.

Attributes:
labels_numpy.ndarray

Labels of each point.

cluster_centers_numpy.ndarray of shape (n_clusters, sz, d)

Cluster centers. sz is the size of the time series used at fit time if the init method is ‘k-means++’ or ‘random’, and the size of the longest initial centroid if those are provided as a numpy array through init parameter.

inertia_float

Sum of distances of samples to their closest cluster center.

n_iter_int

The number of iterations performed during fit.

Notes

If metric is set to "euclidean", the algorithm expects a dataset of equal-sized time series.

Methods

check_is_fitted([method_name])

Check if the estimator has been fitted.

clone()

Obtain a clone of the object with same hyper-parameters and config.

clone_tags(estimator[, tag_names])

Clone tags from another object as dynamic override.

create_test_instance([parameter_set])

Construct an instance of the class, using first test parameter set.

create_test_instances_and_names([parameter_set])

Create list of all test instances and a list of names for them.

fit(X[, y])

Fit time series clusterer to training data.

fit_predict(X[, y])

Compute cluster centers and predict cluster index for each time series.

get_class_tag(tag_name[, tag_value_default])

Get class tag value from class, with tag level inheritance from parents.

get_class_tags()

Get class tags from class, with tag level inheritance from parent classes.

get_config()

Get config flags for self.

get_fitted_params([deep])

Get fitted parameters.

get_param_defaults()

Get object's parameter defaults.

get_param_names([sort])

Get object's parameter names.

get_params([deep])

Get a dict of parameters values for this object.

get_tag(tag_name[, tag_value_default, ...])

Get tag value from instance, with tag level inheritance and overrides.

get_tags()

Get tags from instance, with tag level inheritance and overrides.

get_test_params([parameter_set])

Return testing parameter settings for the estimator.

is_composite()

Check if the object is composed of other BaseObjects.

load_from_path(serial)

Load object from file location.

load_from_serial(serial)

Load object from serialized memory container.

predict(X[, y])

Predict the closest cluster each sample in X belongs to.

predict_proba(X)

Predicts labels probabilities for sequences in X.

reset()

Reset the object to a clean post-init state.

save([path, serialization_format])

Save serialized self to bytes-like object or to (.zip) file.

score(X[, y])

Score the quality of the clusterer.

set_config(**config_dict)

Set config flags to given values.

set_params(**params)

Set the parameters of this object.

set_random_state([random_state, deep, ...])

Set random_state pseudo-random seed parameters for self.

set_tags(**tag_dict)

Set instance level tag overrides to given values.