.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualization/plot_scatter.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_scatter.py: Scatter Plot ============ This example demonstrates :func:`~shapiq.scatter_plot`, which plots the per-sample value of an interaction against the value of one feature. For first-order interactions this matches SHAP's ``shap.plots.scatter``; for higher-order interactions the x-axis is restricted to a single feature in the interaction tuple. .. GENERATED FROM PYTHON SOURCE LINES 11-20 .. code-block:: Python from __future__ import annotations import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from xgboost import XGBRegressor import shapiq .. GENERATED FROM PYTHON SOURCE LINES 21-23 Train a Model ------------- .. GENERATED FROM PYTHON SOURCE LINES 23-36 .. 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 37-41 Compute Explanations for Multiple Instances --------------------------------------------- We explain 200 test instances so the scatter plots show a meaningful distribution while keeping the example fast. .. GENERATED FROM PYTHON SOURCE LINES 41-52 .. code-block:: Python x_explain = x_test[:200] explainer = shapiq.TabularExplainer( model, data=x_test, index="FSII", max_order=2, random_state=42, ) explanations = explainer.explain_X(x_explain, budget=200) .. GENERATED FROM PYTHON SOURCE LINES 53-57 Default Scatter Plot --------------------- Without an explicit ``interaction``, the most important interaction is selected automatically (by mean absolute aggregated value). .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: Python shapiq.scatter_plot(explanations, x_explain, feature_names=feature_names) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_scatter_001.png :alt: plot scatter :srcset: /auto_examples/visualization/images/sphx_glr_plot_scatter_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-65 Main Effect of a Single Feature -------------------------------- Pass a feature name (or index) to plot its first-order Shapley value against its feature values. .. GENERATED FROM PYTHON SOURCE LINES 65-73 .. code-block:: Python shapiq.scatter_plot( explanations, x_explain, interaction="MedInc", feature_names=feature_names, ) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_scatter_002.png :alt: plot scatter :srcset: /auto_examples/visualization/images/sphx_glr_plot_scatter_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-78 Pairwise Interaction --------------------- Plot a higher-order interaction value. By default the x-axis is the first feature in the interaction tuple. .. GENERATED FROM PYTHON SOURCE LINES 78-86 .. code-block:: Python shapiq.scatter_plot( explanations, x_explain, interaction=("MedInc", "Latitude"), feature_names=feature_names, ) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_scatter_003.png :alt: plot scatter :srcset: /auto_examples/visualization/images/sphx_glr_plot_scatter_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-90 Pairwise Interaction with Chosen X-axis ----------------------------------------- Use ``x_feature`` to switch which feature in the interaction is on the x-axis. .. GENERATED FROM PYTHON SOURCE LINES 90-99 .. code-block:: Python shapiq.scatter_plot( explanations, x_explain, interaction=("MedInc", "Latitude"), x_feature="Latitude", feature_names=feature_names, ) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_scatter_004.png :alt: plot scatter :srcset: /auto_examples/visualization/images/sphx_glr_plot_scatter_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 100-104 Color by Another Feature ------------------------- Set ``color`` to render points using a red-blue colormap based on another feature's value, and add a colorbar. .. GENERATED FROM PYTHON SOURCE LINES 104-113 .. code-block:: Python shapiq.scatter_plot( explanations, x_explain, interaction="MedInc", color="HouseAge", feature_names=feature_names, ) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_scatter_005.png :alt: plot scatter :srcset: /auto_examples/visualization/images/sphx_glr_plot_scatter_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 114-118 Disable the X-axis Histogram Strip ----------------------------------- By default a faint histogram of the x-axis feature is drawn along the bottom (SHAP-style). Pass ``hist=False`` to hide it. .. GENERATED FROM PYTHON SOURCE LINES 118-127 .. code-block:: Python shapiq.scatter_plot( explanations, x_explain, interaction="MedInc", feature_names=feature_names, hist=False, ) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_scatter_006.png :alt: plot scatter :srcset: /auto_examples/visualization/images/sphx_glr_plot_scatter_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 128-130 Custom Axis ----------- .. GENERATED FROM PYTHON SOURCE LINES 130-139 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 5)) shapiq.scatter_plot( explanations, x_explain, interaction="MedInc", feature_names=feature_names, ax=ax, ) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_scatter_007.png :alt: plot scatter :srcset: /auto_examples/visualization/images/sphx_glr_plot_scatter_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.137 seconds) .. _sphx_glr_download_auto_examples_visualization_plot_scatter.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_scatter.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_scatter.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_scatter.zip `