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 catboostfrom catboost import *import shapshap.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 explanationshap.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 参数即可
:::
