{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Sample generator used in SMOTE-like samplers\n\nThis example illustrates how a new sample is generated taking into account the\nneighbourhood of this sample. A new sample is generated by selecting the\nrandomly 2 samples of the same class and interpolating a point between these\nsamples.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Authors: Guillaume Lemaitre <g.lemaitre58@gmail.com>\n# License: MIT"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\n\nsns.set_context(\"poster\")\n\nrng = np.random.RandomState(18)\n\nf, ax = plt.subplots(figsize=(8, 8))\n\n# generate some data points\ny = np.array([3.65284, 3.52623, 3.51468, 3.22199, 3.21])\nz = np.array([0.43, 0.45, 0.6, 0.4, 0.211])\ny_2 = np.array([3.3, 3.6])\nz_2 = np.array([0.58, 0.34])\n\n# plot the majority and minority samples\nax.scatter(z, y, label=\"Minority class\", s=100)\nax.scatter(z_2, y_2, label=\"Majority class\", s=100)\n\nidx = rng.randint(len(y), size=2)\nannotation = [r\"$x_i$\", r\"$x_{zi}$\"]\n\nfor a, i in zip(annotation, idx):\n    ax.annotate(a, (z[i], y[i]), xytext=tuple([z[i] + 0.01, y[i] + 0.005]), fontsize=15)\n\n# draw the circle in which the new sample will generated\nradius = np.sqrt((z[idx[0]] - z[idx[1]]) ** 2 + (y[idx[0]] - y[idx[1]]) ** 2)\ncircle = plt.Circle((z[idx[0]], y[idx[0]]), radius=radius, alpha=0.2)\nax.add_artist(circle)\n\n# plot the line on which the sample will be generated\nax.plot(z[idx], y[idx], \"--\", alpha=0.5)\n\n# create and plot the new sample\nstep = rng.uniform()\ny_gen = y[idx[0]] + step * (y[idx[1]] - y[idx[0]])\nz_gen = z[idx[0]] + step * (z[idx[1]] - z[idx[0]])\n\nax.scatter(z_gen, y_gen, s=100)\nax.annotate(\n    r\"$x_{new}$\",\n    (z_gen, y_gen),\n    xytext=tuple([z_gen + 0.01, y_gen + 0.005]),\n    fontsize=15,\n)\n\n# make the plot nicer with legend and label\nsns.despine(ax=ax, offset=10)\nax.set_xlim([0.2, 0.7])\nax.set_ylim([3.2, 3.7])\nplt.xlabel(r\"$X_1$\")\nplt.ylabel(r\"$X_2$\")\nplt.legend()\nplt.tight_layout()\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.10.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}