{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Custom importance getter with SHAP values in CRFE\n\n:class:`~sklearn.ensemble.HistGradientBoostingClassifier` does not expose ``coef_``\nor ``feature_importances_``, so the default ``importance_getter=\"auto\"`` of\n:class:`~tagarnina.crfe.CRFE` cannot rank its features. This example passes a\ncallable instead. The callable returns the mean absolute SHAP value of each\nfeature, which is a global importance that works for any tree model that SHAP\ncan explain.\n\nThe dataset is the German credit dataset from the basic example, with the same\none-hot encoding and the same all-or-none constraints on the categorical features.\n\nThe dataset is public and is available on OpenML (ID 31). Please cite it as:\nDr. Hans Hofmann, Statlog (German Credit Data), UCI Machine Learning Repository,\n1994.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Load the data and build the constraints\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import warnings\n\nimport pandas as pd\nfrom sklearn.compose import ColumnTransformer, make_column_selector\nfrom sklearn.datasets import fetch_openml\nfrom sklearn.preprocessing import OneHotEncoder\n\nfrom tagarnina.optimization.constraints import AllOrNoneConstraint, Constraints\n\nwarnings.filterwarnings(\"ignore\")\n\nX, y = fetch_openml(data_id=31, as_frame=True, return_X_y=True, parser=\"auto\")\ny = (y == \"good\").astype(int)\ncategorical_features = X.select_dtypes(\"category\").columns.tolist()\n\npreprocessor = ColumnTransformer(\n    transformers=[\n        (\"cat\", OneHotEncoder(), make_column_selector(dtype_include=\"category\"))\n    ],\n    remainder=\"passthrough\",\n    verbose_feature_names_out=False,\n)\nX_transformed = pd.DataFrame(\n    data=preprocessor.fit_transform(X),\n    columns=preprocessor.get_feature_names_out(),\n)\n\nconstraints = Constraints(\n    constraints=[\n        AllOrNoneConstraint(\n            features=[c for c in X_transformed.columns if c.startswith(f\"{feature}_\")]\n        )\n        for feature in categorical_features\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## The default importance getter fails\nWith ``importance_getter=\"auto\"`` the first fit raises an error, because the\nestimator exposes neither ``coef_`` nor ``feature_importances_``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.ensemble import HistGradientBoostingClassifier\n\nfrom tagarnina.crfe import CRFE\n\nestimator = HistGradientBoostingClassifier(random_state=0)\n\ntry:\n    CRFE(\n        estimator=estimator,\n        constraints=constraints,\n        step=0.1,\n        n_features_to_select=20,\n        scoring=\"roc_auc\",\n    ).fit(X_transformed, y)\nexcept ValueError as error:\n    print(error)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## SHAP importance getter\nThe callable receives the fitted estimator and the current features ``X``.\nIt explains the model with :class:`shap.TreeExplainer` and returns the mean\nabsolute SHAP value of each feature. CRFE calls it at every elimination step, so\nthe importances always refer to the features still in the model.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport shap\n\n\ndef shap_importances(*, estimator, X):\n    explainer = shap.TreeExplainer(estimator)\n    shap_values = explainer.shap_values(X)\n    return np.abs(shap_values).mean(axis=0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Feature elimination\nThe run is the same as in the basic example, with the callable as\n``importance_getter``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "crfe = CRFE(\n    estimator=estimator,\n    importance_getter=shap_importances,\n    constraints=constraints,\n    step=0.1,\n    n_features_to_select=20,\n    scoring=\"roc_auc\",\n)\ncrfe.fit(X_transformed, y)\n\nselected_features = crfe.get_feature_names_out()\nprint(f\"Selected {len(selected_features)} features\")\nprint(crfe.summary_[\"termination\"])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Explaining the final model\n:attr:`~tagarnina.crfe.CRFE.estimator_` is fitted on the selected features. The\nbar plot shows their mean absolute SHAP values on the training data.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nX_selected = X_transformed[selected_features]\nshap_values = shap.TreeExplainer(crfe.estimator_).shap_values(X_selected)\n\nfig, ax = plt.subplots(figsize=(6, 6))\nmean_abs = pd.Series(np.abs(shap_values).mean(axis=0), index=selected_features)\nmean_abs.sort_values().plot.barh(ax=ax)\nax.set_xlabel(\"Mean |SHAP value| (log-odds)\")\nfig.tight_layout()"
      ]
    }
  ],
  "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.13.17"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}