.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/gallery/examples/plot_msage.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_gallery_examples_plot_msage.py: Shapley Additive Global Importance (SAGE) example ================================================= In this example, we demonstrate how to measure feature importance using SAGE :footcite:t:`Covert2020` on the diabetes dataset. Read more in the :ref:`User Guide `. For this example, we use the marginal version of SAGE, which limits the computational cost. To further reduce the computational cost, Shapley values are estimated using a Monte Carlo approximation. Only a subset of all possible feature coalitions is sampled. This is controlled by the `n_subsets` parameter. Finally, the expectation over the marginal distribution is also approximated using `n_permutations`. .. GENERATED FROM PYTHON SOURCE LINES 18-23 LightGBM example on the bike sharing dataset -------------------------------------------- We demonstrate how to use SAGE on the bike sharing dataset. We fit a LightGBM model and compute its :math:`R^2` score on a held-out test set. .. GENERATED FROM PYTHON SOURCE LINES 23-45 .. code-block:: Python import numpy as np from sklearn.datasets import fetch_openml from sklearn.ensemble import HistGradientBoostingRegressor from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split from sklearn.preprocessing import OrdinalEncoder bike_sharing = fetch_openml("Bike_Sharing_Demand", version=2, as_frame=True) df = bike_sharing.frame df = df[df["year"] == 0].drop(columns=["year"]) X = df.drop(columns=["count"]).to_numpy() X = OrdinalEncoder().fit_transform(X) y = df["count"].to_numpy() X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) model = HistGradientBoostingRegressor(random_state=0, max_depth=5) model.fit(X_train, y_train) y_pred = model.predict(X_test) print("R2 score:", r2_score(y_test, y_pred)) .. rst-class:: sphx-glr-script-out .. code-block:: none R2 score: 0.9283833661677658 .. GENERATED FROM PYTHON SOURCE LINES 46-52 SAGE feature importance ----------------------- We compute the SAGE feature importance for the fitted model. To keep the computational cost tractable, we use a subset of the test set to compute the SAGE values. Finally, the SAGE values are plotted using the `plot_importance` function. .. GENERATED FROM PYTHON SOURCE LINES 52-78 .. code-block:: Python import matplotlib.pyplot as plt from hidimstat import SAGE sage = SAGE( model, n_subsets=512, n_permutations=10, random_state=0, n_jobs=8, ) sage.fit(X_train) subsample_size = 1024 rng = np.random.default_rng(0) subsample_ids = rng.choice(len(X_test), size=subsample_size, replace=False) sage.importance(X_test[subsample_ids], y_test[subsample_ids]) ax = sage.plot_importance( feature_names=df.drop(columns=["count"]).columns.tolist(), color="tab:purple", ) ax.semilogx() plt.tight_layout() plt.show() .. image-sg:: /generated/gallery/examples/images/sphx_glr_plot_msage_001.png :alt: plot msage :srcset: /generated/gallery/examples/images/sphx_glr_plot_msage_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/1925 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_msage.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_msage.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_