old_contents: # -*- coding: utf-8 -*-
  """
  Nearest neighbors
  =================
  
  This example illustrates the use of nearest neighbor methods for database search and classification tasks.
  """
  
  # Author: Romain Tavenard
  # License: BSD 3 clause
  
  from __future__ import print_function
  import numpy
  from sklearn.metrics import accuracy_score
  
  from tslearn.generators import random_walk_blobs
  from tslearn.preprocessing import TimeSeriesScalerMinMax
  from tslearn.neighbors import KNeighborsTimeSeriesClassifier, KNeighborsTimeSeries
  
  numpy.random.seed(0)
  n_ts_per_blob, sz, d, n_blobs = 20, 100, 1, 2
  color_list = ["r", "g"]
  
  # Prepare data
  X, y = random_walk_blobs(n_ts_per_blob=n_ts_per_blob, sz=sz, d=d, n_blobs=n_blobs)
  scaler = TimeSeriesScalerMinMax(min=0., max=1.)  # Rescale time series
  X_scaled = scaler.fit_transform(X)
  
  indices_shuffle = numpy.random.permutation(n_ts_per_blob * n_blobs)
  X_shuffle = X_scaled[indices_shuffle]
  y_shuffle = y[indices_shuffle]
  
  X_train = X_shuffle[:n_ts_per_blob * n_blobs // 2]
  X_test = X_shuffle[n_ts_per_blob * n_blobs // 2:]
  y_train = y_shuffle[:n_ts_per_blob * n_blobs // 2]
  y_test = y_shuffle[n_ts_per_blob * n_blobs // 2:]
  
  # Nearest neighbor search
  knn = KNeighborsTimeSeries(n_neighbors=3, metric="dtw")
  knn.fit(X_train, y_train)
  dists, ind = knn.kneighbors(X_test)
  print("1. Nearest neighbour search")
  print("Computed nearest neighbor indices (wrt DTW)\n", ind)
  print("First nearest neighbor class:", y_test[ind[:, 0]])
  
  # Nearest neighbor classification
  knn_clf = KNeighborsTimeSeriesClassifier(n_neighbors=3, metric="dtw")
  knn_clf.fit(X_train, y_train)
  predicted_labels = knn_clf.predict(X_test)
  print("\n2. Nearest neighbour classification using DTW")
  print("Correct classification rate:", accuracy_score(y_test, predicted_labels))
  
  # Nearest neighbor classification with a different metric (Euclidean distance)
  knn_clf = KNeighborsTimeSeriesClassifier(n_neighbors=3, metric="euclidean")
  knn_clf.fit(X_train, y_train)
  predicted_labels = knn_clf.predict(X_test)
  print("\n3. Nearest neighbour classification using L2")
  print("Correct classification rate:", accuracy_score(y_test, predicted_labels))
  
new_contents: # -*- coding: utf-8 -*-
  """
  Nearest neighbors
  =================
  
  This example illustrates the use of nearest neighbor methods for database search and classification tasks.
  """
  
  # Author: Romain Tavenard
  # License: BSD 3 clause
  
  from __future__ import print_function
  import numpy
  from sklearn.metrics import accuracy_score
  from sklearn.pipeline import Pipeline
  
  from tslearn.generators import random_walk_blobs
  from tslearn.preprocessing import TimeSeriesScalerMinMax
  from tslearn.neighbors import KNeighborsTimeSeriesClassifier, KNeighborsTimeSeries
  from tslearn.piecewise import SymbolicAggregateApproximation
  
  numpy.random.seed(0)
  n_ts_per_blob, sz, d, n_blobs = 20, 100, 1, 2
  color_list = ["r", "g"]
  
  # Prepare data
  X, y = random_walk_blobs(n_ts_per_blob=n_ts_per_blob, sz=sz, d=d, n_blobs=n_blobs)
  scaler = TimeSeriesScalerMinMax(min=0., max=1.)  # Rescale time series
  X_scaled = scaler.fit_transform(X)
  
  indices_shuffle = numpy.random.permutation(n_ts_per_blob * n_blobs)
  X_shuffle = X_scaled[indices_shuffle]
  y_shuffle = y[indices_shuffle]
  
  X_train = X_shuffle[:n_ts_per_blob * n_blobs // 2]
  X_test = X_shuffle[n_ts_per_blob * n_blobs // 2:]
  y_train = y_shuffle[:n_ts_per_blob * n_blobs // 2]
  y_test = y_shuffle[n_ts_per_blob * n_blobs // 2:]
  
  # Nearest neighbor search
  knn = KNeighborsTimeSeries(n_neighbors=3, metric="dtw")
  knn.fit(X_train, y_train)
  dists, ind = knn.kneighbors(X_test)
  print("1. Nearest neighbour search")
  print("Computed nearest neighbor indices (wrt DTW)\n", ind)
  print("First nearest neighbor class:", y_test[ind[:, 0]])
  
  # Nearest neighbor classification
  knn_clf = KNeighborsTimeSeriesClassifier(n_neighbors=3, metric="dtw")
  knn_clf.fit(X_train, y_train)
  predicted_labels = knn_clf.predict(X_test)
  print("\n2. Nearest neighbour classification using DTW")
  print("Correct classification rate:", accuracy_score(y_test, predicted_labels))
  
  # Nearest neighbor classification with a different metric (Euclidean distance)
  knn_clf = KNeighborsTimeSeriesClassifier(n_neighbors=3, metric="euclidean")
  knn_clf.fit(X_train, y_train)
  predicted_labels = knn_clf.predict(X_test)
  print("\n3. Nearest neighbour classification using L2")
  print("Correct classification rate:", accuracy_score(y_test, predicted_labels))
  
  # Nearest neigbhor classification  based on SAX representation
  sax_trans = SymbolicAggregateApproximation(n_segments=10, alphabet_size_avg=5)
  knn_clf = KNeighborsTimeSeriesClassifier(n_neighbors=3, metric="euclidean")
  pipeline_model = Pipeline(steps=[('sax', sax_trans), ('knn', knn_clf)])
  pipeline_model.fit(X_train, y_train)
  predicted_labels = pipeline_model.predict(X_test)
  print("\n3. Nearest neighbour classification using SAX+L2")
  print("Correct classification rate:", accuracy_score(y_test, predicted_labels))
  
subject: Add Pipeline (SAX+L2) example