{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Symbolic Aggregate approXimation\n\n\nThis example shows how you can discretize a time series (i.e. transform a\nsequence of real numbers into a sequence of symbols) using\n:class:`pyts.approximation.SymbolicAggregateApproximation`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.lines as mlines\nimport matplotlib.pyplot as plt\nfrom scipy.stats import norm\nfrom pyts.approximation import SymbolicAggregateApproximation\n\n# Parameters\nn_samples, n_timestamps = 100, 24\n\n# Toy dataset\nrng = np.random.RandomState(41)\nX = rng.randn(n_samples, n_timestamps)\n\n# SAX transformation\nn_bins = 3\nsax = SymbolicAggregateApproximation(n_bins=n_bins, strategy='normal')\nX_sax = sax.fit_transform(X)\n\n# Compute gaussian bins\nbins = norm.ppf(np.linspace(0, 1, n_bins + 1)[1:-1])\n\n# Show the results for the first time series\nbottom_bool = np.r_[True, X_sax[0, 1:] > X_sax[0, :-1]]\n\nplt.figure(figsize=(12, 8))\nplt.plot(X[0], 'o--', label='Original')\nfor x, y, s, bottom in zip(range(n_timestamps), X[0], X_sax[0], bottom_bool):\n    va = 'bottom' if bottom else 'top'\n    plt.text(x, y, s, ha='center', va=va, fontsize=24, color='#ff7f0e')\nplt.hlines(bins, 0, n_timestamps, color='g', linestyles='--', linewidth=0.5)\nsax_legend = mlines.Line2D([], [], color='#ff7f0e', marker='*',\n                           label='SAX - {0} bins'.format(n_bins))\nfirst_legend = plt.legend(handles=[sax_legend], fontsize=14, loc=(0.79, 0.87))\nax = plt.gca().add_artist(first_legend)\nplt.legend(loc=(0.835, 0.93), fontsize=14)\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}