.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualization/plot_visualizing_interactions.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_visualizing_interactions.py: Visualization Catalog ====================== A comprehensive tour of all local and global visualization functions in ``shapiq``: force plot, waterfall plot, network plot, SI graph plot, stacked bar plot, and global bar plot. All examples use the same XGBoost model on the California housing dataset for consistency with the other visualization gallery scripts. .. GENERATED FROM PYTHON SOURCE LINES 12-20 .. 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 21-25 Train Model and Compute Explanations -------------------------------------- We train an XGBoost regressor and compute Shapley values (order 1) and k-SII interactions (order 2) for a single instance. .. GENERATED FROM PYTHON SOURCE LINES 25-51 .. 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) x_explain = x_test[2] explainer = shapiq.TabularExplainer( model, data=x_test, index="k-SII", max_order=2, random_state=42, ) iv = explainer.explain(x_explain, budget=200) sv = iv.get_n_order(1) print(iv) .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=k-SII, max_order=2, min_order=0, estimated=True, estimation_budget=200, n_players=8, baseline_value=2.058321475982666, Top 10 interactions: (): 2.058321475982666 (7,): 1.3350600113103832 (5,): 0.4500846779831734 (6, 7): 0.2980846968500863 (1, 7): 0.25150829991703183 (2, 5): 0.20722694546625944 (1,): 0.1832462712495247 (3,): 0.16499400964618197 (0,): -0.1686034639705406 (6,): -0.4933280450422622 ) .. GENERATED FROM PYTHON SOURCE LINES 52-56 Force Plot ---------- Shows how each interaction pushes the prediction away from the baseline. Works for any order of interactions. .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: Python sv.plot_force(feature_names=feature_names) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_001.png :alt: plot visualizing interactions :srcset: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python iv.plot_force(feature_names=feature_names) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_002.png :alt: plot visualizing interactions :srcset: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-67 Waterfall Plot --------------- Like the force plot but groups small interactions into an "other" bucket. .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python sv.plot_waterfall(feature_names=feature_names) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_003.png :alt: plot visualizing interactions :srcset: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: Python iv.plot_waterfall(feature_names=feature_names) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_004.png :alt: plot visualizing interactions :srcset: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-80 Network Plot ------------- Visualizes first- and second-order interactions as a graph. Node size encodes first-order importance; edge width encodes pairwise interaction strength. .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: Python iv.plot_network(feature_names=feature_names) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_005.png :alt: plot visualizing interactions :srcset: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 84-89 SI Graph Plot -------------- A more general graph plot that can display higher-order interactions as hyper-edges. See the dedicated :doc:`SI Graph Plot example ` for advanced options. .. GENERATED FROM PYTHON SOURCE LINES 89-92 .. code-block:: Python iv.plot_si_graph(feature_names=feature_names, size_factor=3.0) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_006.png :alt: plot visualizing interactions :srcset: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 93-98 Stacked Bar Plot ----------------- Shows per-feature interaction magnitude, stacked by order. Useful for comparing how much each feature contributes via main effects vs. interactions. .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. code-block:: Python shapiq.stacked_bar_plot(iv.get_n_order(1), feature_names=feature_names) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_007.png :alt: plot visualizing interactions :srcset: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (
, ) .. GENERATED FROM PYTHON SOURCE LINES 102-105 .. code-block:: Python shapiq.stacked_bar_plot(iv, feature_names=feature_names) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_008.png :alt: plot visualizing interactions :srcset: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_008.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (
, ) .. GENERATED FROM PYTHON SOURCE LINES 106-110 Global Bar Plot ---------------- Aggregates interaction values across multiple instances to show global feature (interaction) importance. .. GENERATED FROM PYTHON SOURCE LINES 110-113 .. code-block:: Python explanations = [explainer.explain(x_test[i], budget=200) for i in range(5)] shapiq.plot.bar_plot(explanations, feature_names=feature_names, max_display=15) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_009.png :alt: plot visualizing interactions :srcset: /auto_examples/visualization/images/sphx_glr_plot_visualizing_interactions_009.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.627 seconds) .. _sphx_glr_download_auto_examples_visualization_plot_visualizing_interactions.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_visualizing_interactions.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_visualizing_interactions.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_visualizing_interactions.zip `