Understanding SHAP: A Complete Guide

What is SHAP?

SHAP (SHapley Additive exPlanations) explains machine learning predictions by decomposing them into contributions from each feature. It's based on game theory - specifically Shapley values from cooperative game theory.

Step-by-Step Understanding

Step 1: The Basic Question

Given a model prediction, SHAP answers: "How much did each feature contribute to this specific prediction?"

Step 2: The Decomposition

Every prediction is broken down as:

f(x) = baseline + A_contribution + B_contribution + C_contribution

Where:

Step 3: The Baseline

The baseline is the arithmetic mean of all training predictions:

baseline = (1/n) Γ— sum of all model predictions on training data

This is what the model predicts "on average".

πŸ”‘ Key Insight: The baseline is the same for all explanations. Only the feature contributions change from prediction to prediction.

Step 4: How to Get SHAP Values for Each Feature

To get the SHAP value for feature A:

  1. A alone: f({A}) - f({}) = "How much does A help vs nothing?"
  2. A with B: f({A,B}) - f({B}) = "How much does A help when B is present?"
  3. A with C: f({A,C}) - f({C}) = "How much does A help when C is present?"
  4. A with B,C: f({A,B,C}) - f({B,C}) = "How much does A help when B,C are present?"
  5. Average these 4 numbers = SHAP value for A

To get the SHAP value for feature B:

  1. B alone: f({B}) - f({}) = "How much does B help vs nothing?"
  2. B with A: f({A,B}) - f({A}) = "How much does B help when A is present?"
  3. B with C: f({B,C}) - f({C}) = "How much does B help when C is present?"
  4. B with A,C: f({A,B,C}) - f({A,C}) = "How much does B help when A,C are present?"
  5. Average these 4 numbers = SHAP value for B

To get the SHAP value for feature C:

  1. C alone: f({C}) - f({}) = "How much does C help vs nothing?"
  2. C with A: f({A,C}) - f({A}) = "How much does C help when A is present?"
  3. C with B: f({B,C}) - f({B}) = "How much does C help when B is present?"
  4. C with A,B: f({A,B,C}) - f({A,B}) = "How much does C help when A,B are present?"
  5. Average these 4 numbers = SHAP value for C

Linear Models: The Simple Case

Why Linear Models Are Special

For linear models, we don't need the complex averaging process! There's a direct formula.

Linear Model:

f(x) = coeff_A Γ— A + coeff_B Γ— B + coeff_C Γ— C + intercept

SHAP Decomposition:

How We Get the Linear Formula: Step by Step

Remember the general SHAP process? For feature A, we need to calculate:

  1. f({A}) - f({})
  2. f({A,B}) - f({B})
  3. f({A,C}) - f({C})
  4. f({A,B,C}) - f({B,C})
  5. Average these 4 contributions

Let's work this out for a linear model!

Model: f(A,B,C) = coeff_A Γ— A + coeff_B Γ— B + coeff_C Γ— C + intercept

Step 1: Calculate each contribution

Contribution 1: f({A}) - f({})

Contribution 2: f({A,B}) - f({B})

Contribution 3: f({A,C}) - f({C})

Contribution 4: f({A,B,C}) - f({B,C})

Step 2: Average the contributions

A_contribution = (1/4) Γ— [coeff_A Γ— (A - mean(A)) + coeff_A Γ— (A - mean(A)) + coeff_A Γ— (A - mean(A)) + coeff_A Γ— (A - mean(A))]
= (1/4) Γ— [4 Γ— coeff_A Γ— (A - mean(A))]
= coeff_A Γ— (A - mean(A))
🎯 Amazing! All 4 contributions are identical for linear models, so the average is just coeff_A Γ— (A - mean(A))!

Why All 4 Contributions Are The Same

In every case, we're computing:

This is why linear models are special: Feature A's contribution doesn't depend on what other features are present!

Step-by-Step Linear Example

Model: Price = 50 Γ— bedrooms + 100 Γ— bathrooms + 200 Γ— garage + 300

Training Data Averages:

House to Explain:

Model Prediction:

Price = 50Γ—3 + 100Γ—2 + 200Γ—1 + 300 = 150 + 200 + 200 + 300 = 850

SHAP Breakdown:

Verification:

725 + 25 + 20 + 80 = 850 βœ“

Interpretation:

πŸ”‘ Key Insight: For linear models, SHAP is just coefficient Γ— (actual_value - average_value). The coefficient tells you the rate of change, and the distance from average tells you how much to apply it!

When computing f({A}), what do we do with B and C?

We have 3 options:

CRITICAL: Whatever option you choose, use it consistently for ALL calculations!

Application to Different Models

Linear Models

Model: f(x) = coeff_A Γ— A + coeff_B Γ— B + intercept

SHAP Calculation (Simple!):

  • Baseline: coeff_A Γ— mean(A) + coeff_B Γ— mean(B) + intercept
  • A contribution: coeff_A Γ— (A - mean(A))
  • B contribution: coeff_B Γ— (B - mean(B))

Example:

Model: Price = 3Γ—bedrooms + 50Γ—sqft + 100

Training averages: bedrooms=2.5, sqft=15

House: 3 bedrooms, 20 sqft

Prediction: 3Γ—3 + 50Γ—20 + 100 = 1109

SHAP breakdown:

  • Baseline: 3Γ—2.5 + 50Γ—15 + 100 = 857.5
  • Bedrooms contribution: 3Γ—(3-2.5) = 1.5
  • Sqft contribution: 50Γ—(20-15) = 250
  • Check: 857.5 + 1.5 + 250 = 1109 βœ“

Nonlinear Models

Model: Any complex model (trees, neural networks, etc.)

SHAP Calculation (Complex!):

Use the step-by-step process from Step 4 above, with approximation methods:

  • TreeSHAP: Fast and exact for tree-based models
  • KernelSHAP: Sampling approximation for any model
  • DeepSHAP: For neural networks

Example with Random Forest:

Model: Complex ensemble of decision trees

House: 3 bedrooms, 20 sqft

Prediction: $1,200 (from complex tree logic)

SHAP breakdown:

  • Baseline: $950 (average of all training predictions)
  • Bedrooms contribution: +$180 (calculated via TreeSHAP)
  • Sqft contribution: +$70 (calculated via TreeSHAP)
  • Check: $950 + $180 + $70 = $1,200 βœ“

Key Differences Between Linear and Nonlinear

Linear Models

Nonlinear Models

The Universal SHAP Process

For ANY model type:

  1. Train your model (linear, tree, neural network, etc.)
  2. Calculate baseline: Average all training predictions
  3. For each data point to explain:
    • Calculate A_contribution using the method above
    • Calculate B_contribution using the method above
    • Calculate C_contribution using the method above
  4. Verify: baseline + A_contribution + B_contribution + C_contribution = actual prediction
🎯 Bottom Line: SHAP tells you exactly how much each feature A, B, C contributed to each prediction!

What SHAP Values Tell You

What you can learn:

What you cannot learn:

πŸ”¬ Remember: SHAP gives you baseline + A_contribution + B_contribution + C_contribution = prediction. One baseline + one value per feature per data point!