In this tutorial we use catboost for a gradient boosting with trees.
You can install catboost with pip:
$ pip install catboost
# or
$ conda install -c conda-forge catboost
Let’s first explore shap values for dataset with numeric features
import catboost
from catboost import *
import shap
shap.initjs()
X, y = shap.datasets.boston()
model = CatBoostRegressor(iterations=300, learning_rate=0.1, random_seed=123)
model.fit(X, y, verbose=False, plot=False)
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
# visualize the first prediction's explanation
shap.force_plot(explainer.expected_value, shap_values[0, :], X.iloc[0, :])
:::tips
💡 在 Google Colab 或者 Deepnote 中使用时,会出现“Visualization omitted, Javascript library not loaded!”报错信息。
解决办法:只需要在 force_plot
函数中添加 matplotlib=True
参数即可
:::