.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/tabular/plot_conditional_imputer.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_tabular_plot_conditional_imputer.py: Conditional Data Imputation ============================ This example shows how to use :class:`~shapiq.ConditionalImputer` for conditional (observational) imputation when computing Shapley interactions. Conditional imputation respects feature dependencies, unlike marginal (interventional) imputation. .. GENERATED FROM PYTHON SOURCE LINES 10-18 .. code-block:: Python from __future__ import annotations from sklearn.ensemble import RandomForestRegressor from sklearn.model_selection import train_test_split import shapiq .. GENERATED FROM PYTHON SOURCE LINES 19-21 Load Data and Train Model -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-42 .. code-block:: Python X, y = shapiq.load_california_housing() 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 = RandomForestRegressor( n_estimators=100, max_depth=n_features, max_features=2 / 3, max_samples=2 / 3, random_state=42, ) 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 Train R2: 0.7965 Test R2: 0.7431 .. GENERATED FROM PYTHON SOURCE LINES 43-52 Conditional Imputer -------------------- Set ``imputer="conditional"`` in :class:`~shapiq.TabularExplainer`. The imputer trains a gradient boosting model per feature to learn the conditional distribution. Key parameters: - ``sample_size``: samples drawn from conditional background - ``conditional_budget``: coalitions per data point for training - ``conditional_threshold``: quantile threshold for neighbourhood .. GENERATED FROM PYTHON SOURCE LINES 52-64 .. code-block:: Python explainer = shapiq.TabularExplainer( model=model, data=X_train, index="SII", max_order=2, imputer="conditional", sample_size=100, conditional_budget=32, conditional_threshold=0.04, ) .. GENERATED FROM PYTHON SOURCE LINES 65-67 Explain a Single Instance -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 67-72 .. code-block:: Python x_explain = X_test[100] iv = explainer.explain(x_explain, budget=2**n_features, random_state=0) print(iv) .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=SII, max_order=2, min_order=0, estimated=False, estimation_budget=256, n_players=8, baseline_value=2.0701874006108745, Top 10 interactions: (0, 1): 0.07235594319404895 (5,): 0.06398005281148228 (2,): 0.037374147027905556 (5, 7): 0.037311123046789234 (1, 7): -0.029679463654197765 (1, 5): -0.0336762113531598 (0, 7): -0.06493048753501021 (1,): -0.1331566617040377 (7,): -0.17584970197462807 (0,): -0.19972672348833737 ) .. GENERATED FROM PYTHON SOURCE LINES 73-75 Network Plot ------------- .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: Python shapiq.network_plot(interaction_values=iv, feature_names=list(X.columns)) .. image-sg:: /auto_examples/tabular/images/sphx_glr_plot_conditional_imputer_001.png :alt: plot conditional imputer :srcset: /auto_examples/tabular/images/sphx_glr_plot_conditional_imputer_001.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 32.687 seconds) .. _sphx_glr_download_auto_examples_tabular_plot_conditional_imputer.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_conditional_imputer.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_conditional_imputer.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_conditional_imputer.zip `