{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# IntervalRandomFrog - Basic example\n\nAn Interval Random Frog example showing the spectral intervals optimized by\nthe IRF method.\n\nThe example uses a synthetic dataset with 50 standard normally distributed features.\nThe target values only depend on four features: #21 and #24 and #46 and #47.\nIf the Interval Random Frog method is tasked with selecting two intervals of width 5, it identifies\ntwo intervals containing the above enumerated features.\n\nNote, that we use 1000 iterations to decrease the runtime in this case.\nA small number of iterations likely results in unstable selections if no fixed random seed is given.\nWe recommend to use the default 10000 iterations when using the Random Frog method.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom auswahl import IntervalRandomFrog\n\nnp.random.seed(1337)\nX = np.random.randn(100, 50)\ny = 5 * X[:, 21] - 2 * X[:, 24] + 3 * X[:, 46] + X[:, 47]\n\nn_iterations = 1000\nirf = IntervalRandomFrog(n_intervals_to_select=2,\n                         interval_width=5,\n                         n_iterations=n_iterations,\n                         n_jobs=5,\n                         random_state=7331)\nirf.fit(X, y)\n\nidx = np.arange(len(irf.frequencies_))\nplt.plot(idx, irf.frequencies_ / n_iterations, marker='.', zorder=3)\nplt.hlines(y=irf.frequencies_ / n_iterations,\n           xmin=idx,\n           xmax=idx + irf.interval_width - 1,\n           alpha=0.5,\n           zorder=1)\n\ninterval_starts = np.argwhere(np.diff(irf.get_support().astype(int)) > 0) + 1\nplt.hlines(y=irf.frequencies_[interval_starts] / n_iterations,\n           xmin=interval_starts,\n           xmax=interval_starts + irf.interval_width - 1,\n           colors='C01',\n           zorder=2)\n\nplt.ylim([0, 1.01])\nplt.grid(axis='y')\nplt.xticks(range(0, 55, 5))\nplt.xlabel('Feature')\nplt.ylabel('Relative Frequency')\nplt.legend(['Frequency', 'Interval', 'Selected Intervals'])\n\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.8.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}