Skip to content
mentoor

Topic 5 of 6 · 4 min read

Explaining tree predictions with SHAP

Goal: Read permutation importance and SHAP values for a model and say which features drive one given prediction.

Permutation importance tells you which features a model relies on overall, and SHAP values tell you how each feature pushed one specific prediction up or down. Boosted trees are accurate but opaque, and these two tools open them up.

Check yourself · 1 of 3

What does a single SHAP value for one feature in one prediction tell you?

Choose one answer

Why it matters

Someone will ask why the model rejected this loan or flagged this order. "The algorithm said so" is not an answer, and in some domains an explanation is required. Explanations also catch bugs: if customer_id is the top feature, the model found a leak, not a pattern.

Permutation importance (global)

Shuffle one column in held-out data and measure how much the score drops. A big drop means the model leans on that column. It works for any model and is computed on the test or validation set, which keeps it honest. The default importances inside tree libraries are cheaper but can favour columns with many distinct values. Caveat: with two strongly correlated columns, shuffling one leaves the other to cover for it, so both can look unimportant.

SHAP values (per prediction)

A SHAP value assigns each feature a signed contribution to one prediction, relative to the model's average output. The contributions add up to the difference between this prediction and the average. For tree models, shap.TreeExplainer computes them exactly and quickly.

Reading one prediction: start at the base value (the average output), then add each feature's push. A row might read: base 0.20, late_payments=3 +0.25, income=82k -0.08, tenure=2 years -0.03, final 0.34.

import shap

explainer = shap.TreeExplainer(model)
sv = explainer(X_test.iloc[:200])
shap.plots.waterfall(sv[0])   # one row
shap.plots.beeswarm(sv)       # all rows, per feature

Before and after

Before: "Feature importance says tenure is top, so tenure causes churn." After: "tenure is the largest driver of this model's predictions on the test set. For this customer, 14 months of tenure added 0.12 to the churn score. The model is not evidence that changing tenure changes churn."

What goes wrong

  • Reading explanations as causes. They describe the model, not the world.
  • Computing importance on training data, where overfit features look important.
  • Ignoring correlated features, which share credit in SHAP and mislead permutation importance.
  • Trusting a pretty plot from an assistant without checking units: for a classifier, SHAP values may be in log-odds, not probability.

Examples

One loan decision

Feature Value SHAP
base value 0.20
late_payments 3 +0.25
income 82,000 -0.08
tenure_years 2 -0.03
final risk 0.34

Late payments are the reason this applicant scores above average. Income and tenure pushed the score down but not enough.

The leaky top feature

A churn model shows account_closed_date as the top feature by permutation importance. The column is only filled in after a customer has already left. Remove it and retrain; the score falls, but the model now works on live data.

Key points

  • Permutation importance shuffles a column in held-out data and measures the score drop, giving a global view for any model.
  • SHAP values split one prediction into signed feature contributions that sum from the average output to the final value.
  • TreeExplainer computes exact SHAP values for tree models quickly.
  • Explanations describe what the model uses, not what causes the outcome in the real world.
  • Correlated features share credit or hide each other, so read their importances together.
  • A surprising top feature such as an id column is often a sign of leakage.

Common mistakes

  • Computing permutation importance on the training set.
  • Presenting SHAP contributions as proof of cause.
  • Reading the sign of a SHAP value without checking whether it is in log-odds or probability.
  • Splitting credit by eye between two nearly identical features.

Check yourself · 2 of 3

How is permutation importance computed?

Choose one answer

Check yourself · 3 of 3

A churn model's top feature by permutation importance is days_since_cancellation_request. What is the most likely issue?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic