# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Time series classification with Mr-SEQL
#
# Mr-SEQL\[1\] is a univariate time series classifier which train linear classification models (logistic regression) with features extracted from multiple symbolic representations of time series (SAX, SFA). The features are extracted by using SEQL\[2\].
#
# \[1\] <NAME>, <NAME>, <NAME>, <NAME> and <NAME> Interpretable Time Series Classification using Linear Models and Multi-resolution Multi-domain Symbolic Representations in Data Mining and Knowledge Discovery (DAMI), May 2019, https://link.springer.com/article/10.1007/s10618-019-00633-3
#
# \[2\] <NAME>, <NAME>
# “Bounded Coordinate-Descent for Biological Sequence Classification in High Dimensional Predictor Space” (KDD 2011)
#
# In this notebook, we will demonstrate how to use Mr-SEQL for univariate time series classification with the ArrowHead dataset.
#
# ## Imports

from sklearn import metrics
from sklearn.model_selection import train_test_split
from sktime.classification.shapelet_based import MrSEQLClassifier
from sktime.datasets import load_arrow_head
from sktime.datasets import load_basic_motions

# ## Load data
# For more details on the data set, see the [univariate time series classification notebook](https://github.com/alan-turing-institute/sktime/blob/master/examples/02_classification_univariate.ipynb).

X, y = load_arrow_head(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)

# ## Train and Test
#
# Mr-SEQL can be configured to run in different mode with different symbolic representation.
#
# seql_mode can be either 'clf' (SEQL as classifier) or 'fs' (SEQL as feature selection). If 'fs' mode is chosen, a logistic regression classifier will be trained with the features extracted by SEQL.
# 'fs' mode is more accurate in general.
#
# symrep can include either 'sax' or 'sfa' or both. Using both usually produces a better result.
#

# +
# Create mrseql object
ms = MrSEQLClassifier(seql_mode='clf') # use sax by default
# ms = MrSEQLClassifier(seql_mode='fs', symrep=['sfa']) # use sfa representations
# ms = MrSEQLClassifier(seql_mode='fs', symrep=['sax', 'sfa']) # use sax and sfa representations

# fit training data
ms.fit(X_train,y_train)

# prediction
predicted = ms.predict(X_test)

# Classification accuracy
print("Accuracy with mr-seql: %2.3f" % metrics.accuracy_score(y_test, predicted))
# -

# ## Train and Test
# Mr-SEQL also supports multivariate time series. Mr-SEQL extracts features from each dimension of the data independently.

X, y = load_basic_motions(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)

# +
ms = MrSEQLClassifier()

# fit training data
ms.fit(X_train,y_train)

predicted = ms.predict(X_test)

# Classification accuracy
print("Accuracy with mr-seql: %2.3f" % metrics.accuracy_score(y_test, predicted))

StarcoderdataJupyterScriptsDedupFiltered