.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualization/plot_interaction_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_interaction_values.py: Working with InteractionValues ============================== This example shows how to create, manipulate, aggregate, and visualize :class:`~shapiq.InteractionValues` objects -- the central output type of ``shapiq``. .. GENERATED FROM PYTHON SOURCE LINES 9-14 .. code-block:: Python from __future__ import annotations from shapiq import InteractionValues .. GENERATED FROM PYTHON SOURCE LINES 15-18 Creating InteractionValues -------------------------- Shapley values (first-order interactions) for 3 players: .. GENERATED FROM PYTHON SOURCE LINES 18-28 .. code-block:: Python sv = InteractionValues( values={(0,): 1.0, (1,): 1.0, (2,): 1.0}, index="SV", max_order=1, n_players=3, min_order=1, ) print(sv) .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=SV, max_order=1, min_order=1, estimated=True, estimation_budget=None, n_players=3, baseline_value=0.0, Top 10 interactions: (0,): 1.0 (1,): 1.0 (2,): 1.0 ) .. GENERATED FROM PYTHON SOURCE LINES 29-30 Second-order Shapley Interaction Index (SII) values: .. GENERATED FROM PYTHON SOURCE LINES 30-49 .. code-block:: Python raw_scores = { (): 0, (0,): 1, (1,): 1, (2,): 1, (0, 1): 0.5, (0, 2): 0.5, (1, 2): 0.5, } sii = InteractionValues( values=raw_scores, index="SII", max_order=2, n_players=3, min_order=0, ) print(sii) .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=SII, max_order=2, min_order=0, estimated=True, estimation_budget=None, n_players=3, baseline_value=0.0, Top 10 interactions: (0,): 1.0 (1,): 1.0 (2,): 1.0 (0, 1): 0.5 (0, 2): 0.5 (1, 2): 0.5 (): 0.0 ) .. GENERATED FROM PYTHON SOURCE LINES 50-53 Manipulating InteractionValues ------------------------------ Scalar operations and pairwise addition/subtraction are supported. .. GENERATED FROM PYTHON SOURCE LINES 53-69 .. code-block:: Python scaled = sv + 2 scaled *= 2 scaled -= 2 sii2 = InteractionValues( values=raw_scores, index="SII", max_order=2, n_players=3, min_order=0, ) added = sii + sii2 subtracted = sii - sii2 aggregated = sii.aggregate([sii2], aggregation="mean") .. GENERATED FROM PYTHON SOURCE LINES 70-73 Extracting Specific Orders --------------------------- Get only second-order interactions, or as a matrix. .. GENERATED FROM PYTHON SOURCE LINES 73-77 .. code-block:: Python print(sii.get_n_order(2)) print("Second-order matrix:\n", sii.get_n_order_values(2)) .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=SII, max_order=2, min_order=2, estimated=True, estimation_budget=None, n_players=3, baseline_value=0.0, Top 10 interactions: (0, 1): 0.5 (0, 2): 0.5 (1, 2): 0.5 ) Second-order matrix: [[0. 0.5 0.5] [0.5 0. 0.5] [0.5 0.5 0. ]] .. GENERATED FROM PYTHON SOURCE LINES 78-80 Subset of Players ------------------ .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: Python print(sii.get_subset([0, 2])) .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=SII, max_order=2, min_order=0, estimated=True, estimation_budget=None, n_players=1, baseline_value=0.0, Top 10 interactions: (0,): 1.0 (2,): 1.0 (0, 2): 0.5 (): 0.0 ) .. GENERATED FROM PYTHON SOURCE LINES 84-87 Sparsification --------------- Remove near-zero interactions. .. GENERATED FROM PYTHON SOURCE LINES 87-98 .. code-block:: Python noisy = InteractionValues( values={(0,): 1.0, (1,): 1.0, (2,): 1.0, (0, 1): 1e-5, (0, 2): 1e-3, (1, 2): -1e-5}, index="SII", max_order=2, n_players=3, min_order=0, ) noisy.sparsify(threshold=1e-3) print("After sparsify:", noisy.interactions) .. rst-class:: sphx-glr-script-out .. code-block:: none After sparsify: {(0,): 1.0, (1,): 1.0, (2,): 1.0, (0, 2): 0.001} .. GENERATED FROM PYTHON SOURCE LINES 99-102 Visualization ------------- Force plot and UpSet plot. .. GENERATED FROM PYTHON SOURCE LINES 102-105 .. code-block:: Python sii.plot_force() .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_interaction_values_001.png :alt: plot interaction values :srcset: /auto_examples/visualization/images/sphx_glr_plot_interaction_values_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 106-109 .. code-block:: Python sii.plot_upset() .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_interaction_values_002.png :alt: plot interaction values :srcset: /auto_examples/visualization/images/sphx_glr_plot_interaction_values_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 110-112 Saving and Loading ------------------ .. GENERATED FROM PYTHON SOURCE LINES 112-121 .. code-block:: Python import tempfile from pathlib import Path with tempfile.TemporaryDirectory() as tmpdir: path = Path(tmpdir) / "sii.json" sii.save(path) loaded = InteractionValues.load(path) print(loaded) .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=SII, max_order=2, min_order=0, estimated=True, estimation_budget=None, n_players=3, baseline_value=0.0, Top 10 interactions: (0,): 1.0 (1,): 1.0 (2,): 1.0 (0, 1): 0.5 (0, 2): 0.5 (1, 2): 0.5 (): 0.0 ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.241 seconds) .. _sphx_glr_download_auto_examples_visualization_plot_interaction_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_interaction_values.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_interaction_values.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_interaction_values.zip `