.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/plot_sv_calculation.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_auto_examples_basics_plot_sv_calculation.py: Computing Shapley Values ========================= This example introduces cooperative game theory and shows how to compute Shapley values with :mod:`shapiq` -- both exactly and via approximation -- and how to apply them for explainable AI (XAI). .. GENERATED FROM PYTHON SOURCE LINES 9-16 .. code-block:: Python from __future__ import annotations import numpy as np import shapiq .. GENERATED FROM PYTHON SOURCE LINES 17-21 The Cooking Game ---------------- Three cooks (Alice, Bob, Charlie) prepare a meal together. We model their joint productivity as a cooperative game and compute exact Shapley values. .. GENERATED FROM PYTHON SOURCE LINES 21-49 .. code-block:: Python class CookingGame(shapiq.Game): """Cooking game with three cooks.""" def __init__(self) -> None: self.characteristic_function = { (): 0, (0,): 4, (1,): 3, (2,): 2, (0, 1): 9, (0, 2): 8, (1, 2): 7, (0, 1, 2): 15, } super().__init__( n_players=3, player_names=["Alice", "Bob", "Charlie"], normalization_value=self.characteristic_function[()], ) def value_function(self, coalitions: np.ndarray) -> np.ndarray: return np.array([self.characteristic_function[tuple(np.where(c)[0])] for c in coalitions]) cooking_game = CookingGame() .. GENERATED FROM PYTHON SOURCE LINES 50-53 Exact Shapley Values --------------------- The :class:`~shapiq.ExactComputer` evaluates all :math:`2^n` coalitions. .. GENERATED FROM PYTHON SOURCE LINES 53-64 .. code-block:: Python exact_computer = shapiq.ExactComputer(n_players=cooking_game.n_players, game=cooking_game) sv_exact = exact_computer(index="SV") print(sv_exact) sv_exact.plot_stacked_bar( xlabel="Cooks", ylabel="Shapley Values", feature_names=["Alice", "Bob", "Charlie"], ) .. image-sg:: /auto_examples/basics/images/sphx_glr_plot_sv_calculation_001.png :alt: plot sv calculation :srcset: /auto_examples/basics/images/sphx_glr_plot_sv_calculation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=SV, max_order=1, min_order=0, estimated=False, estimation_budget=None, n_players=3, baseline_value=0.0, Top 10 interactions: (0,): 6.0 (1,): 5.0 (2,): 3.9999999999999996 (): 0.0 ) .. GENERATED FROM PYTHON SOURCE LINES 65-70 Approximating Shapley Values ----------------------------- For larger games, exact computation is infeasible. Here we define a 10-player restaurant game and approximate Shapley values with :class:`~shapiq.KernelSHAP`. .. GENERATED FROM PYTHON SOURCE LINES 70-89 .. code-block:: Python rng = np.random.default_rng(42) quality_dict = {cooks: rng.random() * len(cooks) for cooks in shapiq.powerset(range(10))} def restaurant_value_function(coalitions: np.ndarray) -> np.ndarray: return np.array([quality_dict[tuple(np.where(c)[0])] for c in coalitions]) approx = shapiq.KernelSHAP(n=10, random_state=42) sv_approx = approx(game=restaurant_value_function, budget=100) print(sv_approx) sv_approx.plot_stacked_bar( xlabel="Cooks", ylabel="Shapley Values", feature_names=[f"Cook {i}" for i in range(10)], ) .. image-sg:: /auto_examples/basics/images/sphx_glr_plot_sv_calculation_002.png :alt: plot sv calculation :srcset: /auto_examples/basics/images/sphx_glr_plot_sv_calculation_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=SV, max_order=1, min_order=0, estimated=True, estimation_budget=100, n_players=10, baseline_value=0.0, Top 10 interactions: (4,): 1.2877903732343707 (7,): 0.9352475154189892 (6,): 0.7299620630825716 (3,): 0.7097257920905784 (2,): 0.6497116613130968 (8,): 0.6355807790513686 (1,): 0.3433454317119682 (5,): 0.13950816520202586 (9,): 0.026330920377390772 (0,): -0.143064579702957 ) .. GENERATED FROM PYTHON SOURCE LINES 90-94 XAI with Shapley Values ------------------------ We train a Random Forest on the California housing dataset and explain a single prediction using :class:`~shapiq.TabularExplainer`. .. GENERATED FROM PYTHON SOURCE LINES 94-112 .. code-block:: Python from sklearn.ensemble import RandomForestRegressor from sklearn.model_selection import train_test_split data, targets = shapiq.datasets.load_california_housing() feature_names = list(data.columns) n_features = len(feature_names) x_train, x_test, y_train, y_test = train_test_split( data.values, targets.values, test_size=0.2, random_state=42, ) rf = RandomForestRegressor(n_estimators=30, random_state=42) rf.fit(x_train, y_train) print(f"Test R2: {rf.score(x_test, y_test):.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Test R2: 0.8001 .. GENERATED FROM PYTHON SOURCE LINES 113-115 Explain a Single Prediction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 115-134 .. code-block:: Python x_explain = x_test[2] y_pred = rf.predict([x_explain])[0] print(f"Predicted: {y_pred:.3f}, Average: {np.mean(rf.predict(x_test)):.3f}") explainer = shapiq.TabularExplainer( model=rf, data=x_test, imputer="marginal", index="SV", max_order=1, sample_size=100, random_state=42, ) sv = explainer.explain(x_explain, budget=2**n_features) print(sv) sv.plot_force(feature_names=feature_names) .. image-sg:: /auto_examples/basics/images/sphx_glr_plot_sv_calculation_003.png :alt: plot sv calculation :srcset: /auto_examples/basics/images/sphx_glr_plot_sv_calculation_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Predicted: 4.956, Average: 2.066 InteractionValues( index=SV, max_order=1, min_order=0, estimated=False, estimation_budget=256, n_players=8, baseline_value=2.0660426668281655, Top 10 interactions: (): 2.0660426668281655 (5,): 0.9779035073737224 (7,): 0.7369852714174642 (1,): 0.48371604761721454 (6,): 0.3593446011880915 (0,): 0.149606913635752 (3,): 0.10402608554445877 (2,): 0.04175938675099633 (4,): 0.03618851062965703 ) .. GENERATED FROM PYTHON SOURCE LINES 135-139 TreeExplainer for Exact Tree-based SV ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For tree models, :class:`~shapiq.TreeExplainer` computes exact Shapley values in linear time. .. GENERATED FROM PYTHON SOURCE LINES 139-145 .. code-block:: Python tree_explainer = shapiq.TreeExplainer(model=rf, index="SV", max_order=1) sv_tree = tree_explainer.explain(x_explain) print(sv_tree) sv_tree.plot_force(feature_names=feature_names) .. image-sg:: /auto_examples/basics/images/sphx_glr_plot_sv_calculation_004.png :alt: plot sv calculation :srcset: /auto_examples/basics/images/sphx_glr_plot_sv_calculation_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=SV, max_order=1, min_order=0, estimated=True, estimation_budget=None, n_players=8, baseline_value=2.072542972807655, Top 10 interactions: (): 2.072542972807655 (5,): 1.0855436489007038 (7,): 0.6743220798486768 (1,): 0.5348653217356988 (6,): 0.24030731415309226 (0,): 0.23839871195972875 (3,): 0.06806166913442126 (2,): 0.026626962106926326 (4,): 0.014904831205571135 ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.887 seconds) .. _sphx_glr_download_auto_examples_basics_plot_sv_calculation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sv_calculation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sv_calculation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sv_calculation.zip `