{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Pipeline ANOVA SVM\n\nThis example shows how a feature selection can be easily integrated within\na machine learning pipeline.\n\nWe also show that you can easily inspect part of the pipeline.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We will start by generating a binary classification dataset. Subsequently, we\nwill divide the dataset into two subsets.\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_features=20,\n    n_informative=3,\n    n_redundant=0,\n    n_classes=2,\n    n_clusters_per_class=2,\n    random_state=42,\n)\nX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "A common mistake done with feature selection is to search a subset of\ndiscriminative features on the full dataset, instead of only using the\ntraining set. The usage of scikit-learn :func:`~sklearn.pipeline.Pipeline`\nprevents to make such mistake.\n\nHere, we will demonstrate how to build a pipeline where the first step will\nbe the feature selection.\n\nWhen calling `fit` on the training data, a subset of feature will be selected\nand the index of these selected features will be stored. The feature selector\nwill subsequently reduce the number of features, and pass this subset to the\nclassifier which will be trained.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.feature_selection import SelectKBest, f_classif\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.svm import LinearSVC\n\nanova_filter = SelectKBest(f_classif, k=3)\nclf = LinearSVC()\nanova_svm = make_pipeline(anova_filter, clf)\nanova_svm.fit(X_train, y_train)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Once the training is complete, we can predict on new unseen samples. In this\ncase, the feature selector will only select the most discriminative features\nbased on the information stored during training. Then, the data will be\npassed to the classifier which will make the prediction.\n\nHere, we show the final metrics via a classification report.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.metrics import classification_report\n\ny_pred = anova_svm.predict(X_test)\nprint(classification_report(y_test, y_pred))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Be aware that you can inspect a step in the pipeline. For instance, we might\nbe interested about the parameters of the classifier. Since we selected\nthree features, we expect to have three coefficients.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "anova_svm[-1].coef_"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "However, we do not know which features were selected from the original\ndataset. We could proceed by several manners. Here, we will invert the\ntransformation of these coefficients to get information about the original\nspace.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "anova_svm[:-1].inverse_transform(anova_svm[-1].coef_)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can see that the features with non-zero coefficients are the selected\nfeatures by the first step.\n\n"
      ]
    }
  ],
  "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.11.14"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}