old_contents: # -*- coding: utf-8 -*-
  """
  Learning Shapelets
  ==================
  
  This example illustrates the use of the "Learning Shapelets" method for a time series classification task.
  
  More information on the method can be found at: http://fs.ismll.de/publicspace/LearningShapelets/.
  """
  
  # Author: Romain Tavenard
  # License: BSD 3 clause
  
  import numpy
  from sklearn.metrics import accuracy_score
  from keras.optimizers import Adagrad
  import matplotlib.pyplot as plt
  
  from tslearn.datasets import CachedDatasets
  from tslearn.preprocessing import TimeSeriesScalerMinMax
  from tslearn.shapelets import ShapeletModel, grabocka_params_to_shapelet_size_dict
  from tslearn.utils import ts_size
  
  numpy.random.seed(0)
  X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
  X_train = TimeSeriesScalerMinMax().fit_transform(X_train)
  X_test = TimeSeriesScalerMinMax().fit_transform(X_test)
  
  # Set the number of shapelets per size as done in the original paper
  shapelet_sizes = grabocka_params_to_shapelet_size_dict(n_ts=X_train.shape[0],
                                                         ts_sz=X_train.shape[1],
                                                         n_classes=len(set(y_train)),
                                                         l=0.1,
                                                         r=2)
  
  # Define the model using parameters provided by the authors (except that we use fewer iterations here)
  shp_clf = ShapeletModel(n_shapelets_per_size=shapelet_sizes,
                          optimizer=Adagrad(lr=.1),
                          weight_regularizer=.01,
                          max_iter=200,
                          verbose_level=0)
  shp_clf.fit(X_train, y_train)
  predicted_labels = shp_clf.predict(X_test)
  print("Correct classification rate:", accuracy_score(y_test, predicted_labels))
  print(shp_clf.model.loss)
  
  plt.figure()
  for i, sz in enumerate(shapelet_sizes.keys()):
      plt.subplot(len(shapelet_sizes), 1, i + 1)
      plt.title("%d shapelets of size %d" % (shapelet_sizes[sz], sz))
      for shp in shp_clf.shapelets_:
          if ts_size(shp) == sz:
              plt.plot(shp.ravel())
      plt.xlim([0, max(shapelet_sizes.keys()) - 1])
  
  plt.tight_layout()
  plt.show()
  
new_contents: # -*- coding: utf-8 -*-
  """
  Learning Shapelets
  ==================
  
  This example illustrates the use of the "Learning Shapelets" method for a time series classification task.
  
  More information on the method can be found at: http://fs.ismll.de/publicspace/LearningShapelets/.
  """
  
  # Author: Romain Tavenard
  # License: BSD 3 clause
  
  import numpy
  from sklearn.metrics import accuracy_score
  from keras.optimizers import Adagrad
  import matplotlib.pyplot as plt
  
  from tslearn.datasets import CachedDatasets
  from tslearn.preprocessing import TimeSeriesScalerMinMax
  from tslearn.shapelets import ShapeletModel, grabocka_params_to_shapelet_size_dict
  from tslearn.utils import ts_size
  
  numpy.random.seed(0)
  X_train, y_train, X_test, y_test = CachedDatasets().load_dataset("Trace")
  X_train = TimeSeriesScalerMinMax().fit_transform(X_train)
  X_test = TimeSeriesScalerMinMax().fit_transform(X_test)
  
  # Set the number of shapelets per size as done in the original paper
  shapelet_sizes = grabocka_params_to_shapelet_size_dict(n_ts=X_train.shape[0],
                                                         ts_sz=X_train.shape[1],
                                                         n_classes=len(set(y_train)),
                                                         l=0.1,
                                                         r=2)
  
  # Define the model using parameters provided by the authors (except that we use fewer iterations here)
  shp_clf = ShapeletModel(n_shapelets_per_size=shapelet_sizes,
                          optimizer=Adagrad(lr=.1),
                          weight_regularizer=.01,
                          max_iter=200,
                          verbose_level=0)
  shp_clf.fit(X_train, y_train)
  predicted_labels = shp_clf.predict(X_test)
  print("Correct classification rate:", accuracy_score(y_test, predicted_labels))
  
  plt.figure()
  for i, sz in enumerate(shapelet_sizes.keys()):
      plt.subplot(len(shapelet_sizes), 1, i + 1)
      plt.title("%d shapelets of size %d" % (shapelet_sizes[sz], sz))
      for shp in shp_clf.shapelets_:
          if ts_size(shp) == sz:
              plt.plot(shp.ravel())
      plt.xlim([0, max(shapelet_sizes.keys()) - 1])
  
  plt.tight_layout()
  plt.show()
  
subject: Fix unrelated stuff in shapelets example