binder

{ "cells": [ { "cell_type": "markdown", "source": [ "# Transforming time series\n", "\n", "Transforming time series into different data representations is fundamental to time\n", "series machine learning. Transformation can involve extracting features that\n", "characterize the time series, such as mean and variance or changing the series into,\n", "for example, first order differences. We use the term transformer in the\n", "`scikit-learn` sense, not to be confused with deep learning Transformers that employ\n", "an attention mechanism. We call transformers that extract features\n", "`series-to-vector` transformers and those that change the series into a different\n", "representation that is still ordered `series-to-series` transformers.\n", "\n", "We further differentiate between transformers that act on a single series and those\n", "that transform a collection of series. Single series transformers are located in\n", "transformations/series directory and inherit from `BaseSeriesTransformer`. For\n", "example, `AutoCorrelationSeriesTransformer` is a `series-to-series` transformer that\n", "finds the auto correlation function for a single series." ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 23, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.96019465 0.89567531 0.83739477 0.7977347 0.78594315 0.7839188\n", " 0.78459213 0.79221505 0.8278519 0.8827128 ]]\n" ] } ], "source": [ "from aeon.datasets import load_airline\n", "from aeon.transformations.series import AutoCorrelationSeriesTransformer\n", "\n", "series = load_airline()\n", "transformer = AutoCorrelationSeriesTransformer(n_lags=10)\n", "acf = transformer.fit_transform(series)\n", "print(acf)" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "Collection transformers are located in the transformations/collection directory and\n", "inherit from `BaseCollectionTransformer`. For example, `Truncator` truncates all time\n", " series in a collection to the same length." ], "metadata": { "collapsed": false } }, { "cell_type": "code", "source": [ "from aeon.datasets import load_pickup_gesture_wiimoteZ\n", "from aeon.transformations.collection.unequal_length import Truncator\n", "\n", "X, y = load_pickup_gesture_wiimoteZ()\n", "print(\" Unequal length, first case \", X[0].shape, \" tenth case \", X[10].shape)\n", "trunc = Truncator(truncated_length=100)\n", "X2 = trunc.fit_transform(X)\n", "print(\"Truncated collection shape =\", X2.shape)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2025-05-28T13:34:29.282584Z", "start_time": "2025-05-28T13:34:24.749646Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Unequal length, first case (1, 324) tenth case (1, 97)\n", "Truncated collection shape = (100, 1, 29)\n" ] } ], "execution_count": 1 }, { "cell_type": "markdown", "source": [ "`Truncator` is a `series-to-series` transformer\n", " that returns a new collection of time series of the same length. This can then be\n", " used, for example, by a classifier that only works with equal length series:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 25, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data seen by instance of SummaryClassifier has unequal length series, but SummaryClassifier cannot handle unequal length series. \n" ] }, { "data": { "text/plain": "SummaryClassifier()", "text/html": "
SummaryClassifier()
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.
" }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from aeon.classification.feature_based import SummaryClassifier\n", "\n", "summary = SummaryClassifier()\n", "try:\n", " summary.fit(X, y)\n", "except ValueError as e:\n", " print(e)\n", "\n", "summary.fit(X2, y)" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "Some collection transformers are supervised, meaning they fit a transform based on\n", "the class labels. For example, the shapelet transform finds shapelets that are good\n", "at separating classes. This is a `series-to-vector` transformer that produces tabular\n", " output shape `(n_cases, n_shapelets)`.\n" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 26, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1074, 2)\n" ] } ], "source": [ "from aeon.transformations.collection.shapelet_based import RandomShapeletTransform\n", "\n", "st = RandomShapeletTransform(max_shapelets=10, n_shapelet_samples=100)\n", "X2 = st.fit_transform(X, y)\n", "print(X2.shape)" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "`series-to-vector` transformers produce output that is compatible with `scikit-learn`\n", " estimators" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 27, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (1074, 1) + inhomogeneous part.\n" ] }, { "data": { "text/plain": "RandomForestClassifier()", "text/html": "
RandomForestClassifier()
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.
" }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.ensemble import RandomForestClassifier\n", "\n", "rf = RandomForestClassifier()\n", "try:\n", " rf.fit(X, y)\n", "except ValueError as e:\n", " print(e)\n", "rf.fit(X2, y)" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "A list of all the available transformers can be found in the [API](https://www.aeon-toolkit.org/en/latest/api_reference/transformations.html). We currently have\n", "specific notebooks for the following transformers:\n", "\n", "- [preprocessing](preprocessing.ipynb)\n", "- [catch22](catch22.ipynb)\n", "- [channel selection](channel_selection.ipynb)\n", "- [mini rocket](mini_rocket.ipynb)\n", "- [resizing](resizing.ipynb)\n", "- [rocket](rocket.ipynb)\n", "- [sast](sast.ipynb)\n", "- [signature method](signature_method.ipynb)\n", "- [tsfresh](tsfresh.ipynb)\n", "\n" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [], "metadata": { "collapsed": false } } ], "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 }

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