#!/usr/bin/env python # coding: utf-8 # # The ROCKET transform # # ## Overview # # ROCKET [1] transforms time series using random convolutional kernels (random length, weights, bias, dilation, and padding). ROCKET computes two features from the resulting feature maps: the max, and the proportion of positive values (or ppv). The transformed features are used to train a linear classifier. # # [1] Dempster A, Petitjean F, Webb GI (2019) ROCKET: Exceptionally fast and accurate time series classification using random convolutional kernels. [arXiv:1910.13051](https://arxiv.org/abs/1910.13051) # # *** # # ## Contents # # 1. Imports # 2. Univariate Time Series # 3. Multivariate Time Series # 4. Pipeline Example # # *** # # ## 1 Imports # # Import example data, ROCKET, and a classifier (`RidgeClassifierCV` from scikit-learn), as well as NumPy and `make_pipeline` from scikit-learn. # # **Note**: ROCKET compiles (via Numba) on import, which may take a few seconds. # In[33]: # !pip install --upgrade numba # In[34]: import numpy as np from sklearn.linear_model import RidgeClassifierCV from sklearn.pipeline import make_pipeline from aeon.datasets import load_basic_motions # multivariate dataset from aeon.datasets import load_gunpoint # univariate dataset from aeon.transformations.collection.convolution_based import Rocket # ## 2 Univariate Time Series # # We can transform the data using ROCKET and separately fit a classifier, or we can use ROCKET together with a classifier in a pipeline (section 4, below). # # ### 2.1 Load the Training Data # For more details on the data set, see the [univariate time series classification # notebook](https://github.com/aeon-toolkit/aeon/tree/main/examples/classification/classification.ipynb). # In[35]: X_train, y_train = load_gunpoint(split="train") X_train = X_train[:5, :, :] y_train = y_train[:5] print(X_train.shape) # ### 2.2 Initialise ROCKET and Transform the Training Data # In[36]: rocket = Rocket(n_kernels=100) # by default, ROCKET uses 10,000 kernels rocket.fit(X_train) X_train_transform = rocket.transform(X_train) print(X_train_transform.shape) # ### 2.3 Fit a Classifier # We recommend using `RidgeClassifierCV` from scikit-learn for smaller datasets (fewer than approx. 20K training examples), and using logistic regression trained using stochastic gradient descent for larger datasets. # In[37]: classifier = RidgeClassifierCV(alphas=np.logspace(-3, 3, 10)) classifier.fit(X_train_transform, y_train) # ### 2.4 Load and Transform the Test Data # In[38]: X_test, y_test = load_gunpoint(split="test") X_test_transform = rocket.transform(X_test) # ### 2.5 Classify the Test Data # In[39]: classifier.score(X_test_transform, y_test) # *** # # ## 3 Multivariate Time Series # # We can use ROCKET in exactly the same way for multivariate time series. # # ### 3.1 Load the Training Data # In[40]: X_train, y_train = load_basic_motions(split="train") # ### 3.2 Initialise ROCKET and Transform the Training Data # In[41]: rocket = Rocket(n_kernels=100) # by default, ROCKET uses 10,000 kernels rocket.fit(X_train) X_train_transform = rocket.transform(X_train) X_train_transform.shape # ### 3.3 Fit a Classifier # In[42]: classifier = RidgeClassifierCV(alphas=np.logspace(-3, 3, 10)) classifier.fit(X_train_transform, y_train) # ### 3.4 Load and Transform the Test Data # In[43]: X_test, y_test = load_basic_motions(split="test") X_test_transform = rocket.transform(X_test) # ### 3.5 Classify the Test Data # In[44]: classifier.score(X_test_transform, y_test) # *** # # ## 4 Pipeline Example # # We can use ROCKET together with `RidgeClassifierCV` (or another classifier) in a pipeline. We can then use the pipeline like a self-contained classifier, with a single call to `fit`, and without having to separately transform the data, etc. # # ### 4.1 Initialise the Pipeline # In[45]: rocket_pipeline = make_pipeline( Rocket(), RidgeClassifierCV(alphas=np.logspace(-3, 3, 10)) ) # ### 4.2 Load and Fit the Training Data # In[46]: # it is necessary to pass y_train to the pipeline # y_train is not used for the transform, but it is used by the classifier rocket_pipeline.fit(X_train, y_train) # ### 4.3 Load and Classify the Test Data # In[47]: rocket_pipeline.score(X_test, y_test) # In[ ]: