Multi-variate time series classification using a simple CNN#
In this notebook, we use sktime to perform for multi-variate time series classification by deep learning.
[ ]:
import numpy as np
import seaborn as sns
from sklearn.model_selection import GridSearchCV
from sktime.classification.deep_learning.cnn import CNNClassifier
from sktime.datasets import load_basic_motions
sns.set_style("whitegrid")
Load a dataset#
The Basic Motions dataset, from timeseriesclassification.com, has time series in six dimensions.
[ ]:
X_train, y_train = load_basic_motions(split="train", return_X_y=True)
X_test, y_test = load_basic_motions(split="test", return_X_y=True)
print(X_train.shape)
print(X_test.shape)
print(type(X_train.iloc[1, 1]))
X_train.head()
[ ]:
# The class labels
np.unique(y_train)
Train a deep neural network classifier#
Here we choose to use the CNN (convolutional neural network) classifier. Other classifiers provided by sktime-dl include MLP, ResNet, InceptionTime and MCDCNN (Multi Channel Deep Convolutional Neural Network)
[ ]:
network = CNNClassifier(n_epochs=50, verbose=True)
network.fit(X_train, y_train)
network.score(X_test, y_test)
Grid Search#
sktime-dl is compatible with scikit-learn and can use sklearn’s GridSearchCV.
Here we search over two parameters, CNN kernel size and pooling size.
[ ]:
param_grid = {"kernel_size": [7, 9], "avg_pool_size": [3, 5]}
grid = GridSearchCV(network, param_grid=param_grid, cv=3)
grid.fit(X_train, y_train)
print("Best cross-validation accuracy: {:.2f}".format(grid.best_score_))
print("Test set score: {:.2f}".format(grid.score(X_test, y_test)))
print("Best parameters: {}".format(grid.best_params_))
Generated using nbsphinx. The Jupyter notebook can be found here.