.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualization/plot_si_graph.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_si_graph.py: SI Graph Plot ============= This example demonstrates the SI graph plot, which visualizes Shapley interactions as a network. Players are nodes; interactions are edges whose color, thickness, and opacity encode strength and direction. .. GENERATED FROM PYTHON SOURCE LINES 9-17 .. code-block:: Python from __future__ import annotations from sklearn.model_selection import train_test_split from xgboost import XGBRegressor import shapiq .. GENERATED FROM PYTHON SOURCE LINES 18-21 Train a Model ------------- We use an XGBoost regressor on the California housing dataset. .. GENERATED FROM PYTHON SOURCE LINES 21-34 .. code-block:: Python x_data, y_data = shapiq.datasets.load_california_housing(to_numpy=False) feature_names = list(x_data.columns) x_data, y_data = x_data.values, y_data.values x_train, x_test, y_train, y_test = train_test_split( x_data, y_data, test_size=0.2, random_state=42, ) model = XGBRegressor(random_state=42, max_depth=4, n_estimators=50) model.fit(x_train, y_train) .. raw:: html
XGBRegressor(base_score=None, booster=None, callbacks=None,
                 colsample_bylevel=None, colsample_bynode=None,
                 colsample_bytree=None, device=None, early_stopping_rounds=None,
                 enable_categorical=False, eval_metric=None, feature_types=None,
                 feature_weights=None, gamma=None, grow_policy=None,
                 importance_type=None, interaction_constraints=None,
                 learning_rate=None, max_bin=None, max_cat_threshold=None,
                 max_cat_to_onehot=None, max_delta_step=None, max_depth=4,
                 max_leaves=None, min_child_weight=None, missing=nan,
                 monotone_constraints=None, multi_strategy=None, n_estimators=50,
                 n_jobs=None, num_parallel_tree=None, ...)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 35-37 Compute Interaction Explanations --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 37-49 .. code-block:: Python x_explain = x_test[2] explainer = shapiq.TabularExplainer( model, data=x_test, index="FSII", max_order=3, random_state=42, ) explanation = explainer.explain(x_explain, budget=200) print(explanation) .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=FSII, max_order=3, min_order=0, estimated=True, estimation_budget=200, n_players=8, baseline_value=2.058321475982666, Top 10 interactions: (): 2.058321475982666 (7,): 1.402967800326762 (5,): 0.43523832301404913 (1,): 0.21230560526572517 (1, 6, 7): 0.2047095317738413 (1, 7): 0.17704990788723965 (3,): 0.17256228093418974 (1, 5): 0.15740167449061243 (0,): -0.16957968468468973 (6,): -0.4322099716014773 ) .. GENERATED FROM PYTHON SOURCE LINES 50-52 Basic SI Graph --------------- .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python explanation.plot_si_graph(show=False) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_si_graph_001.png :alt: plot si graph :srcset: /auto_examples/visualization/images/sphx_glr_plot_si_graph_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (
, ) .. GENERATED FROM PYTHON SOURCE LINES 56-59 Scaling and Feature Names ------------------------- Adjust node sizes and add feature names for readability. .. GENERATED FROM PYTHON SOURCE LINES 59-66 .. code-block:: Python explanation.plot_si_graph( feature_names=feature_names, size_factor=5.0, node_size_scaling=0.5, ) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_si_graph_002.png :alt: plot si graph :srcset: /auto_examples/visualization/images/sphx_glr_plot_si_graph_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 67-70 Filtering Interactions ---------------------- Show only interactions above a threshold or the top-N strongest. .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python explanation.plot_si_graph(feature_names=feature_names, draw_threshold=0.05) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_si_graph_003.png :alt: plot si graph :srcset: /auto_examples/visualization/images/sphx_glr_plot_si_graph_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-77 .. code-block:: Python explanation.plot_si_graph(feature_names=feature_names, n_interactions=7) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_si_graph_004.png :alt: plot si graph :srcset: /auto_examples/visualization/images/sphx_glr_plot_si_graph_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-81 .. code-block:: Python explanation.plot_si_graph(feature_names=feature_names, interaction_direction="positive") .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_si_graph_005.png :alt: plot si graph :srcset: /auto_examples/visualization/images/sphx_glr_plot_si_graph_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 82-85 Filtering by Order ------------------- Show only interactions up to a certain order. .. GENERATED FROM PYTHON SOURCE LINES 85-88 .. code-block:: Python explanation.plot_si_graph(feature_names=feature_names, min_max_order=(1, 2)) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_si_graph_006.png :alt: plot si graph :srcset: /auto_examples/visualization/images/sphx_glr_plot_si_graph_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: Python explanation.plot_si_graph(feature_names=feature_names, min_max_order=(3, -1)) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_si_graph_007.png :alt: plot si graph :srcset: /auto_examples/visualization/images/sphx_glr_plot_si_graph_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.333 seconds) .. _sphx_glr_download_auto_examples_visualization_plot_si_graph.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_si_graph.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_si_graph.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_si_graph.zip `