.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/approximation/plot_sax.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_approximation_plot_sax.py: ================================ Symbolic Aggregate approXimation ================================ Binning continuous data into intervals can be seen as an approximation that reduces noise and captures the trend of a time series. The Symbolic Aggregate approXimation (SAX) algorithm bins continuous time series into intervals, transforming independently each time series (a sequence of floats) into a sequence of symbols, usually letters. This example illustrates the transformation. It is implemented as :class:`pyts.approximation.SymbolicAggregateApproximation`. .. GENERATED FROM PYTHON SOURCE LINES 15-57 .. image-sg:: /auto_examples/approximation/images/sphx_glr_plot_sax_001.png :alt: Symbolic Aggregate approXimation :srcset: /auto_examples/approximation/images/sphx_glr_plot_sax_001.png :class: sphx-glr-single-img .. code-block:: default # Author: Johann Faouzi # License: BSD-3-Clause import numpy as np import matplotlib.lines as mlines import matplotlib.pyplot as plt from scipy.stats import norm from pyts.approximation import SymbolicAggregateApproximation # Parameters n_samples, n_timestamps = 100, 24 # Toy dataset rng = np.random.RandomState(41) X = rng.randn(n_samples, n_timestamps) # SAX transformation n_bins = 3 sax = SymbolicAggregateApproximation(n_bins=n_bins, strategy='normal') X_sax = sax.fit_transform(X) # Compute gaussian bins bins = norm.ppf(np.linspace(0, 1, n_bins + 1)[1:-1]) # Show the results for the first time series bottom_bool = np.r_[True, X_sax[0, 1:] > X_sax[0, :-1]] plt.figure(figsize=(6, 4)) plt.plot(X[0], 'o--', label='Original') for x, y, s, bottom in zip(range(n_timestamps), X[0], X_sax[0], bottom_bool): va = 'bottom' if bottom else 'top' plt.text(x, y, s, ha='center', va=va, fontsize=14, color='#ff7f0e') plt.hlines(bins, 0, n_timestamps, color='g', linestyles='--', linewidth=0.5) sax_legend = mlines.Line2D([], [], color='#ff7f0e', marker='*', label='SAX - {0} bins'.format(n_bins)) first_legend = plt.legend(handles=[sax_legend], fontsize=8, loc=(0.76, 0.86)) ax = plt.gca().add_artist(first_legend) plt.legend(loc=(0.81, 0.93), fontsize=8) plt.xlabel('Time', fontsize=14) plt.title('Symbolic Aggregate approXimation', fontsize=16) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.689 seconds) .. _sphx_glr_download_auto_examples_approximation_plot_sax.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sax.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sax.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_