{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Recursive feature elimination\n\nThis example demonstrates how Recursive Feature Elimination\n(:class:`~sklearn.feature_selection.RFE`) can be used to determine the\nimportance of individual pixels for classifying handwritten digits.\n:class:`~sklearn.feature_selection.RFE` recursively removes the least\nsignificant features, assigning ranks based on their importance, where higher\n`ranking_` values denote lower importance. The ranking is visualized using both\nshades of blue and pixel annotations for clarity. As expected, pixels positioned\nat the center of the image tend to be more predictive than those near the edges.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>See also `sphx_glr_auto_examples_feature_selection_plot_rfe_with_cross_validation.py`</p></div>\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nimport matplotlib.pyplot as plt\n\nfrom sklearn.datasets import load_digits\nfrom sklearn.feature_selection import RFE\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.preprocessing import MinMaxScaler\n\n# Load the digits dataset\ndigits = load_digits()\nX = digits.images.reshape((len(digits.images), -1))\ny = digits.target\n\npipe = Pipeline(\n    [\n        (\"scaler\", MinMaxScaler()),\n        (\"rfe\", RFE(estimator=LogisticRegression(), n_features_to_select=1, step=1)),\n    ]\n)\n\npipe.fit(X, y)\nranking = pipe.named_steps[\"rfe\"].ranking_.reshape(digits.images[0].shape)\n\n# Plot pixel ranking\nplt.matshow(ranking, cmap=plt.cm.Blues)\n\n# Add annotations for pixel numbers\nfor i in range(ranking.shape[0]):\n    for j in range(ranking.shape[1]):\n        plt.text(j, i, str(ranking[i, j]), ha=\"center\", va=\"center\", color=\"black\")\n\nplt.colorbar()\nplt.title(\"Ranking of pixels with RFE\\n(Logistic Regression)\")\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.11.14"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}