Back to models
Classifier

KNeighborsTimeSeriesClassifierTslearn

K-nearest neighbors Time Series Classifier, from tslearn.

Quickstart

python
from sktime.classification.distance_based import KNeighborsTimeSeriesClassifierTslearn

estimator = KNeighborsTimeSeriesClassifierTslearn(n_neighbors=5, weights='uniform', metric='dtw', metric_params=None, n_jobs=None, verbose=0)

Parameters(6)

n_neighborsint (default: 5)
Number of nearest neighbors to be considered for the decision.
weightsstr or callable, optional (default: ‘uniform’)

Weight function used in prediction. Possible values:

  • ‘uniform’: uniform weights. All points in each neighborhood are weighted equally.

  • ‘distance’: weight points by the inverse of their distance. in this case, closer neighbors of a query point will have a greater influence than neighbors which are further away.

  • [callable]: a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.

metric{‘dtw’, ‘softdtw’, ‘ctw’, ‘euclidean’, ‘sqeuclidean’, ‘cityblock’, ‘sax’}

default: ‘dtw’. Metric to be used at the core of the nearest neighbor procedure. When 'sax' is provided as a metric, the data is expected to be normalized such that each time series has zero mean and unit variance. 'euclidean', 'sqeuclidean', 'cityblock' are described in scipy.spatial.distance doc.

metric_paramsdict or None (default: None)

Dictionary of metric parameters. For metrics that accept parallelization of the cross-distance matrix computations, n_jobs and verbose keys passed in metric_params are overridden by the n_jobs and verbose arguments. For 'sax' metric, these are hyper-parameters to be passed at the creation of the SymbolicAggregateApproximation object.

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.

verboseint, optional (default=0)
The verbosity level: if non zero, progress messages are printed. Above 50, the output is sent to stdout. The frequency of the messages increases with the verbosity level. If it more than 10, all iterations are reported.

Examples

>>> from sktime.classification.distance_based import (
... KNeighborsTimeSeriesClassifierTslearn
... )
>>> from sktime.datasets import load_unit_test
>>> X_train, y_train = load_unit_test (split = "train")
>>> X_test, y_test = load_unit_test (split = "test")
>>> clf = KNeighborsTimeSeriesClassifierTslearn (
... n_neighbors = 5,
... weights = "uniform",
... metric = "dtw",
... metric_params = None,
... n_jobs = None,
... verbose = 0,
... )
>>> clf. fit (X_train, y_train) KNeighborsTimeSeriesClassifierTslearn(
... )
>>> y_pred = clf. predict (X_test)