-
Notifications
You must be signed in to change notification settings - Fork 58
Add shapiq -> shap.Explanation bridge helper #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+127
−27
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
88fe534
Add shapiq -> shap.Explanation bridge helper
adrian-prior 570e0de
Pass per-row baselines through instead of averaging (Gemini, #292)
adrian-prior 1fab4d0
Re-gitignore .local/ and drop the test-plot artifacts I added by mistake
adrian-prior 6173b44
Drop the .gitignore tweak from this PR; out of scope
adrian-prior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| try: | ||
| from . import feature_selection, pdp, shapiq | ||
| from . import feature_selection, pdp, shap, shapiq | ||
| from .shap import shapiq_to_shap_explanation | ||
| except ImportError: | ||
| raise ImportError( | ||
| "Please install tabpfn-extensions with the 'interpretability' extra: pip install 'tabpfn-extensions[interpretability]'", | ||
| ) | ||
| __all__ = ["feature_selection", "shapiq", "pdp"] | ||
| __all__ = ["feature_selection", "shapiq", "pdp", "shap", "shapiq_to_shap_explanation"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright (c) Prior Labs GmbH 2025. | ||
| # Licensed under the Apache License, Version 2.0 | ||
|
|
||
| """Bridge helpers for using the SHAP library's plotting ecosystem with | ||
| Shapley values computed by shapiq. | ||
|
|
||
| We use shapiq for the actual Shapley-value computation — it's faster and | ||
| extension-friendly for TabPFN — but the SHAP library's plotting ecosystem | ||
| (``shap.plots.waterfall``, ``beeswarm``, ``summary``, ``dependence``, etc.) | ||
| is mature and widely used. This module bridges the two. | ||
|
|
||
| The ``shap`` package is **not** part of the ``interpretability`` extra. Install | ||
| it separately (``pip install shap``) if you want to use these helpers. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| import numpy as np | ||
|
|
||
| if TYPE_CHECKING: | ||
| import shap | ||
| from numpy.typing import ArrayLike | ||
|
|
||
|
|
||
| def shapiq_to_shap_explanation( | ||
| explainer: Any, | ||
| X: ArrayLike, | ||
| *, | ||
| budget: int, | ||
| feature_names: list[str] | None = None, | ||
| ) -> shap.Explanation: | ||
| """Compute first-order Shapley values with a shapiq explainer for each | ||
| row in ``X`` and wrap them in a ``shap.Explanation`` ready for use with | ||
| ``shap.plots.*`` and ``shap.summary_plot``. | ||
|
|
||
| Mirrors the pattern in ``examples/interpretability/shap_example.py``: | ||
| one ``.explain(...)`` call per row, stack the first-order arrays into an | ||
| ``(n, d)`` matrix, average baseline values, and pass everything to | ||
| ``shap.Explanation``. | ||
|
|
||
| Args: | ||
| explainer: A shapiq explainer — e.g. one returned by | ||
| ``get_tabpfn_imputation_explainer(..., index="SV", max_order=1)``. | ||
| X: ``(n, d)`` array of rows to explain. | ||
| budget: Number of model evaluations shapiq is allowed per row. For | ||
| small ``d`` and exact Shapley values, pass ``2**d``. | ||
| feature_names: Optional list of feature name strings (length ``d``). | ||
| Used by ``shap.plots.*`` for axis labels. | ||
|
|
||
| Returns: | ||
| A ``shap.Explanation`` with ``values.shape == (n, d)``. | ||
|
|
||
| Notes: | ||
| Only first-order Shapley values are wrapped. ``shap.Explanation`` | ||
| doesn't represent higher-order interactions; for those, use | ||
| shapiq's native plots on the ``InteractionValues`` object. | ||
|
|
||
| Requires ``shap`` to be installed (``pip install shap``). It is | ||
| kept out of the ``interpretability`` extra by design — shapiq is | ||
| the runtime dependency, shap is opt-in for plotting. | ||
| """ | ||
| import shap | ||
|
|
||
| X_arr = np.asarray(X) | ||
| n = len(X_arr) | ||
| ivs = [explainer.explain(x=X_arr[i], budget=budget) for i in range(n)] | ||
| values = np.stack([iv.get_n_order_values(1) for iv in ivs]) | ||
| # Pass per-row baselines through unchanged. For the imputation path the | ||
| # background is fixed and these are all the same value; for the Rundel | ||
| # remove-and-recontextualize path baselines genuinely vary per row. | ||
| # shap.Explanation accepts a 1-d (n,) array natively. | ||
| base_values = np.array([iv.baseline_value for iv in ivs]) | ||
| return shap.Explanation( | ||
| values=values, | ||
| base_values=base_values, | ||
| data=X_arr, | ||
| feature_names=feature_names, | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.