Back to models
Classifier

KNeighborsTimeSeriesClassifier

K-nearest neighbours Time Series Classifier.

Quickstart

python
from sktime.classification.distance_based import KNeighborsTimeSeriesClassifier

estimator = KNeighborsTimeSeriesClassifier(n_neighbors=1, weights='uniform', algorithm='brute', distance='dtw', distance_params=None, distance_mtype=None, pass_train_distances=False, leaf_size=30, n_jobs=None)

Parameters(9)

n_neighborsint, optional, default = 1
k in “k nearest neighbours”
weights{‘uniform’, ‘distance’} or callable, 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.

algorithmstr, optional. default = ‘brute’

search method for neighbours one of {‘ball_tree’, ‘brute’, ‘brute_incr’}

  • ‘brute’ precomputes the distance matrix and applies sklearn KNeighborsClassifier directly. This algorithm is not memory efficient as it scales with the size of the distance matrix, but may be more runtime efficient.

  • ‘brute_incr’ passes the distance to sklearn KNeighborsClassifier, with algorithm='brute'. This is useful for large datasets, for memory efficiency, as the distance is used incrementally, without precomputation. However, this may be less runtime efficient.

  • ‘ball_tree’ uses a ball tree to find the nearest neighbors, using KNeighborsClassifier from sklearn. May be more runtime and memory efficient on mid-to-large datasets, however, the distance computation may be slower.

distancestr, sktime pairwise transformer, or callable, optional. default =’dtw’

distance measure between time series

  • if str, must be one of the following strings: ‘euclidean’, ‘squared’, ‘dtw’, ‘ddtw’, ‘wdtw’, ‘wddtw’, ‘lcss’, ‘edr’, ‘erp’, ‘msm’, ‘twe’ this will substitute a hard-coded distance metric from sktime.dists_kernels._numba_distances.

  • if sktime pairwise transformer, must implement the pairwise-transformer interface. sktime transformers are available in sktime.dists_kernels, and discoverable via registry.all_estimators by searching for pairwise-transformer type.

  • if non-class callable, parameters can be passed via distance_params Example: knn_dtw = KNeighborsTimeSeriesClassifier(distance=’dtw’, distance_params={‘epsilon’:0.1})

  • if any callable, must be of signature (X: Panel, X2: Panel) -> np.ndarray. The output must be mxn array if X is Panel of m Series, X2 of n Series; if distance_mtype is not set, must be able to take X, X2 which are of pd_multiindex and numpy3D mtype

distance_paramsdict, optional, default = None.
dictionary for distance parameters, in case that distance is a str or callable
distance_mtypestr, or list of str optional. default = None.
mtype that distance expects for X and X2, if a callable only set this if distance is not BasePairwiseTransformerPanel descendant
pass_train_distancesbool, optional, default = False.
Whether distances between training points are computed and passed to sklearn. Passing is superfluous for algorithm=’brute’, but may have impact otherwise.
leaf_sizeint, optional, default=30

Leaf size passed to BallTree or KDTree. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem.

n_jobsint, optional, default=None

The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. Does not affect the fit method.

Examples

>>> from sktime.classification.distance_based import KNeighborsTimeSeriesClassifier
>>> from sktime.datasets import load_unit_test
>>> X_train, y_train = load_unit_test (return_X_y = True, split = "train")
>>> X_test, y_test = load_unit_test (return_X_y = True, split = "test")
>>> classifier = KNeighborsTimeSeriesClassifier (distance = "euclidean")
>>> classifier. fit (X_train, y_train) KNeighborsTimeSeriesClassifier(
... )
>>> y_pred = classifier. predict (X_test)