.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualization/plot_upset.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_upset.py: UpSet Plot ========== This example demonstrates the UpSet plot for visualizing feature interactions. The UpSet plot shows the most important interactions as vertical bars with the interacting features in a matrix below. .. 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-20 Train a Model and Compute Explanations ---------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-44 .. 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="FSII", max_order=2, 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=2, min_order=0, estimated=True, estimation_budget=200, n_players=8, baseline_value=2.058321475982666, Top 10 interactions: (): 2.058321475982666 (7,): 1.3230916479551968 (5,): 0.41396584141388837 (6, 7): 0.31170752719138173 (1, 7): 0.267773292536492 (2, 5): 0.21734101617495935 (1,): 0.14959330449384228 (1, 5): 0.1408693332978724 (0,): -0.2003426692827929 (6,): -0.5329538181989749 ) .. GENERATED FROM PYTHON SOURCE LINES 45-47 Basic UpSet Plot ---------------- .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python explanation.plot_upset() .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_upset_001.png :alt: plot upset :srcset: /auto_examples/visualization/images/sphx_glr_plot_upset_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 51-53 Customization: Number of Interactions --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: Python explanation.plot_upset(n_interactions=10) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_upset_002.png :alt: plot upset :srcset: /auto_examples/visualization/images/sphx_glr_plot_upset_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 57-60 Color the Matrix ---------------- Color-code the matrix based on interaction sign (red = positive, blue = negative). .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python explanation.plot_upset(color_matrix=True) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_upset_003.png :alt: plot upset :srcset: /auto_examples/visualization/images/sphx_glr_plot_upset_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-66 Feature Names ------------- .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: Python explanation.plot_upset(feature_names=feature_names, n_interactions=15) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_upset_004.png :alt: plot upset :srcset: /auto_examples/visualization/images/sphx_glr_plot_upset_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-73 Show Only Relevant Features ---------------------------- Set ``all_features=False`` to show only features present in the top interactions. .. GENERATED FROM PYTHON SOURCE LINES 73-76 .. code-block:: Python explanation.plot_upset(all_features=False, n_interactions=7, feature_names=feature_names) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_upset_005.png :alt: plot upset :srcset: /auto_examples/visualization/images/sphx_glr_plot_upset_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-79 Adjust Figure Size ------------------- .. GENERATED FROM PYTHON SOURCE LINES 79-81 .. code-block:: Python explanation.plot_upset(n_interactions=5, figsize=(5, None)) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_upset_006.png :alt: plot upset :srcset: /auto_examples/visualization/images/sphx_glr_plot_upset_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.791 seconds) .. _sphx_glr_download_auto_examples_visualization_plot_upset.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_upset.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_upset.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_upset.zip `