Skip to content

Interpretability, attribution, and explanation audits · Interpretability, explanations, and model-mechanism analysis · EXP003 · Algorithm

TreeSHAP

Compute Shapley attributions efficiently by using tree structure rather than treating every prediction as an opaque black-box query.

Runs in CodeSplash (Experimental) Complete calculation CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Runs in CodeSplash (Experimental)
Code example
Complete calculation
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through explain.shap. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Compute Shapley attributions efficiently by using tree structure rather than treating every prediction as an opaque black-box query.

Question / hypotheses

Which features account for this tree-ensemble prediction under the specified feature-dependence and background semantics?

Architecture / computational structure

Tree-specific path bookkeeping combines feature inclusion/exclusion contributions while traversing an ensemble. Interventional background integration and tree-path-dependent weighting are distinct supported semantics; they need not give the same explanation.

Assumptions

The tree representation and selected model output must be supported. The explanation game, treatment of repeated feature splits, feature dependence and background population must be declared. Exact computation for a chosen game does not make that game a causal model.

Formula / computational definition

f(x)=ϕ0+∑j=1dϕj(x),ϕ0=v(∅),ϕj is the Shapley value for the declared tree coalition game.f(x)=\phi_0+\sum_{j=1}^d\phi_j(x),\qquad \phi_0=v(\varnothing),\quad \phi_j\text{ is the Shapley value for the declared tree coalition game}.

Parameters and notation

Tree thresholds and leaves are learned predictor parameters, held fixed during explanation. Design choices include output scale, feature-dependence mode, background size/data, interaction-value computation, and approximation settings if used. Library defaults can evolve; pin the explainer configuration. Here d is the number of input features, f the selected scalar tree-ensemble output, phi_0 the empty-coalition reference value, and phi_j the contribution of feature j for input x.

Fitting, fine-tuning, and adaptation

Refit or reload the explainer when the ensemble changes. Adaptation belongs to the predictor training process, whereas background changes redefine the reference explanation. Validate feature order after one-hot encoding or preprocessing changes.

How to use and calibrate

Check additivity on the chosen scale, compare a few tiny trees against exact coalition enumeration, and inspect background sensitivity. Aggregate local explanations with explicit weighting of the explained population; a global mean of absolute attribution loses sign and heterogeneity.

Example application

Explain a small regression tree while demonstrating why the explanation depends on how unavailable features are integrated over a background set.

Interpretation / pitfalls

Watch out

Tree-path-dependent and interventional values answer different questions. Correlated predictors can redistribute contributions. A globally important feature need not matter for a particular case. TreeSHAP is not a replacement for held-out predictive evaluation.

Python and R code examples

Scope: Exact background-replacement Shapley calculation for a tiny two-feature tree by enumeration; it verifies the game but does not implement TreeSHAP path recursion. Python: NumPy; R: base R.

Validation: Python executed successfully on the supplied fixture; R reviewed but unexecuted. Full implementations and supported operation details are provided in the linked primary/official sources.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Exact background-replacement Shapley calculation for a tiny two-feature tree by enumeration; it verifies the game but does not implement TreeSHAP path recursion. Python: NumPy; R: base R. · needs numpy

import numpy as np
bg=np.array([[0.,0.],[0.,1.],[1.,0.],[1.,1.]]); x=np.array([1.,1.])
def f(a): return np.where(a[:,0]>.5,np.where(a[:,1]>.5,5.,3.),1.)
def v(S):
    z=bg.copy();z[:,S]=x[S];return f(z).mean()
a,b,c,d=v([]),v([0]),v([1]),v([0,1]);phi=np.array([((b-a)+(d-c))/2,((c-a)+(d-b))/2])
assert np.isclose(a+phi.sum(),f(x[None,:])[0]);print(a,phi)
R

Complete calculation — Exact background-replacement Shapley calculation for a tiny two-feature tree by enumeration; it verifies the game but does not implement TreeSHAP path recursion. Python: NumPy; R: base R.

bg <- rbind(c(0,0),c(0,1),c(1,0),c(1,1)); x <- c(1,1)
f <- function(a) ifelse(a[,1]>.5,ifelse(a[,2]>.5,5,3),1)
v <- function(S) {z<-bg;if(length(S)) for(j in S) z[,j]<-x[j];mean(f(z))}
a<-v(integer());b<-v(1);c<-v(2);d<-v(1:2)
phi<-c(((b-a)+(d-c))/2,((c-a)+(d-b))/2)
stopifnot(abs(a+sum(phi)-f(matrix(x,nrow=1)))<1e-12);print(c(base=a,phi))

Sources

From the CodeSplash reference, entry EXP003.