{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# Early time series classification with aeon\n", "\n", "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.\n", "\n", "This notebook gives a quick guide to get you started with running eTSC algorithms in aeon.\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Data sets and problem types\n", "The UCR/UEA [time series classification archive](https://timeseriesclassification.com/) 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." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "pycharm": { "is_executing": true } }, "outputs": [], "source": [ "# Imports used in this notebook\n", "import numpy as np\n", "\n", "from aeon.classification.early_classification._teaser import TEASER\n", "from aeon.classification.interval_based import TimeSeriesForestClassifier\n", "from aeon.datasets import load_arrow_head" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(175, 1, 251)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load default train/test splits from aeon/datasets/data\n", "arrow_train_X, arrow_train_y = load_arrow_head(split=\"train\")\n", "arrow_test_X, arrow_test_y = load_arrow_head(split=\"test\")\n", "\n", "arrow_test_X.shape" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Building the TEASER classifier\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
TEASER(classification_points=[25, 50, 75, 100, 125, 150, 175, 200, 251],\n",
       "       estimator=TimeSeriesForestClassifier(n_estimators=10, random_state=0),\n",
       "       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.
" ], "text/plain": [ "TEASER(classification_points=[25, 50, 75, 100, 125, 150, 175, 200, 251],\n", " estimator=TimeSeriesForestClassifier(n_estimators=10, random_state=0),\n", " random_state=0)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "teaser = TEASER(\n", " random_state=0,\n", " classification_points=[25, 50, 75, 100, 125, 150, 175, 200, 251],\n", " estimator=TimeSeriesForestClassifier(n_estimators=10, random_state=0),\n", ")\n", "teaser.fit(arrow_train_X, arrow_train_y)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Determine the accuracy and earliness on the test data\n", "\n", "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 34% of the full test data, we were able to get an accuracy of 65%." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Earliness on Test Data 0.34\n", "Accuracy on Test Data 0.65\n", "Harmonic Mean on Test Data 0.65\n" ] } ], "source": [ "hm, acc, earl = teaser.score(arrow_test_X, arrow_test_y)\n", "print(\"Earliness on Test Data %2.2f\" % earl)\n", "print(\"Accuracy on Test Data %2.2f\" % acc)\n", "print(\"Harmonic Mean on Test Data %2.2f\" % hm)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### Determine the accuracy and earliness on the train data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Earliness on Train Data 0.28\n", "Accuracy on Train Data 0.72\n" ] } ], "source": [ "print(\"Earliness on Train Data %2.2f\" % teaser._train_earliness)\n", "print(\"Accuracy on Train Data %2.2f\" % teaser._train_accuracy)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### Comparison to Classification on full Test Data\n", "\n", "With the full test data, we would obtain 67% accuracy with the same classifier." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy on the full Test Data 0.67\n" ] } ], "source": [ "accuracy = (\n", " TimeSeriesForestClassifier(n_estimators=10, random_state=0)\n", " .fit(arrow_train_X, arrow_train_y)\n", " .score(arrow_test_X, arrow_test_y)\n", ")\n", "print(\"Accuracy on the full Test Data %2.2f\" % accuracy)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Classifying with incomplete time series\n", "\n", "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.\n", "\n", "### First test with only 50 datapoints (out of 251)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First 10 Finished prediction\n", " [ 0 4 5 8 9 16 18 21 22 30]\n", "First 10 Probabilities of finished predictions\n", " [[0.8 0. 0.2]\n", " [0.8 0. 0.2]\n", " [0.6 0.1 0.3]\n", " [0.2 0.2 0.6]\n", " [0.6 0.3 0.1]\n", " [0. 0.3 0.7]\n", " [0.3 0.1 0.6]\n", " [0.1 0.3 0.6]\n", " [0.8 0.1 0.1]\n", " [0.8 0. 0.2]]\n" ] } ], "source": [ "X = arrow_test_X[:, :, :50]\n", "probas, _ = teaser.predict_proba(X)\n", "idx = (probas >= 0).all(axis=1)\n", "print(\"First 10 Finished prediction\\n\", np.argwhere(idx).flatten()[:10])\n", "print(\"First 10 Probabilities of finished predictions\\n\", probas[idx][:10])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy with 50 points on Test Data 0.65\n" ] } ], "source": [ "_, acc, _ = teaser.score(X, arrow_test_y)\n", "print(\"Accuracy with 50 points on Test Data %2.2f\" % acc)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### We may also do predictions in a streaming scenario where more data becomes available from time to time\n", "\n", "The rationale is to keep the state info from the previous predictions in the TEASER object and use it whenever new data is available." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Earliness on length 25 is 0.10\n", "Accuracy on length 25 is 0.43\n", "Harmonic Mean on length 25 is 0.59\n", "...........\n", "Earliness on length 50 is 0.20\n", "Accuracy on length 50 is 0.67\n", "Harmonic Mean on length 50 is 0.73\n", "...........\n", "Earliness on length 75 is 0.26\n", "Accuracy on length 75 is 0.62\n", "Harmonic Mean on length 75 is 0.67\n", "...........\n", "Earliness on length 100 is 0.29\n", "Accuracy on length 100 is 0.64\n", "Harmonic Mean on length 100 is 0.67\n", "...........\n", "Earliness on length 125 is 0.30\n", "Accuracy on length 125 is 0.64\n", "Harmonic Mean on length 125 is 0.67\n", "...........\n", "Earliness on length 150 is 0.32\n", "Accuracy on length 150 is 0.64\n", "Harmonic Mean on length 150 is 0.66\n", "...........\n", "Earliness on length 175 is 0.33\n", "Accuracy on length 175 is 0.63\n", "Harmonic Mean on length 175 is 0.65\n", "...........\n", "Earliness on length 200 is 0.34\n", "Accuracy on length 200 is 0.64\n", "Harmonic Mean on length 200 is 0.65\n", "...........\n", "Earliness on length 251 is 0.34\n", "Accuracy on length 251 is 0.66\n", "Harmonic Mean on length 251 is 0.66\n", "...........\n", "Time Stamp of final decisions [ 50 150 75 75 50 50 251 125 50 50 75 75 75 75 75 150 50 75\n", " 50 75 125 50 50 251 75 75 75 75 75 75 50 175 251 50 175 50\n", " 50 75 75 75 75 50 75 251 75 75 50 50 75 175 75 75 125 50\n", " 75 50 50 100 175 75 150 50 75 50 75 75 75 75 50 75 50 75\n", " 150 50 125 50 100 75 50 50 175 75 251 75 50 75 75 175 50 50\n", " 75 50 50 50 75 50 75 100 75 100 50 50 50 50 50 50 50 150\n", " 75 50 50 50 251 100 125 75 125 100 75 50 75 50 50 75 200 50\n", " 50 100 50 50 75 75 50 150 50 75 50 75 200 50 75 75 200 200\n", " 75 50 75 200 75 75 50 200 75 75 75 100 200 75 75 50 100 50\n", " 50 75 50 75 75 75 75 50 75 75 50 100 75]\n" ] } ], "source": [ "test_points = [25, 50, 75, 100, 125, 150, 175, 200, 251]\n", "final_states = np.zeros((arrow_test_X.shape[0], 4), dtype=int)\n", "final_decisions = np.zeros(arrow_test_X.shape[0], dtype=int)\n", "open_idx = np.arange(0, arrow_test_X.shape[0])\n", "teaser.reset_state_info()\n", "\n", "for i in test_points:\n", " probas, decisions = teaser.update_predict_proba(arrow_test_X[:, :, :i])\n", " final_states[open_idx] = teaser.get_state_info()\n", "\n", " arrow_test_X, open_idx, final_idx = teaser.split_indices_and_filter(\n", " arrow_test_X, open_idx, decisions\n", " )\n", " final_decisions[final_idx] = i\n", "\n", " (\n", " hm,\n", " acc,\n", " earliness,\n", " ) = teaser.compute_harmonic_mean(final_states, arrow_test_y)\n", "\n", " print(\"Earliness on length %2i is %2.2f\" % (i, earliness))\n", " print(\"Accuracy on length %2i is %2.2f\" % (i, acc))\n", " print(\"Harmonic Mean on length %2i is %2.2f\" % (i, hm))\n", "\n", " print(\"...........\")\n", "\n", "print(\"Time Stamp of final decisions\", final_decisions)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "\n", "#### References:\n", "\n", "\\[1\\] Schäfer, P., & Leser, U. (2020). TEASER: early and accurate time series classification. Data mining and knowledge discovery, 34(5), 1336-1362" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }