.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/approximators/plot_exact_computer.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_approximators_plot_exact_computer.py: ExactComputer ============= Exact computation of Shapley values and interaction indices for small games using :class:`~shapiq.ExactComputer`. .. GENERATED FROM PYTHON SOURCE LINES 8-26 .. code-block:: Python from __future__ import annotations import numpy as np import shapiq N_PLAYERS = 8 feature_names = [f"x{i}" for i in range(N_PLAYERS)] weights = np.array([0.4, 0.3, 0.2, 0.1, 0.05, -0.1, -0.2, -0.3]) def game_fun(coalitions: np.ndarray) -> np.ndarray: coalitions = np.atleast_2d(coalitions) return (coalitions @ weights) + 0.5 * coalitions[:, 0] * coalitions[:, 1] .. GENERATED FROM PYTHON SOURCE LINES 27-29 Compute exact Shapley values ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 29-34 .. code-block:: Python computer = shapiq.ExactComputer(game_fun, n_players=N_PLAYERS) sv = computer(index="SV", order=1) print(sv) .. 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=8, baseline_value=0.0, Top 10 interactions: (0,): 0.6499999999999996 (1,): 0.5499999999999997 (2,): 0.20000000000000007 (3,): 0.1 (4,): 0.04999999999999996 (): 0.0 (5,): -0.10000000000000006 (6,): -0.2000000000000001 (7,): -0.29999999999999993 ) .. GENERATED FROM PYTHON SOURCE LINES 35-37 Force plot of Shapley values ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: Python sv.plot_force(feature_names=feature_names) .. image-sg:: /auto_examples/approximators/images/sphx_glr_plot_exact_computer_001.png :alt: plot exact computer :srcset: /auto_examples/approximators/images/sphx_glr_plot_exact_computer_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 41-43 Compute exact k-SII values --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 43-47 .. code-block:: Python ksii = computer(index="k-SII", order=2) print(ksii) .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=k-SII, max_order=2, min_order=0, estimated=False, estimation_budget=None, n_players=8, baseline_value=0.0, Top 10 interactions: (0, 1): 0.5 (0,): 0.3999999999999996 (1,): 0.2999999999999997 (2,): 0.20000000000000018 (3,): 0.10000000000000002 (4,): 0.04999999999999999 (1, 5): -1.1102230246251565e-16 (5,): -0.09999999999999994 (6,): -0.2 (7,): -0.29999999999999993 ) .. GENERATED FROM PYTHON SOURCE LINES 48-50 Force plot of k-SII values -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: Python ksii.plot_force(feature_names=feature_names) .. image-sg:: /auto_examples/approximators/images/sphx_glr_plot_exact_computer_002.png :alt: plot exact computer :srcset: /auto_examples/approximators/images/sphx_glr_plot_exact_computer_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-56 Network plot of k-SII values ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: Python ksii.plot_network(feature_names=feature_names) .. image-sg:: /auto_examples/approximators/images/sphx_glr_plot_exact_computer_003.png :alt: plot exact computer :srcset: /auto_examples/approximators/images/sphx_glr_plot_exact_computer_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.636 seconds) .. _sphx_glr_download_auto_examples_approximators_plot_exact_computer.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_exact_computer.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_exact_computer.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_exact_computer.zip `