Add basic xgboost.interpret.shap_values API#12208
Draft
RAMitchell wants to merge 2 commits into
Draft
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new public Python entry point for interpretability by introducing xgboost.interpret.shap_values, initially implemented as a thin wrapper around Booster.predict(pred_contribs=True) and exposing a stable module/function surface for future interpretability features (per #11947).
Changes:
- Introduce
python-package/xgboost/interpret.pywith a first-passshap_values()API (bias-column handling, optional device override, background data explicitly unsupported for now). - Export the new
interpretmodule fromxgboost.__init__forfrom xgboost import interpret. - Add focused pytest coverage for API behavior and device override config restoration.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/python/test_interpret.py | Adds unit tests covering shap_values parity with pred_contribs, sklearn-model acceptance, background-data rejection, and config restoration. |
| python-package/xgboost/interpret.py | Implements the new xgboost.interpret.shap_values wrapper and related helpers. |
| python-package/xgboost/init.py | Exposes the new interpret module at the package top level and in __all__. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+28
to
+31
| if iteration_range is None: | ||
| get_iteration_range = getattr(model, "_get_iteration_range", None) | ||
| if get_iteration_range is not None: | ||
| return get_iteration_range(iteration_range) |
Comment on lines
+39
to
+42
| if isinstance(X, DMatrix): | ||
| if feature_names is not None: | ||
| X.feature_names = feature_names | ||
| return X |
Comment on lines
+64
to
+69
| config = booster.save_config() | ||
| try: | ||
| booster.set_param({"device": device}) | ||
| return booster.predict(data, **kwargs) | ||
| finally: | ||
| booster.load_config(config) |
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.
Summary
Adds an initial
xgboost.interpretmodule with a basicshap_valuesfunction.This is a small first step toward #11947. The implementation wraps the existing
Booster.predict(pred_contribs=True)path and keeps the behavior intentionally narrow while establishing the public module/function entry point.Changes
xgboost.interpret.shap_valuesBoosteror sklearn-style XGBoost modelDMatrixor array-like inputsreturn_bias=Trueto return(values, bias)device=override, restoring the booster config after predictionX_backgroundfor now, since interventional SHAP is not implemented yetNotes
This does not add generated docs yet. The function has a docstring, but a dedicated Sphinx page can follow once the initial API shape is agreed.
Testing
Result:
5 passedPre-commit passed during commit, including
ruff,ruff format, andpylint.