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
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.
# !pip install --upgrade numba
import numpy as np
from sklearn.linear_model import RidgeClassifierCV
from sklearn.pipeline import make_pipeline
from aeon.datasets import load_arrow_head # univariate dataset
from aeon.datasets import load_basic_motions # multivariate dataset
from aeon.transformations.collection.convolution_based import Rocket
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).
For more details on the data set, see the univariate time series classification notebook.
X_train, y_train = load_arrow_head(split="train")
rocket = Rocket() # by default, ROCKET uses 10,000 kernels
rocket.fit(X_train)
X_train_transform = rocket.transform(X_train)
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.
classifier = RidgeClassifierCV(alphas=np.logspace(-3, 3, 10))
classifier.fit(X_train_transform, y_train)
RidgeClassifierCV(alphas=array([1.00000000e-03, 4.64158883e-03, 2.15443469e-02, 1.00000000e-01, 4.64158883e-01, 2.15443469e+00, 1.00000000e+01, 4.64158883e+01, 2.15443469e+02, 1.00000000e+03]))In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
RidgeClassifierCV(alphas=array([1.00000000e-03, 4.64158883e-03, 2.15443469e-02, 1.00000000e-01, 4.64158883e-01, 2.15443469e+00, 1.00000000e+01, 4.64158883e+01, 2.15443469e+02, 1.00000000e+03]))
X_test, y_test = load_arrow_head(split="test")
X_test_transform = rocket.transform(X_test)
classifier.score(X_test_transform, y_test)
0.7771428571428571
X_train, y_train = load_basic_motions(split="train")
rocket = Rocket()
rocket.fit(X_train)
X_train_transform = rocket.transform(X_train)
classifier = RidgeClassifierCV(alphas=np.logspace(-3, 3, 10))
classifier.fit(X_train_transform, y_train)
RidgeClassifierCV(alphas=array([1.00000000e-03, 4.64158883e-03, 2.15443469e-02, 1.00000000e-01, 4.64158883e-01, 2.15443469e+00, 1.00000000e+01, 4.64158883e+01, 2.15443469e+02, 1.00000000e+03]))In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
RidgeClassifierCV(alphas=array([1.00000000e-03, 4.64158883e-03, 2.15443469e-02, 1.00000000e-01, 4.64158883e-01, 2.15443469e+00, 1.00000000e+01, 4.64158883e+01, 2.15443469e+02, 1.00000000e+03]))
X_test, y_test = load_basic_motions(split="test")
X_test_transform = rocket.transform(X_test)
classifier.score(X_test_transform, y_test)
0.975
rocket_pipeline = make_pipeline(
Rocket(), RidgeClassifierCV(alphas=np.logspace(-3, 3, 10))
)
X_train, y_train = load_arrow_head(split="train")
# 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)
Pipeline(steps=[('rocket', Rocket()), ('ridgeclassifiercv', RidgeClassifierCV(alphas=array([1.00000000e-03, 4.64158883e-03, 2.15443469e-02, 1.00000000e-01, 4.64158883e-01, 2.15443469e+00, 1.00000000e+01, 4.64158883e+01, 2.15443469e+02, 1.00000000e+03])))])In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
Pipeline(steps=[('rocket', Rocket()), ('ridgeclassifiercv', RidgeClassifierCV(alphas=array([1.00000000e-03, 4.64158883e-03, 2.15443469e-02, 1.00000000e-01, 4.64158883e-01, 2.15443469e+00, 1.00000000e+01, 4.64158883e+01, 2.15443469e+02, 1.00000000e+03])))])
Rocket()
RidgeClassifierCV(alphas=array([1.00000000e-03, 4.64158883e-03, 2.15443469e-02, 1.00000000e-01, 4.64158883e-01, 2.15443469e+00, 1.00000000e+01, 4.64158883e+01, 2.15443469e+02, 1.00000000e+03]))
X_test, y_test = load_arrow_head(split="test", return_X_y=True)
rocket_pipeline.score(X_test, y_test)
0.7885714285714286