binder

Early time series classification with aeon#

Early time series classification (eTSC) is the problem of classifying a time series after as few measurements as possible with the highest possible accuracy. The most critical issue of any eTSC method is to decide when enough data of a time series has been seen to take a decision: Waiting for more data points usually makes the classification problem easier but delays the time in which a classification is made; in contrast, earlier classification has to cope with less input data, often leading to inferior accuracy.

This notebook gives a quick guide to get you started with running eTSC algorithms in aeon.

Data sets and problem types#

The UCR/UEA time series classification archive contains a large number of example TSC problems that have been used thousands of times in the literature to assess TSC algorithms. Read the data loading documentation and notebooks for details on the aeon data formats and loading data for aeon.

[ ]:
# Imports used in this notebook
import numpy as np

from aeon.classification.early_classification._teaser import TEASER
from aeon.classification.interval_based import TimeSeriesForestClassifier
from aeon.datasets import load_arrow_head
[2]:
# Load default train/test splits from aeon/datasets/data
arrow_train_X, arrow_train_y = load_arrow_head(split="train")
arrow_test_X, arrow_test_y = load_arrow_head(split="test")

arrow_test_X.shape
[2]:
(175, 1, 251)

Building the TEASER classifier#

TEASER [1] is a two-tier model using a base classifier to make predictions and a decision making estimator to decide whether these predictions are safe. As a first tier, TEASER requires a TSC algorithm, such as WEASEL, which produces class probabilities as output. As a second tier an anomaly detector is required, such as a one-class SVM.

[3]:
teaser = TEASER(
    random_state=0,
    classification_points=[25, 50, 75, 100, 125, 150, 175, 200, 251],
    estimator=TimeSeriesForestClassifier(n_estimators=10, random_state=0),
)
teaser.fit(arrow_train_X, arrow_train_y)
[3]:
TEASER(classification_points=[25, 50, 75, 100, 125, 150, 175, 200, 251],
       estimator=TimeSeriesForestClassifier(n_estimators=10, random_state=0),
       random_state=0)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

Determine the accuracy and earliness on the test data#

Commonly accuracy is used to determine the correctness of the predictions, while earliness is used to determine how much of the series is required on average to obtain said accuracy. I.e. for the below values, using just 43% of the full test data, we were able to get an accuracy of 69%.

[10]:
hm, acc, earl = teaser.score(arrow_test_X, arrow_test_y)
print("Earliness on Test Data %2.2f" % earl)
print("Accuracy on Test Data %2.2f" % acc)
print("Harmonic Mean on Test Data %2.2f" % hm)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[10], line 1
----> 1 hm, acc, earl = teaser.score(arrow_test_X, arrow_test_y)
      2 print("Earliness on Test Data %2.2f" % earl)
      3 print("Accuracy on Test Data %2.2f" % acc)

File C:\Code\aeon\aeon\classification\early_classification\base.py:314, in BaseEarlyClassifier.score(self, X, y)
    311 self.check_is_fitted()
    313 # boilerplate input checks for predict-like methods
--> 314 self._check_X(X)
    315 X = self._convert_X(X)
    317 return self._score(X, y)

File C:\Code\aeon\aeon\classification\early_classification\base.py:631, in BaseEarlyClassifier._check_X(self, X)
    629 def _check_X(self, X):
    630     """To follow."""
--> 631     metadata = _get_metadata(X)
    632     # Check classifier capabilities for X
    633     allow_multivariate = self.get_tag("capability:multivariate")

File C:\Code\aeon\aeon\classification\early_classification\base.py:707, in _get_metadata(X)
    705 metadata = {}
    706 metadata["multivariate"] = not is_univariate(X)
--> 707 metadata["missing_values"] = has_missing(X)
    708 metadata["unequal_length"] = not is_equal_length(X)
    709 metadata["n_cases"] = get_n_cases(X)

File C:\Code\aeon\aeon\utils\validation\collection.py:305, in has_missing(X)
    303 type = get_type(X)
    304 if type == "numpy3D" or type == "numpy2D":
--> 305     return np.any(np.isnan(np.min(X)))
    306 if type == "np-list":
    307     for x in X:

File <__array_function__ internals>:180, in amin(*args, **kwargs)

File C:\Code\aeon\venv\lib\site-packages\numpy\core\fromnumeric.py:2918, in amin(a, axis, out, keepdims, initial, where)
   2802 @array_function_dispatch(_amin_dispatcher)
   2803 def amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
   2804          where=np._NoValue):
   2805     """
   2806     Return the minimum of an array or minimum along an axis.
   2807
   (...)
   2916     6
   2917     """
-> 2918     return _wrapreduction(a, np.minimum, 'min', axis, None, out,
   2919                           keepdims=keepdims, initial=initial, where=where)

File C:\Code\aeon\venv\lib\site-packages\numpy\core\fromnumeric.py:86, in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     83         else:
     84             return reduction(axis=axis, out=out, **passkwargs)
---> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

ValueError: zero-size array to reduction operation minimum which has no identity

Determine the accuracy and earliness on the train data#

[5]:
print("Earliness on Train Data %2.2f" % teaser._train_earliness)
print("Accuracy on Train Data %2.2f" % teaser._train_accuracy)
Earliness on Train Data 0.31
Accuracy on Train Data 0.69

Comparison to Classification on full Test Data#

With the full test data, we would obtain 68% accuracy with the same classifier.

[6]:
accuracy = (
    TimeSeriesForestClassifier(n_estimators=10, random_state=0)
    .fit(arrow_train_X, arrow_train_y)
    .score(arrow_test_X, arrow_test_y)
)
print("Accuracy on the full Test Data %2.2f" % accuracy)
Accuracy on the full Test Data 0.67

Classifying with incomplete time series#

The main draw of eTSC is the capabilility to make classifications with incomplete time series. aeon eTSC algorithms accept inputs with less time points than the full series length, and output two items: The prediction made and whether the algorithm thinks the prediction is safe. Information about the decision such as the time stamp it was made at can be obtained from the state_info attribute.

First test with only 50 datapoints (out of 251)#

[7]:
X = arrow_test_X[:, :, :50]
probas, _ = teaser.predict_proba(X)
idx = (probas >= 0).all(axis=1)
print("First 10 Finished prediction\n", np.argwhere(idx).flatten()[:10])
print("First 10 Probabilities of finished predictions\n", probas[idx][:10])
First 10 Finished prediction
 [ 0  1  4  5  9 11 24 30 32 35]
First 10 Probabilities of finished predictions
 [[0.9 0.  0.1]
 [0.3 0.1 0.6]
 [0.8 0.1 0.1]
 [0.7 0.3 0. ]
 [0.5 0.2 0.3]
 [0.6 0.2 0.2]
 [0.1 0.2 0.7]
 [0.8 0.  0.2]
 [0.3 0.1 0.6]
 [0.9 0.  0.1]]
[8]:
_, acc, _ = teaser.score(X, arrow_test_y)
print("Accuracy with 50 points on Test Data %2.2f" % acc)
Accuracy with 50 points on Test Data 0.57

We may also do predictions in a streaming scenario where more data becomes available from time to time#

The rationale is to keep the state info from the previous predictions in the TEASER object and use it whenever new data is available.

[9]:
test_points = [25, 50, 75, 100, 125, 150, 175, 200, 251]
final_states = np.zeros((arrow_test_X.shape[0], 4), dtype=int)
final_decisions = np.zeros(arrow_test_X.shape[0], dtype=int)
open_idx = np.arange(0, arrow_test_X.shape[0])
teaser.reset_state_info()

for i in test_points:
    probas, decisions = teaser.update_predict_proba(arrow_test_X[:, :, :i])
    final_states[open_idx] = teaser.get_state_info()

    arrow_test_X, open_idx, final_idx = teaser.split_indices_and_filter(
        arrow_test_X, open_idx, decisions
    )
    final_decisions[final_idx] = i

    (
        hm,
        acc,
        earliness,
    ) = teaser.compute_harmonic_mean(final_states, arrow_test_y)

    print("Earliness on length %2i is %2.2f" % (i, earliness))
    print("Accuracy on length %2i is %2.2f" % (i, acc))
    print("Harmonic Mean on length %2i is %2.2f" % (i, hm))

    print("...........")

print("Time Stamp of final decisions", final_decisions)
Earliness on length 25 is 0.10
Accuracy on length 25 is 0.50
Harmonic Mean on length 25 is 0.64
...........
Earliness on length 50 is 0.20
Accuracy on length 50 is 0.57
Harmonic Mean on length 50 is 0.67
...........
Earliness on length 75 is 0.27
Accuracy on length 75 is 0.72
Harmonic Mean on length 75 is 0.72
...........
Earliness on length 100 is 0.32
Accuracy on length 100 is 0.62
Harmonic Mean on length 100 is 0.65
...........
Earliness on length 125 is 0.35
Accuracy on length 125 is 0.69
Harmonic Mean on length 125 is 0.67
...........
Earliness on length 150 is 0.37
Accuracy on length 150 is 0.69
Harmonic Mean on length 150 is 0.66
...........
Earliness on length 175 is 0.39
Accuracy on length 175 is 0.68
Harmonic Mean on length 175 is 0.64
...........
Earliness on length 200 is 0.40
Accuracy on length 200 is 0.69
Harmonic Mean on length 200 is 0.65
...........
Earliness on length 251 is 0.40
Accuracy on length 251 is 0.67
Harmonic Mean on length 251 is 0.63
...........
Time Stamp of final decisions [ 50  50 251  75  50  50 175 200 175  50  75  50  75  75 100 251 100 100
 125  75 100 100  75 100  50 125  75 100  75  75  50  75  50 125 175  50
  50  75  75 125  50  75  75  50 175 100 150 125  75 100  75  75  75  75
  50 100  50 175  75  50 200  50  50  50  75 200  75 125  75 125 150 175
 125  50 150  50  75  75  50 100  75 251 251  75  50 100  50 150 100  50
  75 100 251  50  50  50 200 100  75  50 200 100  50  50  50  50 251 100
  75  75 125  50 125 100 100  50  75 175 175  50  50 100 175 150 100 100
  50 100 100 100 175  50  50 100 100 175 251 125 125 100 100 125 100 125
 100 125  50 175  75 125 100 100 125  50  50 100 125 100 100 100 251 150
  50  75 175 125  50  50 125  75  50 100 175  50 100]

References:#

[1] Schäfer, P., & Leser, U. (2020). TEASER: early and accurate time series classification. Data mining and knowledge discovery, 34(5), 1336-1362


Generated using nbsphinx. The Jupyter notebook can be found here.