{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Usage of pipeline embedding samplers\n\nAn example of the :class:`~imbalanced_ensemble.pipeline.Pipeline` object (or\n:func:`~imbalanced_ensemble.pipeline.make_pipeline` helper function) working with\ntransformers (:class:`~sklearn.decomposition.PCA`, \n:class:`~sklearn.neighbors.KNeighborsClassifier` from *scikit-learn*) and resamplers\n(:class:`~imbalanced_ensemble.sampler.under_sampling.EditedNearestNeighbours`, \n:class:`~imbalanced_ensemble.sampler.over_sampling.SMOTE`).\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Adapted from imbalanced-learn\n# Authors: Christos Aridas\n#          Guillaume Lemaitre <g.lemaitre58@gmail.com>\n# License: MIT"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's first create an imbalanced dataset and split in to two sets.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.datasets import make_classification\nfrom sklearn.model_selection import train_test_split\n\nX, y = make_classification(\n    n_classes=2,\n    class_sep=1.25,\n    weights=[0.3, 0.7],\n    n_informative=3,\n    n_redundant=1,\n    flip_y=0,\n    n_features=5,\n    n_clusters_per_class=1,\n    n_samples=5000,\n    random_state=10,\n)\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, we will create each individual steps \nthat we would like later to combine\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.decomposition import PCA\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom imbalanced_ensemble.sampler.under_sampling import EditedNearestNeighbours\nfrom imbalanced_ensemble.sampler.over_sampling import SMOTE\n\npca = PCA(n_components=2)\nenn = EditedNearestNeighbours()\nsmote = SMOTE(random_state=0)\nknn = KNeighborsClassifier(n_neighbors=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, we can finally create a pipeline to specify in which order the different\ntransformers and samplers should be executed before to provide the data to\nthe final classifier.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from imbalanced_ensemble.pipeline import make_pipeline\n\nmodel = make_pipeline(pca, enn, smote, knn)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can now use the pipeline created as a normal classifier where resampling\nwill happen when calling `fit` and disabled when calling `decision_function`,\n`predict_proba`, or `predict`.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.metrics import classification_report\n\nmodel.fit(X_train, y_train)\ny_pred = model.predict(X_test)\nprint(classification_report(y_test, y_pred))"
      ]
    }
  ],
  "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.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}