.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/plot_parallel_computation.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_basics_plot_parallel_computation.py: Parallel Computation with joblib ================================= This example shows how to speed up ``shapiq`` explanations using parallel computation via `joblib `_. We use a simple synthetic game to keep runtime short. .. GENERATED FROM PYTHON SOURCE LINES 9-16 .. code-block:: Python from __future__ import annotations import numpy as np import shapiq .. GENERATED FROM PYTHON SOURCE LINES 17-20 Define a Synthetic Game ----------------------- A lightweight callable that we can explain quickly. .. GENERATED FROM PYTHON SOURCE LINES 20-35 .. code-block:: Python rng = np.random.default_rng(42) n_features = 8 weights = rng.standard_normal(n_features) def synthetic_model(x: np.ndarray) -> np.ndarray: """Simple linear model with interaction term.""" return x @ weights + 0.5 * x[:, 0] * x[:, 1] # Create synthetic data X_background = rng.standard_normal((100, n_features)) X_test = rng.standard_normal((6, n_features)) .. GENERATED FROM PYTHON SOURCE LINES 36-38 Explain a Single Instance -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-53 .. code-block:: Python explainer = shapiq.Explainer( model=synthetic_model, data=X_background, index="k-SII", max_order=2, random_state=0, ) print(f"Explainer type: {type(explainer).__name__}") iv = explainer.explain(X_test[0], budget=256) print(iv) shapiq.network_plot(interaction_values=iv, feature_names=[f"x{i}" for i in range(n_features)]) .. image-sg:: /auto_examples/basics/images/sphx_glr_plot_parallel_computation_001.png :alt: plot parallel computation :srcset: /auto_examples/basics/images/sphx_glr_plot_parallel_computation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Explainer type: TabularExplainer InteractionValues( index=k-SII, max_order=2, min_order=0, estimated=False, estimation_budget=256, n_players=8, baseline_value=0.3123873157557045, Top 10 interactions: (5,): 0.5176617643254364 (): 0.3123873157557045 (0, 1): 0.03694416683658724 (6,): -0.06933886084594183 (0,): -0.11273767884210385 (7,): -0.1365021776001595 (1,): -0.2622014244845414 (2,): -0.26702089174324456 (4,): -0.4915673843261027 (3,): -0.5965700344493814 ) (
, ) .. GENERATED FROM PYTHON SOURCE LINES 54-57 Parallel Explanation of Multiple Instances -------------------------------------------- Use ``n_jobs`` in :meth:`~shapiq.Explainer.explain_X` to parallelize. .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python ivs = explainer.explain_X(X_test, budget=256, n_jobs=2) print(f"Computed {len(ivs)} explanations") .. rst-class:: sphx-glr-script-out .. code-block:: none Computed 6 explanations .. GENERATED FROM PYTHON SOURCE LINES 62-64 Global Feature Importance -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: Python shapiq.plot.bar_plot(ivs, feature_names=[f"x{i}" for i in range(n_features)]) .. image-sg:: /auto_examples/basics/images/sphx_glr_plot_parallel_computation_002.png :alt: plot parallel computation :srcset: /auto_examples/basics/images/sphx_glr_plot_parallel_computation_002.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 2.522 seconds) .. _sphx_glr_download_auto_examples_basics_plot_parallel_computation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_parallel_computation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_parallel_computation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_parallel_computation.zip `