← All posts
Machine Learning · TreeSHAP · Part 1 of 5

TreeSHAP: Start with One Small Tree

Let’s use a small customer dataset to understand TreeSHAP. We will build a spending prediction from the table, then work out how each customer detail contributes to that prediction.

Start with ten customers

Here is our dummy data. Each row is one customer. The first three details—customer group, service plan, and usage—are the inputs. Monthly spending is the number we want to predict.

Ten customers and their monthly spending
NameGroupPlanUsageMonthly ($)
AshaNewBasicLow8
BenNewBasicHigh10
CaraNewBasicLow12
DevNewPremiumHigh30
EliReturningBasicLow45
FarahReturningPremiumLow55
GiaReturningBasicHigh80
HariReturningPremiumHigh85
IshaReturningBasicHigh95
JayReturningPremiumHigh100

Keep this table in mind as we go. Every count and average in the tree below comes from these ten rows.

Turn the table into a small prediction tree

Use customer group for the first split. That separates four new customers from six returning customers. Split the new customers by plan, and the returning customers by usage.

For each final group, predict its average monthly spending:

  1. New + Basic: Asha, Ben, and Cara spent 8, 10, and 12. Their average is (8 + 10 + 12) / 3 = 10.
  2. New + Premium: Dev is the only customer in this group. The prediction is 30.
  3. Returning + Low usage: Eli and Farah spent 45 and 55. Their average is (45 + 55) / 2 = 50.
  4. Returning + High usage: Gia, Hari, Isha, and Jay spent 80, 85, 95, and 100. Their average is (80 + 85 + 95 + 100) / 4 = 90.
Ten customers split into four New and six Returning. New customers split by plan into three Basic customers predicting $10 and one Premium customer predicting $30. Returning customers split by usage into two Low customers predicting $50 and four High customers predicting $90.
Each final box is a leaf. Its prediction is the average spending of the customers who reach it. The counts are the same 3, 1, 2, and 4 customers we just grouped. Select the figure to view it at full size.

Predict Maya’s spending

Maya is the customer we want to explain:

NameGroupPlanUsageMonthly ($)
MayaReturningPremiumHigh?

At the first split, Maya is a returning customer, so go right. Her usage is high, so go right again. The tree predicts $90 per month, the average for Gia, Hari, Isha, and Jay.

The tree reaches that prediction without checking Maya’s plan. Keep that observation in mind: we will soon see why her plan can still receive some credit in a SHAP explanation.

To keep the later calculations short, we will call customer group A, plan B, and usage C. The code uses 0 for New, Basic, and Low, and 1 for Returning, Premium, and High. Maya’s row is therefore A = 1, B = 1, C = 1.

What would we predict before knowing Maya’s details?

Start by averaging the tree’s predictions for the ten customers. Three receive a prediction of 10, one receives 30, two receive 50, and four receive 90:

(3 × 10 + 1 × 30 + 2 × 50 + 4 × 90) / 10 = 52.

This starting value is called the baseline. Maya’s prediction is 90, which is 38 above the baseline. TreeSHAP shares that extra 38 among her customer group, plan, and usage.

We will get there by revealing Maya’s details one at a time and watching the prediction change.

First reveal that Maya is a returning customer

We can now follow the returning-customer branch. But suppose we do not yet know her usage. Look back at the six returning customers: two have low usage and four have high usage.

Use those counts to average the two possible predictions:

(2 / 6) × 50 + (4 / 6) × 90 = 76⅔.

So learning that Maya is a returning customer moves the prediction from 52 to 76⅔. Next, reveal that her usage is high. We can now choose the 90 leaf directly, an increase of 90 − 76⅔ = 13⅓.

This gives usage a contribution of 13⅓ in this particular order. Revealing usage before customer group gives a different contribution. SHAP handles this by averaging over all possible reveal orders; we will calculate them in the next chapter.

Here is the rule we will reuse: when a detail is known, follow Maya’s value; when it is unknown, average both branches using their customer counts. For example, not knowing usage means averaging the Low and High branches, just as we did above. These counts are often called cover, and this way of calculating the averages is called tree-path-dependent.

Reveal Maya’s details yourself

Check a box to reveal that detail. The average prediction updates below, along with the share assigned to each leaf. All amounts are in dollars.

What do we know about Maya?

No details known: average prediction = 52.

Leaf $1030%
Leaf $3010%
Leaf $5020%
Leaf $9040%

Why does knowing only the plan give 58?

Try checking only Plan: Premium. There are two different splits to keep track of: New versus Returning at the top, and Basic versus Premium inside the New branch. Each split has its own counts.

First look inside the New branch. Asha, Ben, and Cara have Basic plans; Dev has Premium. Before we know Maya’s plan, we average both possibilities:

Plan unknown, inside the New branch:
(3 / 4) × 10 + (1 / 4) × 30 = 15.

After we learn that Maya has Premium, we can answer the plan question directly. Follow the Premium branch to its prediction of 30:

Premium known, inside the New branch:
0 × 10 + 1 × 30 = 30.

Dev being one of four customers gave us the 1/4 weight when the plan was unknown. Once Premium is known, we no longer average over Basic and Premium. The New branch’s result changes from 15 to 30.

Now return to the top of the tree. Maya’s customer group is still unknown. In this tree-path calculation, an unknown split keeps its original customer counts: four New and six Returning. We therefore combine:

Only plan known: (4 / 10) × 30 + (6 / 10) × 76⅔ = 58.

This is how plan can contribute even though Maya’s final route skips the plan split. It helps change the average while her customer group is still unknown.

The explanation we will calculate next

Starting average $52
+ customer group $24⅓
+ plan $3
+ usage $10⅔
= Maya’s prediction $90.

The three contributions add to the extra $38. The next chapter works through every reveal order to show exactly where $24⅓, $3, and $10⅔ come from.

Maya's waterfall starts at $52, adds about $24.33 for customer group, $3 for plan, and $10.67 for usage, and ends at $90.
Read from the baseline at the bottom to Maya’s prediction at the top. The bars show each input’s contribution. Select the figure to view it at full size.

A continuous route through the series

Read the next chapters in order for the derivation. If you already understand the algorithm, Part 7 contains a complete runnable example and the verification results.

  1. TreeSHAP: Start with One Small Tree — Start with ten dummy customers, build the tree’s predictions from their spending, and explain Maya’s $90 prediction.
  2. Calculate Tree SHAP Values by Hand — Work through all eight feature groups and all six reveal orders, including a feature outside the prediction path.
  3. How TreeSHAP Avoids Listing Every Feature Group — Follow Maya’s prediction from leaf weights to the TreeSHAP recurrence, repeated splits, and tree sums.
  4. Choose What a Hidden Feature Means — Keep Maya and the tree fixed while comparing stored branch counts with replacement from the same ten customers.
  5. Use TreeExplainer and Verify the Result — Run TreeExplainer on Maya and the original tree, then verify the baseline, prediction, and each feature contribution.

The algorithmic reference is Lundberg, Erion, and Lee’s Tree SHAP paper. The examples and figures here are original calculations; the downloadable scripts compare them with SHAP’s TreeExplainer. Start with the next chapter to see where every contribution comes from.