Benchmark XGBoost explanations

This notebook compares several different explantion methods when applied to XGBoost models. These methods are compared across many different evaluation metrics. Explanation error is the primary metric we sort by, but we also compare across many other metrics, since no single metric fully captures the performance of an attribution explanation method.

For a more detailed explanation of each of the metrics used here please check out the documation strings of the various classes.

Benchmark explainers on an XGBoost regression model of Boston housing

Build the model and explanations

[1]:
import numpy as np
from sklearn.model_selection import train_test_split
import xgboost
import shap
import shap.benchmark

# build the model
model = xgboost.XGBRegressor(n_estimators=1000, subsample=0.3)
X,y = shap.datasets.boston()
X = X.values
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
model.fit(X_train, y_train, eval_set=[(X_test, y_test)], early_stopping_rounds=10, verbose=False)

# define the benchmark evaluation sample set
X_eval = X_test[:]
y_eval = y_test[:]

# use an independent masker
masker = shap.maskers.Independent(X_train)
pmasker = shap.maskers.Partition(X_train)

# build the explainers
explainers = [
    ("Permutation", shap.explainers.Permutation(model.predict, masker)),
    ("Permutation part.", shap.explainers.Permutation(model.predict, pmasker)),
    ("Partition", shap.explainers.Partition(model.predict, pmasker)),
    ("Tree", shap.explainers.Tree(model, masker)),
    ("Tree approx.", shap.explainers.Tree(model, masker, approximate=True)),
    ("Exact", shap.explainers.Exact(model.predict, masker)),
    ("Random", shap.explainers.other.Random(model.predict, masker))
]

# # dry run to get all the code warmed up for valid runtime measurements
for name, exp in explainers:
    exp(X_eval[:1])

# explain with all the explainers
attributions = [(name, exp(X_eval)) for name, exp in explainers]
Exact explainer: 128it [00:39,  3.27it/s]

Run the benchmarks

[2]:
results = {}

smasker = shap.benchmark.ExplanationError(
    masker, model.predict, X_eval
)
results["explanation error"] = [smasker(v, name=n) for n,v in attributions]

ct = shap.benchmark.ComputeTime()
results["compute time"] = [ct(v, name=n) for n,v in attributions]

for mask_type, ordering in [("keep", "positive"), ("remove", "positive"), ("keep", "negative"), ("remove", "negative")]:
    smasker = shap.benchmark.SequentialMasker(
        mask_type, ordering, masker, model.predict, X_eval
    )
    results[mask_type + " " + ordering] = [smasker(v, name=n) for n,v in attributions]

cmasker = shap.maskers.Composite(masker, shap.maskers.Fixed())
for mask_type, ordering in [("keep", "absolute"), ("remove", "absolute")]:
    smasker = shap.benchmark.SequentialMasker(
        mask_type, ordering, cmasker, lambda X, y: (y - model.predict(X))**2, X_eval, y_eval
    )
    results[mask_type + " " + ordering] = [smasker(v, name=n) for n,v in attributions]

Show scores across all metrics for all explainers

This multi-metric benchmark plot sorts the method by the first method, and rescales the scores to be relative for each metric, so that the best score appears at the top and the worse score at the bottom.

[3]:
shap.plots.benchmark(sum(results.values(), []))
../../../_images/example_notebooks_benchmarks_tabular_Benchmark_XGBoost_explanations_8_0.png

Show overall performance again but without Random

Since random scores are so much worse than reasonable explanation methods, we draw the same plot again but without the Random method so we can see smaller variations in performance.

[4]:
shap.plots.benchmark(filter(lambda x: x.method != "Random", sum(results.values(), [])))
../../../_images/example_notebooks_benchmarks_tabular_Benchmark_XGBoost_explanations_10_0.png

Show detail plots of each metric type

If we plot scores for one metric at a time then we can see a much more detailed comparison of the methods. Some methods just have a score (explanation error and compute time) while other methods have entire performance curves, and the score is the area under (or over) these curves.

[5]:
import matplotlib.pyplot as plt
num_plot_rows = len(results) // 2 + len(results) % 2
fig, ax = plt.subplots(num_plot_rows, 2, figsize=(12, 5 * num_plot_rows))

for i, k in enumerate(results):
    plt.subplot(num_plot_rows, 2, i+1)
    shap.plots.benchmark(results[k], show=False)
if i % 2 == 0:
    ax[-1, -1].axis('off')
plt.tight_layout()
plt.show()
../../../_images/example_notebooks_benchmarks_tabular_Benchmark_XGBoost_explanations_12_0.png

Benchmark explainers on an XGBoost classification model of census reported income

Build the model and explanations

[6]:
# build the model
model = xgboost.XGBClassifier(n_estimators=1000, subsample=0.3)
X,y = shap.datasets.adult()
X = X.values
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
model.fit(X_train, y_train, eval_set=[(X_test, y_test)], early_stopping_rounds=10, verbose=False)
def logit_predict(X):
    return model.predict(X, output_margin=True)
def loss_predict(X, y):
    probs = model.predict_proba(X)
    return [-np.log(probs[i,y[i]*1]) for i in range(len(y))]


# define the benchmark evaluation sample set (limited to 1000 samples for the sake of time)
X_eval = X_test[:1000]
y_eval = y_test[:1000]

# use an independent masker
masker = shap.maskers.Independent(X_train)
pmasker = shap.maskers.Partition(X_train)

# build the explainers
explainers = [
    ("Permutation", shap.explainers.Permutation(logit_predict, masker)),
    ("Permutation part.", shap.explainers.Permutation(logit_predict, pmasker)),
    ("Partition", shap.explainers.Partition(logit_predict, pmasker)),
    ("Tree", shap.explainers.Tree(model, masker)),
    ("Tree approx.", shap.explainers.Tree(model, masker, approximate=True)),
    ("Random", shap.explainers.other.Random(logit_predict, masker)),
    ("Exact", shap.explainers.Exact(logit_predict, masker))
]

# # dry run to get all the code warmed up for valid runtime measurements
for name, exp in explainers:
    exp(X_eval[:1])

# explain with all the explainers
attributions = [(name, exp(X_eval)) for name, exp in explainers]
Permutation explainer: 1001it [00:18, 55.26it/s]
Permutation explainer: 1001it [00:18, 54.26it/s]
Partition explainer: 1001it [01:21, 12.26it/s]
Exact explainer: 1001it [01:20, 12.36it/s]

Run the benchmarks

[7]:
results = {}

# we run explanation error first as the primary metric
smasker = shap.benchmark.ExplanationError(
    masker, logit_predict, X_eval
)
results["explanation error"] = [smasker(v, name=n) for n,v in attributions]

# next compute time
ct = shap.benchmark.ComputeTime()
results["compute time"] = [ct(v, name=n) for n,v in attributions]

# then removal and addition of feature metrics based on model output
for mask_type, ordering in [("keep", "positive"), ("remove", "positive"), ("keep", "negative"), ("remove", "negative")]:
    smasker = shap.benchmark.SequentialMasker(
        mask_type, ordering, masker, logit_predict, X_eval
    )
    results[mask_type + " " + ordering] = [smasker(v, name=n) for n,v in attributions]

# then removal and addition of feature metrics based on model loss
cmasker = shap.maskers.Composite(masker, shap.maskers.Fixed())
for mask_type, ordering in [("keep", "absolute"), ("remove", "absolute")]:
    smasker = shap.benchmark.SequentialMasker(
        mask_type, ordering, cmasker, loss_predict, X_eval, y_eval
    )
    results[mask_type + " " + ordering] = [smasker(v, name=n) for n,v in attributions]

Show an overall area-under-curve score across all metrics for all explainers

[8]:
shap.plots.benchmark(sum(results.values(), []))
../../../_images/example_notebooks_benchmarks_tabular_Benchmark_XGBoost_explanations_19_0.png

Show overall performance again but without Random

[9]:
shap.plots.benchmark(filter(lambda x: x.method != "Random", sum(results.values(), [])))
../../../_images/example_notebooks_benchmarks_tabular_Benchmark_XGBoost_explanations_21_0.png

Show detail plots of each metric type

[10]:
import matplotlib.pyplot as plt
num_plot_rows = len(results) // 2 + len(results) % 2
fig, ax = plt.subplots(num_plot_rows, 2, figsize=(12, 5 * num_plot_rows))

for i, k in enumerate(results):
    plt.subplot(num_plot_rows, 2, i+1)
    shap.plots.benchmark(results[k], show=False)
if i % 2 == 0:
    ax[-1, -1].axis('off')
plt.tight_layout()
plt.show()
../../../_images/example_notebooks_benchmarks_tabular_Benchmark_XGBoost_explanations_23_0.png

Have an idea for more helpful examples? Pull requests that add to this documentation notebook are encouraged!