SHAP (SHapley Additive exPlanations) › Example: Walking Through the Formula

Michael BrenndoerferJuly 15, 20256 min read
Machine Learning from ScratchAll chapters
Part 6 of 9Explainability
Chapter 51SHAP (SHapley Additive exPlanations) › Example: Walking Through the Formula
Reading section 4 of 8 · Example: Walking Through the Formula · 6 minSection 4 of 8 · 6 min

Book contents

Section 4 of 8 · 6 min read

SHAP (SHapley Additive exPlanations) › Example: Walking Through the FormulaOne section · Sections in this chapter4 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Now that we've explored the theoretical foundation of SHAP, let's work through a concrete example that demonstrates how the formula translates into actual calculations. This walkthrough will make tangible all the abstract concepts we've discussed, including marginal contributions, weighting factors, and the efficiency property, showing how they work together in practice to produce fair, interpretable attributions.

We'll use a simple house price prediction model with three features, which is small enough to compute all subsets manually while still illustrating the key principles. This example will help you see how the formula's components combine in a real calculation, making the mathematical abstraction concrete and actionable.

Setting Up the Problem

Suppose we have a simple regression model that predicts house prices based on three features:

  • x1x_1: Square footage (in thousands)
  • x2x_2: Number of bedrooms
  • x3x_3: Age of house (in years)

Our model is: f(x1,x2,x3)=50x1+10x2−2x3+100f(x_1, x_2, x_3) = 50x_1 + 10x_2 - 2x_3 + 100

Notice that this is a linear model, which simplifies our calculations, but the SHAP approach works identically for any model type. For a specific house with x1=2.5x_1 = 2.5 (2,500 square feet), x2=3x_2 = 3 (3 bedrooms), and x3=10x_3 = 10 (10 years old), the prediction would be:

f(2.5,3,10)=50(2.5)+10(3)−2(10)+100=125+30−20+100=235f(2.5, 3, 10) = 50(2.5) + 10(3) - 2(10) + 100 = 125 + 30 - 20 + 100 = 235

Applying the Formula: Step by Step

To calculate SHAP values using our formula, we need to:

  1. Identify all possible subsets SS that don't include each feature
  2. Compute the marginal contribution [f(S∪{i})−f(S)][f(S \cup \{i\}) - f(S)] for each subset
  3. Apply the weighting factor ∣S∣!(∣F∣−∣S∣−1)!∣F∣!\frac{|S|!(|F|-|S|-1)!}{|F|!} to each marginal contribution
  4. Sum the weighted contributions to get the final SHAP value

Let's assume our baseline f(∅)=100f(\emptyset) = 100 (the prediction with no features). This baseline represents what the model would predict if it had no information about the house. Essentially, it's a starting point from which features add their contributions.

For feature x1x_1 (Square footage):

We need to consider all subsets that don't include x1x_1:

  • S=∅S = \emptyset: f(∅)=100f(\emptyset) = 100, f({x1})=50(2.5)+100=225f(\{x_1\}) = 50(2.5) + 100 = 225
    • Marginal contribution: 225−100=125225 - 100 = 125
  • S={x2}S = \{x_2\}: f({x2})=10(3)+100=130f(\{x_2\}) = 10(3) + 100 = 130, f({x1,x2})=50(2.5)+10(3)+100=255f(\{x_1, x_2\}) = 50(2.5) + 10(3) + 100 = 255
    • Marginal contribution: 255−130=125255 - 130 = 125
  • S={x3}S = \{x_3\}: f({x3})=−2(10)+100=80f(\{x_3\}) = -2(10) + 100 = 80, f({x1,x3})=50(2.5)−2(10)+100=205f(\{x_1, x_3\}) = 50(2.5) - 2(10) + 100 = 205
    • Marginal contribution: 205−80=125205 - 80 = 125
  • S={x2,x3}S = \{x_2, x_3\}: f({x2,x3})=10(3)−2(10)+100=110f(\{x_2, x_3\}) = 10(3) - 2(10) + 100 = 110, f({x1,x2,x3})=235f(\{x_1, x_2, x_3\}) = 235
    • Marginal contribution: 235−110=125235 - 110 = 125

The SHAP value for x1x_1 is the weighted average of these marginal contributions:

ϕ1=0!2!3!⋅125+1!1!3!⋅125+1!1!3!⋅125+2!0!3!⋅125=13⋅125+16⋅125+16⋅125+13⋅125=125\phi_1 = \frac{0!2!}{3!} \cdot 125 + \frac{1!1!}{3!} \cdot 125 + \frac{1!1!}{3!} \cdot 125 + \frac{2!0!}{3!} \cdot 125 = \frac{1}{3} \cdot 125 + \frac{1}{6} \cdot 125 + \frac{1}{6} \cdot 125 + \frac{1}{3} \cdot 125 = 125

For feature x2x_2 (Number of bedrooms):

  • S=∅S = \emptyset: Marginal contribution = 130−100=30130 - 100 = 30
  • S={x1}S = \{x_1\}: Marginal contribution = 255−225=30255 - 225 = 30
  • S={x3}S = \{x_3\}: Marginal contribution = 110−80=30110 - 80 = 30
  • S={x1,x3}S = \{x_1, x_3\}: Marginal contribution = 235−205=30235 - 205 = 30
ϕ2=13⋅30+16⋅30+16⋅30+13⋅30=30\phi_2 = \frac{1}{3} \cdot 30 + \frac{1}{6} \cdot 30 + \frac{1}{6} \cdot 30 + \frac{1}{3} \cdot 30 = 30

For feature x3x_3 (Age of house):

  • S=∅S = \emptyset: Marginal contribution = 80−100=−2080 - 100 = -20
  • S={x1}S = \{x_1\}: Marginal contribution = 205−225=−20205 - 225 = -20
  • S={x2}S = \{x_2\}: Marginal contribution = 110−130=−20110 - 130 = -20
  • S={x1,x2}S = \{x_1, x_2\}: Marginal contribution = 235−255=−20235 - 255 = -20
ϕ3=13⋅(−20)+16⋅(−20)+16⋅(−20)+13⋅(−20)=−20\phi_3 = \frac{1}{3} \cdot (-20) + \frac{1}{6} \cdot (-20) + \frac{1}{6} \cdot (-20) + \frac{1}{3} \cdot (-20) = -20

Verifying the Mathematical Guarantees

This calculation provides an opportunity to verify the efficiency property that we discussed earlier, the property that makes SHAP explanations complete and interpretable. Let's check:

ϕ1+ϕ2+ϕ3=125+30+(−20)=135\phi_1 + \phi_2 + \phi_3 = 125 + 30 + (-20) = 135 f(F)−f(∅)=235−100=135f(F) - f(\emptyset) = 235 - 100 = 135

The sum of all SHAP values exactly equals the difference between the full prediction and the baseline. This confirms two things: first, that our calculations are correct (a useful sanity check), and second, that the efficiency property holds in practice. Every unit of difference between baseline and prediction is accounted for. There's no missing contribution and no unexplained remainder. Just a complete decomposition that we can trust.

This property is what makes SHAP explanations actionable: if we start with the baseline (100) and add each feature's SHAP contribution in sequence, we reconstruct the exact prediction: 100+125+30+(−20)=235100 + 125 + 30 + (-20) = 235. This linear decomposition is exactly what the waterfall plot visualizes, and it's what makes SHAP explanations intuitive. Each feature's contribution is explicit and mathematically guaranteed to add up correctly.

Interpreting the Results

Now that we've calculated the SHAP values, what do they tell us?

  • Square footage contributes +125 to the prediction. This is by far the most important feature, accounting for most of the difference between baseline and final prediction. The positive contribution makes intuitive sense: larger houses cost more.

  • Number of bedrooms contributes +30 to the prediction. This is moderately important, with a positive effect that aligns with expectations that more bedrooms increase value.

  • Age of house contributes -20 to the prediction. This feature reduces the price, which matches our intuition that older houses tend to be worth less, all else equal.

The total contribution of all features (135) plus the baseline (100) equals the final prediction (235). These attributions reflect the model's structure (the coefficients in our linear model) and account for how features interact. The SHAP values emerge from considering all possible contexts, not just isolated contributions.