Add scatter plot visualization for interaction values#516
Open
Add scatter plot visualization for interaction values#516
Conversation
Introduces shapiq.scatter_plot, a SHAP-style scatter (dependence) plot that works for both first-order Shapley values and higher-order interactions. For higher-order interactions the x-axis is restricted to a single feature in the interaction tuple (defaulting to the first, overridable via x_feature), matching the design discussed in the issue. https://claude.ai/code/session_01BebyCFsKiVH49mUQejbMCd
Apply ruff-format collapses for lines that fit, simplify a redundant if/elif branch, and switch isinstance() tuples to PEP 604 union syntax to satisfy UP038 (matches the rest of the codebase). https://claude.ai/code/session_01BebyCFsKiVH49mUQejbMCd
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Adds tests for the four error paths flagged by codecov: TypeError on non-tuple interaction (float, list), empty interaction tuple, tuple of unsupported feature type (float), and the "no non-empty interactions" fallback when interaction=None on an InteractionValues that contains only the empty coalition. https://claude.ai/code/session_01BebyCFsKiVH49mUQejbMCd
…s, max_order=2 Review fixes for the scatter plot: - y-values now go through iv[interaction_tuple] (canonical InteractionValues __getitem__ contract: missing interactions resolve to 0.0) instead of raw iv.dict_values[interaction_tuple] dict access, which would have raised KeyError on any sample whose lookup didn't contain the requested tuple. - Pre-flight 'not found in InteractionValues lookup' guard now checks the union of lookups across all samples instead of only sample 0, so a heterogeneous list with the interaction missing only from index 0 no longer falsely rejects a valid request. - Docstring updated to describe the broadened guard. Example script (examples/visualization/plot_scatter.py) bumped from 20 to 200 explained instances and from max_order=3 to max_order=2 so the scatter distribution is meaningful while staying around ~6s end-to-end in the docs build.
The y-axis previously read 'SHAP value' for first-order interactions,
which is misleading whenever the user picked a non-SV index (FSII, k-SII,
STII, ...), and inconsistent with the higher-order branch's 'Interaction
value: A x B' format.
Replace both with a single format derived from the actual index attribute:
{index}({feature_a}, {feature_b}, ...)
So a first-order FSII run on MedInc reads 'FSII(MedInc)' instead of
'SHAP value', and a pair reads 'FSII(MedInc, Latitude)'. Update the
existing test assertion accordingly.
Signed-off-by: Maximilian <maximilian.muschalik@gmail.com>
Owner
Author
|
Add a one-liner to the changelog |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation and Context
This PR adds a new
scatter_plotfunction to visualize per-sample interaction values against feature values. This is inspired by SHAP's scatter plot but extended to support higher-order interactions. For first-order interactions, it matches SHAP's behavior; for higher-order interactions, the x-axis is restricted to a single feature from the interaction tuple.The implementation includes:
Public API Changes
New public function:
shapiq.scatter_plot()- Main plotting function exported fromshapiq.plotmodule and top-levelshapiqnamespaceHow Has This Been Tested?
Comprehensive unit tests added in
tests/shapiq/tests_unit/tests_plots/test_scatter.pycovering:interaction=NoneExample notebook added demonstrating typical usage patterns.
Checklist
CHANGELOG.md(not visible in diff, may be separate).__init__.py).