SHAP (SHapley Additive exPlanations) explains how a model arrives at individual predictions by assigning each feature a contribution value. Based on Shapley values from cooperative game theory, it distributes feature credit or importance across a prediction. For tabular models, these explanations can reveal how features are being used, challenge earlier assumptions, and identify features that are unhelpful or detrimental. section 7.3
Use SHAP after training to make a complex model easier to discuss with domain experts and business stakeholders. Explanations can support trust and transparency needs in regulated settings, and can help inspect possible bias in model decision-making. They complement exploratory data analysis: EDA suggests how data may be useful, while SHAP shows how the fitted model used it. section 7.3
For XGBoost or LightGBM models, obtain and inspect SHAP values through a practical workflow:
- Generate contribution values directly from a trained booster with prediction contributions enabled; XGBoost and LightGBM can produce them natively. section 7.3 section 7.4
- Check that the SHAP values reconstruct predictions in the model’s output representation; the illustrated regression output treats them as prediction multipliers. section 7.3
- Use a bar importance plot to rank influential features, or a violin plot to inspect the distribution of contributions across observations. section 7.3 section 7.4
- Package a selected prediction, its original feature values, and its SHAP values into JSON when an explanation must be passed into a prompt or another interface. section 7.3
Explaining a tabular regression prediction
Transform the input features with the fitted preprocessing pipeline, create an XGBoost DMatrix, and call the booster with pred_contribs=True. The resulting contribution array can be paired with transformed feature names, summarized in plots, or serialized with one prediction’s original values and SHAP values for a narrative explanation. 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
This listing shows the minimal XGBoost path: retrieve the booster, transform features, create a DMatrix, then generate both SHAP contributions and predictions. section 7.3




