.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualization/plot_shapley_values.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_visualization_plot_shapley_values.py: Computing Shapley Values. ========================= A minimal example showing how to compute exact Shapley values for a small cooperative game using :class:`~shapiq.ExactComputer`. .. GENERATED FROM PYTHON SOURCE LINES 7-15 .. code-block:: Python from __future__ import annotations import matplotlib.pyplot as plt import numpy as np import shapiq .. GENERATED FROM PYTHON SOURCE LINES 16-20 Define a Simple Game -------------------- We define a 4-player weighted voting game as a callable. The game value is 1 if the coalition's total weight exceeds a threshold, else 0. .. GENERATED FROM PYTHON SOURCE LINES 20-30 .. code-block:: Python weights = np.array([0.4, 0.3, 0.2, 0.1]) threshold = 0.5 def voting_game(coalitions: np.ndarray) -> np.ndarray: """Return 1 if coalition weight exceeds the threshold, else 0.""" return (coalitions @ weights > threshold).astype(float) .. GENERATED FROM PYTHON SOURCE LINES 31-35 Compute Exact Shapley Values ---------------------------- :class:`~shapiq.ExactComputer` exhaustively evaluates all 2^n coalitions to compute exact interaction values. It accepts any callable game directly. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python computer = shapiq.ExactComputer(voting_game, n_players=4) sv = computer(index="SV", order=1) .. GENERATED FROM PYTHON SOURCE LINES 40-42 Visualize with a Bar Plot ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-52 .. code-block:: Python fig, ax = plt.subplots(figsize=(5, 3)) ax.bar(range(4), [sv[(i,)] for i in range(4)], color="#5b9bd5") ax.set_xticks(range(4)) ax.set_xticklabels([f"Player {i}" for i in range(4)]) ax.set_ylabel("Shapley Value") ax.set_title("Shapley Values for a Weighted Voting Game") ax.axhline(0, color="black", linewidth=0.8, linestyle="--") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_shapley_values_001.png :alt: Shapley Values for a Weighted Voting Game :srcset: /auto_examples/visualization/images/sphx_glr_plot_shapley_values_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.051 seconds) .. _sphx_glr_download_auto_examples_visualization_plot_shapley_values.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_shapley_values.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_shapley_values.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_shapley_values.zip `