Front page example (XGBoost)

The code from the front page example using XGBoost.

[8]:
import xgboost
import shap

# train XGBoost model
X,y = shap.datasets.boston()
model = xgboost.XGBRegressor(max_depth=1).fit(X, y)

# explain the model's predictions using SHAP values
# (same syntax works for LightGBM, CatBoost, and scikit-learn models)
background = shap.maskers.TabularPartitions(X, sample=100)
explainer = shap.Explainer(model.predict, background, link=shap.links.logit)
shap_values = explainer(X)

# visualize the first prediction's explanation
shap.plots.waterfall(shap_values[0])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-fa7a6360ae5e> in <module>
      9 # (same syntax works for LightGBM, CatBoost, and scikit-learn models)
     10 background = shap.maskers.TabularPartitions(X, sample=100)
---> 11 explainer = shap.Explainer(model.predict_proba, background, link=shap.links.logit)
     12 shap_values = explainer(X)
     13

AttributeError: 'XGBRegressor' object has no attribute 'predict_proba'
[19]:
import xgboost
import shap

# train XGBoost model
X,y = shap.datasets.adult()
model = xgboost.XGBClassifier(max_depth=1, learning_rate=0.5).fit(X, y)

# explain the model's predictions using SHAP values
# (same syntax works for LightGBM, CatBoost, and scikit-learn models)
background = shap.maskers.TabularPartitions(X, sample=100)
def f(x):
    return shap.links.identity(model.predict_proba(x, validate_features=False)[:,1])
explainer = shap.Explainer(f, background, link=shap.links.logit)
shap_values = explainer(X[:100])

# visualize the first prediction's explanation
shap.plots.waterfall(shap_values[0])
../../../_images/example_notebooks_tabular_examples_tree_based_models_Front_page_example_(XGBoost)_2_0.png
[20]:
f(background.data).mean()
[20]:
0.24263532
[20]:
f(background.data).mean()
[20]:
0.24263532
[ ]:

[ ]:

[21]:
shap_values.expected_value
[21]:
0.242635348305339
[22]:
shap.plots.dependence(shap_values[:,"Age"])
../../../_images/example_notebooks_tabular_examples_tree_based_models_Front_page_example_(XGBoost)_8_0.png
[15]:
shap.plots.dependence(shap_values[:,"Age"])
../../../_images/example_notebooks_tabular_examples_tree_based_models_Front_page_example_(XGBoost)_9_0.png
[2]:
# plot the global importance of each feature
shap.plots.bar(shap_values[0])
../../../_images/example_notebooks_tabular_examples_tree_based_models_Front_page_example_(XGBoost)_10_0.png
[3]:
shap.plots.initjs()

# visualize the first prediction's explanation
shap.plots.force(shap_values[0])
[3]:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
[4]:
# visualize the first prediction's explanation
shap.plots.force(shap_values)
[4]:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
[5]:
# plot the importance of a single feature across all samples
shap.plots.dependence(shap_values[:,"RM"], color=shap_values)
../../../_images/example_notebooks_tabular_examples_tree_based_models_Front_page_example_(XGBoost)_13_0.png
[6]:
# plot the global importance of each feature
shap.plots.bar(shap_values)
../../../_images/example_notebooks_tabular_examples_tree_based_models_Front_page_example_(XGBoost)_14_0.png
[7]:
# plot the distribution of importances for each feature over all samples
shap.plots.summary(shap_values)
../../../_images/example_notebooks_tabular_examples_tree_based_models_Front_page_example_(XGBoost)_15_0.png