How to Interpret a Model with SHAP in Python: Values, Summary Plot and Dependence Plot
SHAP values measure how much each feature pushed a prediction up or down, and in Python the shap library produces them in a few lines. The flow is always the same: keep the trained model, pick the right explainer, compute the values, then read the summary plot and the dependence plot. This page walks through that path step by step, what each chart answers, and where the reading usually goes wrong.
What are SHAP values and why do they explain a model?
SHAP comes from Shapley values, a concept from cooperative game theory. The idea is to split a prediction among the features, as if each one were a player contributing a share of the result.
What makes the method useful is additivity: the model's base value plus the contributions of every feature adds up exactly to the prediction for that row. That gives two readings at once — a global one (the average weight of each feature across the dataset) and a local one (why that specific row received that prediction).
The cost is computational. For tree models, TreeExplainer handles the calculation efficiently; for generic models, KernelExplainer works but is slower and usually requires sampling.
How do you generate SHAP values in Python in practice?
The minimum path has five steps and does not require changing the model you already trained.
An example with Random Forest looks like this:
import shap explainer = shap.TreeExplainer(model) shap_values = explainer.shap_values(X) shap.summary_plot(shap_values, X) shap.dependence_plot('feature_name', shap_values, X)
Check the base value before interpreting anything. If the sum of the contributions does not match the model's prediction, the explainer does not correspond to the model you are looking at.
For classifiers, shap_values can come back as a list with one array per class. Pick the class you care about before plotting, or the summary plot will mix contributions from different outputs.
- Install the library with pip install shap and import it in the notebook.
- Keep the already fitted model and the X matrix used at prediction time.
- Choose the right explainer: TreeExplainer for decision tree, Random Forest, Gradient Boosting, XGBoost and LightGBM; LinearExplainer for linear models; KernelExplainer as the generic fallback.
- Compute the values with explainer.shap_values(X) or by calling the explainer directly.
- Store the result in a DataFrame with the column names, so you do not lose track of which feature is which.
How do you read a summary plot and what does it reveal about the features?
The summary plot answers a specific question: which features carry the most weight in the model as a whole, and in which direction. Each dot is a row of the dataset, the horizontal axis is the SHAP value and the color represents the feature's value. Features are ordered by the mean absolute contribution.
Read the color together with the position. Red dots on the right mean that high values of that feature push the prediction up; blue dots on the right mean the opposite. If the colors mix on both sides, the feature's effect depends on another variable or is not monotonic.
There is a bar version, with plot_type='bar', that shows only the mean absolute value. It is easier to communicate, but it loses the direction of the effect.
The summary plot does not prove causality and does not certify the quality of the model. It describes the behavior of the trained model, including the biases it learned.
How do you use a dependence plot to understand a single feature's effect?
The dependence plot swaps the global view for a single-feature view: the feature's value on the horizontal axis, its SHAP value on the vertical axis. It is the chart that shows whether the effect is linear, stepped or U-shaped.
The call is direct: shap.dependence_plot('feature_name', shap_values, X).
The automatic color highlights a second feature that the first one appears to interact with. Interpret with caution: the color may reflect only correlation between features, not a real interaction. To check, choose the interaction feature manually and compare the result on data held out from training.
A good practice is to generate the dependence plot for every feature that appeared at the top of the summary plot. The combined reading answers what the global ranking alone cannot: in which range of values the feature actually changes the prediction.
What are the common mistakes when interpreting SHAP and how do you avoid them?
Most mistakes are not in the code, they are in the conclusion drawn from the chart. SHAP explains the model, not the world: a feature with high weight may only be capturing a correlated variable that was left out.
Another frequent confusion is comparing SHAP values across models trained on different data or with different feature sets. The base value changes, the scale changes, and the comparison stops meaning anything. Compare contributions within one model, or retrain both models on the same data and features before putting the charts side by side.
Two practical traps: interpreting on training data only, which hides overfitting, and running KernelExplainer on a large dataset without sampling, which can take far longer than the modeling itself. Tree explainers handle the full dataset, but plots get crowded — a sample of rows is enough to read the pattern.
Finally, SHAP sits on top of solid fundamentals. You need a fitted model, correct preprocessing and a validation strategy before any explanation makes sense. The Python for Data Science and Analytics course at python.paanalytics.net/ builds that base from scratch — NumPy and Pandas, data cleaning and outlier detection, then regression, Random Forest, Gradient Boosting, XGBoost and LightGBM, feature selection and cross-validation, closing with an end-to-end machine learning project. It costs R$ 470, or 12 installments of R$ 20,37 on the card, paid through the Hotmart checkout, with lifetime access, a certificate and a community for networking.
| Explainer | Best for | Cost on large data |
|---|---|---|
| TreeExplainer | Decision tree, Random Forest, Gradient Boosting, XGBoost, LightGBM | Low — reads the tree structure directly |
| LinearExplainer | Linear and logistic regression | Low — closed-form for linear models |
| KernelExplainer | Any model, including SVM and Naive Bayes | High — usually needs sampling |
Explainer choices follow the model-specific design of the shap library; the model families listed are the ones covered in the course.
Build the Python and machine learning base that makes SHAP useful
Secure payment via Hotmart, up to 12 installments.
Enroll now Product siteFrequently asked questions
Do I need to retrain the model to use SHAP?
No. SHAP works on a model that is already fitted. You keep the trained object and the feature matrix used at prediction time, then call the explainer. Retraining is only needed if you change the model or the features.
Which explainer should I use for XGBoost, LightGBM or Random Forest?
TreeExplainer. It is built for tree ensembles and reads the structure of the model directly. For linear and logistic regression, use LinearExplainer. For SVM or Naive Bayes, KernelExplainer is the generic option, at the cost of being slower.
Can I compare SHAP values between two different models?
Only when both were trained on the same data with the same features and the same base value logic. Otherwise the scales and the reference point differ and the comparison is misleading. A safer approach is to compare rankings within each model and check whether the same features stay at the top.
Does SHAP prove that a feature causes the outcome?
No. SHAP describes how the model uses a feature, not how the world works. A high contribution can come from a correlated variable acting as a proxy. Causal claims need a separate design, such as an experiment or a causal model.
How is SHAP different from the model's built-in feature_importances_?
Built-in importances usually give one number per feature for the whole model, without direction and without a per-row explanation. SHAP gives a value per row per feature, so you can see both the global ranking and why a single prediction came out the way it did.