Skip to content

Add basic xgboost.interpret.shap_values API#12208

Merged
RAMitchell merged 6 commits into
dmlc:masterfrom
RAMitchell:codex/interpret-basic
May 28, 2026
Merged

Add basic xgboost.interpret.shap_values API#12208
RAMitchell merged 6 commits into
dmlc:masterfrom
RAMitchell:codex/interpret-basic

Conversation

@RAMitchell

Copy link
Copy Markdown
Member

Summary

Adds an initial xgboost.interpret module with a basic shap_values function.

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

  • Add xgboost.interpret.shap_values
  • Accept either a Booster or sklearn-style XGBoost model
  • Accept DMatrix or array-like inputs
  • Return feature SHAP values without the bias column by default
  • Support return_bias=True to return (values, bias)
  • Support optional temporary device= override, restoring the booster config after prediction
  • Reject X_background for now, since interventional SHAP is not implemented yet
  • Add focused Python tests

Notes

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

PYTHONPATH=/home/nfs/rorym/xgboost-wt/interpret-basic/python-package \
  conda run -n xgboost python -m pytest tests/python/test_interpret.py

Result: 5 passed

Pre-commit passed during commit, including ruff, ruff format, and pylint.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py with a first-pass shap_values() API (bias-column handling, optional device override, background data explicitly unsupported for now).
  • Export the new interpret module from xgboost.__init__ for from 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 thread python-package/xgboost/interpret.py Outdated
Comment thread python-package/xgboost/interpret.py
Comment thread python-package/xgboost/interpret.py Outdated
@RAMitchell
RAMitchell marked this pull request as ready for review May 13, 2026 11:46
@RAMitchell
RAMitchell requested a review from trivialfis May 13, 2026 11:46
Comment thread python-package/xgboost/interpret.py Outdated

config = booster.save_config()
try:
booster.set_param({"device": device})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should add a new code path to set the device. This setter makes the function mutable

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread python-package/xgboost/interpret.py Outdated
Comment thread python-package/xgboost/interpret.py Outdated
Comment thread python-package/xgboost/interpret.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

Comment thread python-package/xgboost/interpret.py
Comment thread python-package/xgboost/interpret.py Outdated
Comment thread python-package/xgboost/interpret.py
Comment thread python-package/xgboost/interpret.py Outdated
@RAMitchell
RAMitchell merged commit 60ae480 into dmlc:master May 28, 2026
81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants