Repository navigation
feat: add waterfall plot for local explanations (#453) - #698
laasyapriya2007 wants to merge 4 commits into
Conversation
Add plot_waterfall in interpret.visual.plot and support plot_type='waterfall' in local explanation visualize() and show(). - Implements plot_waterfall to visualize instance score breakdown from intercept to final prediction. - Supports both horizontal and vertical orientations and configurable top_n with remaining features aggregation. - Extends FeatureValueExplanation, EBMExplanation, and LinearExplanation visualize() to support plot_type='waterfall'. - Adds unit tests for regression, binary, and multiclass waterfall visualizations. Signed-off-by: Laasya Priya <laasyapriya1234@gmail.com>
for more information, see https://pre-commit.ci
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #698 +/- ##
==========================================
+ Coverage 67.80% 67.94% +0.13%
==========================================
Files 77 77
Lines 11893 11969 +76
==========================================
+ Hits 8064 8132 +68
- Misses 3829 3837 +8 Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved issues break existing preserve() calls, prevent waterfall rendering through the default show() path, and produce incomplete or inaccurate waterfall summaries and prediction totals.
5 open issues
Avoid passing plot_type to unsupported visualize methods · New Normalize waterfall title default to include prediction summary · New Propagate plot options through Dash rendering · New Preserve nonzero waterfall remainders without absolute cutoff · New Include init_score in local explanation waterfall totals · New
What changed in this PR
Adds waterfall plots for local feature attributions, addressing #453 by showing the intercept, individual contributions, and their accumulated total within the existing explanation visualization API.
Changes:
- Adds a Plotly waterfall helper with horizontal/vertical layouts, top-term grouping, and multiclass selection.
- Extends explanation APIs and inline/preserve providers to accept waterfall selection.
- Adds tests for plotting options and EBM/linear-model integration.
| File | Description |
|---|---|
python/interpret-core/tests/visual/test_plot.py |
Tests waterfall layouts, grouping, class selection, and model integration. |
python/interpret-core/interpret/visual/plot.py |
Implements the waterfall plotting helper. |
python/interpret-core/interpret/visual/_inline.py |
Forwards visualization options to explanation rendering. |
python/interpret-core/interpret/provider/_visualize.py |
Adds plot selection to preserve and option forwarding to inline rendering. |
python/interpret-core/interpret/glassbox/_linear.py |
Forwards plot options from linear explanations. |
python/interpret-core/interpret/glassbox/_ebm.py |
Integrates waterfall rendering into EBM local explanations. |
python/interpret-core/interpret/core/templates.py |
Routes local waterfall requests to the plotting helper. |
Review effort: Balanced
|
|
||
| # Get visual object | ||
| visual = explanation.visualize(key=key) | ||
| visual = explanation.visualize(key=key, plot_type=plot_type) |
| # Handle local instance graphs | ||
| if self.explanation_type == "local": | ||
| if plot_type == "waterfall": | ||
| return plot_waterfall(data_dict, title=title, **kwargs) |
| default_key=key, | ||
| detected_envs=self.detected_envs, | ||
| js_url=self.js_url, | ||
| **kwargs, |
| if top_n is not None and len(pairs) > top_n: | ||
| top_pairs = pairs[:top_n] | ||
| remaining_sum = sum(s for _, s in pairs[top_n:]) | ||
| if abs(remaining_sum) > 1e-9: |
| step_names = [intercept_name] + [p[0] for p in pairs] + ["Prediction"] | ||
| step_scores = [intercept] + [p[1] for p in pairs] + [0.0] | ||
| measures = ["relative"] * (len(step_names) - 1) + ["total"] |


Description
This PR resolves #453 by adding support for waterfall plots in local explanations.
Waterfall charts are one of the most effective and widely-used ways to visualize local additive feature attributions (similar to SHAP waterfall plots), clearly illustrating how the prediction is obtained starting from the base value (
Intercept), through individual feature score contributions, to the final modelPrediction.Key Changes
plot_waterfallininterpret.visual.plot:go.Waterfallcharts for local explanation data dictionaries.Interceptas the baseline, cascades each sorted feature score contribution, and terminates with atotalbar forPrediction.orientation="h", default) and vertical (orientation="v") orientations.top_n(defaults to 15), automatically aggregating the remaining lower-magnitude terms into an"Other features"step.class_idxselection).visualizeandshow):FeatureValueExplanation.visualize(key, plot_type="waterfall")EBMExplanation.visualize(key, plot_type="waterfall")LinearExplanation.visualize(key, plot_type="waterfall")interpret.show(local_exp, key=0, plot_type="waterfall")and inline / preserve providers.tests/visual/test_plot.pycovering basic waterfall generation, vertical layout,top_ngrouping, multiclass support, and model explanation visualization.How to Test