One Nearest-Neighbor Classifier

Time series classification tasks usually rely on Nearest Neighbors Search, which classifies query series by computing their distances to all series in the training set and label query series using the class label of their nearest training sample. Here we provide a

One Nearest Neighbor Classifier

class tsdistance.OneNN.OneNN(metric, metric_param=None, lb_metric=False, lb_param=None)

Class for implementing One Nearest Neighbors Search with Lower Bounding Measures

Parameters:
  • metric (function) – distance measure to compute similarity

  • metric_param – parameters of distance measure (if applicable) ,default = None.

  • lb_metric (function) – lower bounding distance measure to compute similarity (only applicable if metric is one of the Elastic Measures)

  • lb_param (tuple) – parameters of distance measure (if applicable) ,default = None.

fit(X, Xlabel)

This function fits the 1NN classifier from the training dataset.

Parameters:
  • X (np.array) – training dataset

  • Xlabel (np.array) – target values (labels)

Returns:

Fitted 1NN classifier

predict(Y)

Predic class lables for given dataset

Parameters:

X (np.array) – test samples

Returns:

Predicted class label for each data sample

Example

Input

# "Coffee" is one of the UCR Archive datasets.

>>> model = Bounded1NN(metric = 'lcss')
>>> model.fit(Coffee_train_X, Coffee_train_y)
>>> predicted_label = model.predict(Coffee_test_X)
>>> print('predicted_label: ', predicted_label)

Output:

lb_predict:  [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

sklearn.neighbors.KNeighborsClassifier in the Scikit-Learn library is a popular tool for Nearest Neighbor Search, Bounded1NN gives the same accuracy and is about 5 times faster when using lcss for classification tasks.