Add basic xgboost.interpret.shap_values API#12208
Conversation
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.
|
|
||
| config = booster.save_config() | ||
| try: | ||
| booster.set_param({"device": device}) |
There was a problem hiding this comment.
I'm not sure if we should add a new code path to set the device. This setter makes the function mutable
There was a problem hiding this comment.
Yeah I don't like the fact that its mutable but if we don't do this its not clear that its GPU accelerated and its unclear how that might work. I'd like to keep it as optional arg but document.
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.