Repository navigation
fix: preserve neighbor order in stability explanations - #794
Conversation
milton-minervino
left a comment
There was a problem hiding this comment.
Thank you for this PR, this fixes real bugs: SHAP rows now follow the neighbourhood order (instance first, then by distance), tied distances can no longer push the selected row out of the top N, and duplicate feature rows no longer multiply rows.
Two small requests before merging:
- Can you please document the new
ValueErrorin theshap_neighborsdocstring by adding aRaisessection? Withneighbor_positions=Noneand duplicate feature rows it now raisesValueError, where it used to silently return too many rows. Please also note that rows now follow the neighborhood order (instance first). - Can you add tests for the paths the new tests don't cover?
find_neighborsin classification mode withreturn_positions=True. Assert thatpositions[i][0]is the selected row and thatlen(positions[i]) == len(neighbors[i])after the filters.Explainer.compute_features_stabilityend to end, for both thelen(selection) == 1branch (local_neighbors) and the multi-row branch (features_stability). Check thatamplitudematches the selected row's SHAP values.
|
Thanks for the review. I added the The two affected test files pass (73 tests), and the six stability plotting tests pass too. Ruff on the production file and |
milton-minervino
left a comment
There was a problem hiding this comment.
Thank you for the changes!
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #794 +/- ##
===========================================
+ Coverage 79.03% 79.13% +0.10%
===========================================
Files 59 59
Lines 7287 7305 +18
Branches 1534 1538 +4
===========================================
+ Hits 5759 5781 +22
+ Misses 951 947 -4
Partials 577 577
🚀 New features to boost your workflow:
|
Description
I fixed the row mix-up in feature stability calculations.
find_neighborsnow carries each selected row's position through the prediction and distance filters, andcompute_features_stabilitypasses those positions toshap_neighbors. This keeps the requested instance first, preserves distance order, and avoids multiplying rows when two observations have identical features.Direct calls to
shap_neighborswithout positions still work for unique feature rows. They now follow the supplied neighbourhood order; for duplicate feature rows, they raise a clear error instead of returning ambiguous results.Fixes: #777
Type of change
How Has This Been Tested?
uv run --no-sync python -m pytest tests/unit_tests/utils/test_explanation_metrics.py tests/unit_tests/explainer/test_smart_explainer.py -q— 86 passeduv run --no-sync python -m pytest tests/unit_tests/explainer/test_smart_plotter.py -k stability -q— 5 passeduvx ruff check shapash/utils/explanation_metrics.pyandgit diff --check— passedTest configuration: Windows, Python 3.11.9, Shapash source branch based on upstream
develop.The regression tests cover an instance later in the dataset than a neighbour, duplicate feature rows, distance ties that would otherwise push the selected row out of the initial slice, classification filtering with returned row positions, and both stability output branches.