.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/trees/plot_treeshapiq_lightgbm.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_trees_plot_treeshapiq_lightgbm.py: TreeSHAP-IQ for LightGBM ========================= This example demonstrates :class:`~shapiq.TreeExplainer` on a LightGBM model trained on the bike-sharing dataset. TreeSHAP-IQ computes exact Shapley interaction values in linear time for tree ensembles. .. GENERATED FROM PYTHON SOURCE LINES 9-17 .. code-block:: Python from __future__ import annotations import lightgbm from sklearn.model_selection import train_test_split import shapiq .. GENERATED FROM PYTHON SOURCE LINES 18-20 Load Data and Train Model -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-40 .. code-block:: Python X, y = shapiq.load_bike_sharing() X_train, X_test, y_train, y_test = train_test_split( X.values, y.values, test_size=0.25, random_state=42, ) n_features = X_train.shape[1] model = lightgbm.LGBMRegressor( n_estimators=100, max_depth=n_features, random_state=42, verbose=-1, ) model.fit(X_train, y_train) print(f"Train R2: {model.score(X_train, y_train):.4f}") print(f"Test R2: {model.score(X_test, y_test):.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/shapiq/checkouts/latest/.venv/lib/python3.12/site-packages/sklearn/utils/validation.py:2691: UserWarning: X does not have valid feature names, but LGBMRegressor was fitted with feature names warnings.warn( Train R2: 0.9599 /home/docs/checkouts/readthedocs.org/user_builds/shapiq/checkouts/latest/.venv/lib/python3.12/site-packages/sklearn/utils/validation.py:2691: UserWarning: X does not have valid feature names, but LGBMRegressor was fitted with feature names warnings.warn( Test R2: 0.9478 .. GENERATED FROM PYTHON SOURCE LINES 41-44 Compute Shapley Interactions ----------------------------- We compute k-SII scores up to order 3 for a single instance. .. GENERATED FROM PYTHON SOURCE LINES 44-50 .. code-block:: Python explainer = shapiq.TreeExplainer(model=model, index="k-SII", min_order=1, max_order=3) x = X_test[1234] interaction_values = explainer.explain(x) print(interaction_values) .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=k-SII, max_order=3, min_order=1, estimated=False, estimation_budget=None, n_players=12, baseline_value=190.379622526228, Top 10 interactions: (np.int64(0),): 35.085159510882214 (np.int64(1), np.int64(5)): 14.984490827500759 (np.int64(0), np.int64(1)): 14.033445365073064 (np.int64(1), np.int64(6)): 11.124251580989434 (np.int64(0), np.int64(8)): -13.612044956259176 (np.int64(2),): -15.387584854862604 (np.int64(6),): -21.97379736656223 (np.int64(0), np.int64(9)): -32.8740365775636 (np.int64(5),): -42.991609226571114 (np.int64(1),): -56.72710843542341 ) .. GENERATED FROM PYTHON SOURCE LINES 51-53 First-order Values (Shapley Values) ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: Python print(interaction_values.get_n_order(1).dict_values) .. rst-class:: sphx-glr-script-out .. code-block:: none {(np.int64(0),): 35.085159510882214, (np.int64(1),): -56.72710843542341, (np.int64(5),): -42.991609226571114, (np.int64(9),): -4.207060851031486, (np.int64(10),): -10.861061944301156, (np.int64(11),): -2.472089266311177, (np.int64(2),): -15.387584854862604, (np.int64(3),): 5.371857243915892, (np.int64(6),): -21.97379736656223, (np.int64(8),): -5.934637391289779, (np.int64(7),): 0.25444939802939326, (np.int64(4),): 0.5157679457473514} .. GENERATED FROM PYTHON SOURCE LINES 57-59 Visualization: Network Plot ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python shapiq.network_plot(interaction_values=interaction_values, feature_names=list(X.columns)) .. image-sg:: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_lightgbm_001.png :alt: plot treeshapiq lightgbm :srcset: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_lightgbm_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (
, ) .. GENERATED FROM PYTHON SOURCE LINES 63-65 Stacked Bar Plot (First Order) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 65-71 .. code-block:: Python shapiq.stacked_bar_plot( interaction_values=interaction_values.get_n_order(1), feature_names=list(X.columns), ) .. image-sg:: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_lightgbm_002.png :alt: plot treeshapiq lightgbm :srcset: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_lightgbm_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (
, ) .. GENERATED FROM PYTHON SOURCE LINES 72-74 Stacked Bar Plot (First + Second Order) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 74-80 .. code-block:: Python shapiq.stacked_bar_plot( interaction_values=interaction_values.get_n_order(2, min_order=1), feature_names=list(X.columns), ) .. image-sg:: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_lightgbm_003.png :alt: plot treeshapiq lightgbm :srcset: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_lightgbm_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (
, ) .. GENERATED FROM PYTHON SOURCE LINES 81-83 Force Plot ---------- .. GENERATED FROM PYTHON SOURCE LINES 83-86 .. code-block:: Python interaction_values.plot_force(feature_names=list(X.columns), contribution_threshold=0.03) .. image-sg:: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_lightgbm_004.png :alt: plot treeshapiq lightgbm :srcset: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_lightgbm_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-90 Global Feature Importance -------------------------- Compute interaction values for 50 test instances and show global bar plot. .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. code-block:: Python list_of_ivs = explainer.explain_X(X_test[:50]) shapiq.plot.bar_plot(list_of_ivs, feature_names=list(X.columns), max_display=20) .. image-sg:: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_lightgbm_005.png :alt: plot treeshapiq lightgbm :srcset: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_lightgbm_005.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 47.719 seconds) .. _sphx_glr_download_auto_examples_trees_plot_treeshapiq_lightgbm.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_treeshapiq_lightgbm.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_treeshapiq_lightgbm.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_treeshapiq_lightgbm.zip `