skip to content

In shap 0.52's TreeExplainer, what does feature_perturbation="auto" resolve to, and what changes when you pass a background dataset?

level: middleimportance: should knowfreq 12%

answer

  1. the default changed in 0.47
  2. it depends on whether data arrives
  3. training cover counts versus background rows
  4. probability output needs the background path
  5. approximate moved from constructor to call

basics

~20 s

In shap 0.52, feature_perturbation="auto" means "interventional" when TreeExplainer gets a background and "tree_path_dependent" when it does not. A background makes the explainer integrate over those rows, which allows probability or log-loss outputs and costs time per row.

solid answer

~40 s

Since shap 0.47 the default is `feature_perturbation="auto"`, and in 0.52 it resolves when the explainer is built: "interventional" if `data` is given, "tree_path_dependent" if not. Tree-path-dependent needs no background; it uses how many training samples went down each branch, stored in the model, and only supports `model_output="raw"`. Interventional integrates over the background rows, its run time grows linearly with their number, and it is required for `model_output="probability"` or "log_loss". A DataFrame passed as `data` goes through the default `Independent` masker, so only 100 of its rows are kept unless you pass a masker with a larger `max_samples`. Passing "interventional" with no data does not fail in 0.52: it raises a `FutureWarning` and falls back to tree-path-dependent. Fast approximate values are requested on the call, `explainer(X, approximate=True)`.

code

python · 16 lines
python
import shap

# No background: "auto" resolves to tree_path_dependent
explainer = shap.TreeExplainer(quote_model)
print(explainer.feature_perturbation)   # tree_path_dependent

# A 20,000-row background: "auto" resolves to interventional,
# and the default Independent masker keeps only 100 rows
explainer = shap.TreeExplainer(quote_model, data=X_recent_loads)
print(explainer.feature_perturbation)   # interventional
print(explainer.data.shape[0])          # 100

# Keep 1,000 rows by building the masker yourself
masker = shap.maskers.Independent(X_recent_loads, max_samples=1000)
explainer = shap.TreeExplainer(quote_model, data=masker)
sv = explainer(X_quotes)

go deeper

for a junior

Recall the two modes: tree_path_dependent needs no background, interventional uses the background you pass, and "auto" picks between them based on whether data was given.

for a middle

Explain what each mode uses for a hidden feature, the 0.47 switch to "auto", the 100-row masker cut on a DataFrame background, and why probability output needs the interventional path.

for a senior

Show you would log the resolved mode, size the background with max_samples on purpose, and catch the FutureWarning fallback and constructor-side approximate in older pipelines before they mislead.

for a principal

Weigh a background-free explainer that is fast and stable against an interventional one tied to a chosen reference population, and decide who owns that background's definition.

## The argument and its default `shap.TreeExplainer(model, data=None, model_output="raw", feature_perturbation="auto")` explains tree ensembles. The **`feature_perturbation`** argument decides how the explainer treats a feature it is "hiding" when it computes an attribution. There are two real modes, and since shap 0.47 a third value, "auto", picks between them: | Value | Needs a background? | What stands in for a hidden feature | |---|---|---| | "tree_path_dependent" | no | the training samples that went down each branch, recorded in the fitted model | | "interventional" | yes | the rows of the background dataset you passed | | "auto" (default) | either | interventional if `data` is given, tree_path_dependent otherwise | Before 0.47 the default was "interventional"; the docstring records the switch to "auto" in 0.47. In shap 0.52, "auto" is resolved in the constructor, and `explainer.feature_perturbation` shows the mode actually used. ## What passing a background changes Take a freight quote engine's LightGBM regressor. `shap.TreeExplainer(quote_model)` gets no data, so "auto" becomes **tree_path_dependent**. No background rows are needed, and for LightGBM, XGBoost and CatBoost shap can hand the work to the model package's own contribution routine, which is fast. `shap.TreeExplainer(quote_model, data=X_recent_loads)` gets a background, so "auto" becomes **interventional**. Three things follow: 1. **The background is wrapped and possibly cut.** TreeExplainer builds on the unified `Explainer`, which wraps a DataFrame or array as `shap.maskers.Independent(data)` with `max_samples=100`. A larger table is sub-sampled to 100 rows and shap logs that it did so. To keep more, pass `shap.maskers.Independent(X_recent_loads, max_samples=1000)` as `data`. 2. **Cost scales with the background.** The docstring says run time grows linearly with the number of background rows and suggests 100 to 1,000. 3. **The expected value comes from the background.** It is the mean model output over those rows rather than a value derived from the training counts, so the two modes report different baselines for the same model. Why the two modes split credit differently between correlated features is a question about Shapley values and faithfulness; the library question is which mode you actually ran. ## The edge cases in shap 0.52 - **Interventional with no data.** `feature_perturbation="interventional"` and `data=None` raises a `FutureWarning` ("In the future, passing feature_perturbation='interventional' without providing a background dataset will raise an error") and falls back to tree_path_dependent. It is not an error yet. - **The slow-runtime warning.** With an explicit "interventional" and more than 1,000 background rows, shap warns that runtimes may be slow and suggests `shap.sample(data, 100)`. Because a plain DataFrame is already cut to 100 rows, you only see this when you pass an `Independent` masker with a higher `max_samples`. The check sits in the explicit-"interventional" branch, so "auto" with the same masker does not warn. - **Output scales.** Tree-path-dependent supports only `model_output="raw"`; asking for "probability" or "log_loss" without a background raises a `ValueError`. How to read the resulting scale is a separate, interpretation question. ## approximate: a call argument now In 0.47 `approximate` moved to the call: `explainer(X, approximate=True)` or `explainer.shap_values(X, approximate=True)`. The constructor still accepts it in 0.52 with a `DeprecationWarning` whose text says it will be "removed in version v0.48.0"; it was not removed, and code written for 0.46 keeps working with a warning. On the background-free path a LightGBM model rejects `approximate=True` with an assertion, so the flag is not a universal speed switch. ## Choosing a mode for a batch job For an overnight run over 50,000 quotes, the two modes trade differently: - **tree_path_dependent** needs no reference table to maintain and runs on the model package's fast path, but it can only explain the raw output, and its baseline is fixed by the training data that built the trees. - **interventional** needs a background you choose and version, costs time in proportion to its rows, and is the only way to get probability or log-loss outputs; its baseline is the mean prediction over that background. - Whichever you choose, pin it: pass `feature_perturbation=` explicitly in production code, so adding or removing a `data` argument during a refactor cannot switch the mode under you. ## Practical checklist - Decide the mode deliberately and log `explainer.feature_perturbation`. - If you pass a background, decide its size with `max_samples` rather than trusting the 100-row default. - Need probabilities or a per-row loss breakdown: pass a background. - Move any constructor-side `approximate` to the call.

  • A classifier must be explained on the probability scale; what does TreeExplainer need for model_output="probability"?
    The interventional path, which means a background passed as `data`. With no background, "auto" resolves to tree_path_dependent, and that mode supports only `model_output="raw"`, so the constructor raises a `ValueError`. "log_loss" has the same requirement and also needs the labels `y` on the call. Reading what the probability-scale values then mean is an interpretation question, not a constructor one.
  • Code written for shap 0.46 passes approximate=True to the TreeExplainer constructor; what happens in 0.52?
    It still runs. The constructor accepts the argument and emits a `DeprecationWarning` saying it was deprecated in v0.47.0 and will be removed in v0.48.0, a removal that never happened. The supported form is the call argument, `explainer(X, approximate=True)`. On the background-free path a LightGBM model rejects `approximate=True` with an assertion either way.

  • TreeExplainer is interventional by default whether or not you pass data.
  • TreeExplainer integrates over every row of a 20,000-row DataFrame you pass as data.
  • Without a background, TreeExplainer can still explain the probability output.
  • Passing interventional with no background raises an error in shap 0.52.
  • The constructor's approximate argument is the current way to request fast values.

Assumes shap 0.52: feature_perturbation="auto" is the default since 0.47, approximate moved to the call in 0.47, and the constructor's approximate still only warns in 0.52.