TreeExplainer
expected value
training data
machine learning
model interpretation

Why I get different expected_value when I include the training data in TreeExplainer?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In SHAP, the expected_value is not a fixed property of the model alone. It is the model's baseline output under the background distribution used by the explainer, so when you include the training data or switch explainer settings, you can legitimately get a different expected value.

What expected_value Means

SHAP explanations decompose a prediction into:

prediction = expected_value + sum_of_feature_contributions

That baseline term is the average output the explainer treats as "normal" before any specific feature effects are added.

For tree models, TreeExplainer can define that baseline in different ways depending on:

  • whether you pass background data explicitly
  • which feature_perturbation mode is being used
  • whether the model output is raw score, probability, or something else

So the baseline is not just "the model intercept" in a simple linear-model sense.

Why Including Training Data Changes It

When you pass background data to TreeExplainer, you are telling SHAP what data distribution to average over. If that background dataset changes, the expected model output changes too.

A small example makes this clear:

python
1import shap
2from sklearn.datasets import load_breast_cancer
3from sklearn.ensemble import RandomForestClassifier
4from sklearn.model_selection import train_test_split
5
6X, y = load_breast_cancer(return_X_y=True, as_frame=True)
7X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
8
9model = RandomForestClassifier(random_state=42)
10model.fit(X_train, y_train)
11
12explainer_train = shap.TreeExplainer(model, X_train)
13explainer_test = shap.TreeExplainer(model, X_test)
14
15print(explainer_train.expected_value)
16print(explainer_test.expected_value)

If X_train and X_test have slightly different feature distributions, the expected output under those distributions will also differ.

That is not an error. It is the explainer doing exactly what it was asked to do.

Background Data Defines the Reference Distribution

The most important mental model is this: SHAP explanations are relative to a reference distribution.

If the background dataset is:

  • the full training set, the baseline reflects the model's average output over training-like data
  • a small sample, the baseline reflects that sample instead
  • a filtered subset, the baseline reflects that subgroup only

This means two valid explainers can produce different expected values for the same model because they answer slightly different reference questions.

Tree Path Dependent Versus Interventional Behavior

TreeExplainer also has behavior differences related to how it handles feature dependence.

With tree-path-dependent logic, the explainer can rely on the training counts recorded in the fitted trees. With interventional logic, the background data you pass plays a more direct role in the expected value and attribution calculation.

That means changing:

  • the background dataset
  • the feature_perturbation mode

can both shift the expected value.

Here is a small example showing explicit configuration:

python
1explainer = shap.TreeExplainer(
2    model,
3    X_train,
4    feature_perturbation="interventional"
5)
6
7print(explainer.expected_value)

If you build another explainer with a different dataset or mode, the baseline may move.

Model Output Scale Also Matters

Tree models often have more than one prediction scale:

  • raw score or margin
  • probability
  • log loss or another transformed output in some contexts

If you explain raw scores in one run and probabilities in another, the expected value can differ even when the background dataset stays the same.

python
proba = model.predict_proba(X_test[:5])[:, 1]
print(proba)

For classification models in particular, it is easy to mix up:

  • expected value in raw tree-output space
  • expected value in probability space

Those are not numerically identical quantities.

A Small Diagnostic Pattern

When you see a surprising change in expected_value, check these variables first:

python
1print(type(explainer.expected_value))
2print(len(X_train), len(X_test))
3print(X_train.mean().head())
4print(X_test.mean().head())

Then verify:

  • which dataset was passed to TreeExplainer
  • which output space is being explained
  • whether the explainer settings changed between runs

Most confusion comes from one of those three factors.

Which Background Data Should You Use

For many tabular workflows, a representative sample of training data is a sensible baseline because it reflects what the model learned from.

But sometimes a narrower background is exactly what you want. For example, if you want to explain predictions relative to a business subgroup, using subgroup-specific background data may be appropriate.

The key is consistency. Interpretations only compare cleanly when the same reference distribution and output definition are used.

Common Pitfalls

A common mistake is assuming expected_value should be identical no matter what data you pass to TreeExplainer. It should not.

Another issue is mixing probability explanations and raw-score explanations without realizing the output scale changed.

Developers also often compare explainers built with different background samples and then conclude SHAP is unstable. In reality, the reference distribution changed.

Finally, using a tiny or biased background sample can make the baseline less representative than intended.

Summary

  • 'expected_value depends on the background distribution used by the explainer.'
  • Including training data changes that reference distribution, so the baseline can change.
  • TreeExplainer settings such as feature_perturbation also affect the result.
  • Raw-score and probability explanations can have different expected values.
  • Use a consistent background dataset and output scale when comparing SHAP results.

Course illustration
Course illustration

All Rights Reserved.