TreeSHAP efficiently computes SHAP values for tree-based models, showing how each feature contributes to an individual prediction. This supports model debugging, feature engineering, interpretability, bias investigation, and conversations with stakeholders or regulators. section 7.3
SHAP values distribute a prediction’s feature credit using Shapley values from cooperative game theory. Rather than evaluating every feature combination, SHAP approximates contributions through resampling; tree implementations make this practical for models such as XGBoost and LightGBM. section 7.3
A practical TreeSHAP workflow includes:
- Retrieve the trained tree model and transform input data through the existing preprocessing pipeline. section 7.3
- Generate per-feature contributions, compare them with predictions, and inspect global importance or violin plots. section 7.3
- Package feature values and SHAP values as JSON to create readable explanations for individual predictions. section 7.3
Listing 7.19 SHAP values as an XGBoost output
from XGBoost import DMatrix
booster = model_pipeline['xgb'].get_booster() #1
Xt = model_pipeline['processing'].transform(X) #2
feature_names = (
model_pipeline['processing']
.get_feature_names_out()
) #3
Xd = DMatrix(Xt) #4
shap_values = booster.predict(Xd, pred_contribs=True) #5
preds = booster.predict(Xd) #6
Inspect the XGBoost implementation that extracts transformed features, constructs a DMatrix, and requests prediction contributions. section 7.3

