🚀原文地址 📑Notebook

    In this tutorial we use catboost for a gradient boosting with trees.

    You can install catboost with pip:

    1. $ pip install catboost
    2. # or
    3. $ conda install -c conda-forge catboost

    Let’s first explore shap values for dataset with numeric features

    1. import catboost
    2. from catboost import *
    3. import shap
    4. shap.initjs()
    5. X, y = shap.datasets.boston()
    6. model = CatBoostRegressor(iterations=300, learning_rate=0.1, random_seed=123)
    7. model.fit(X, y, verbose=False, plot=False)
    8. explainer = shap.TreeExplainer(model)
    9. shap_values = explainer.shap_values(X)
    10. # visualize the first prediction's explanation
    11. shap.force_plot(explainer.expected_value, shap_values[0, :], X.iloc[0, :])

    image.png :::tips 💡 在 Google Colab 或者 Deepnote 中使用时,会出现“Visualization omitted, Javascript library not loaded!”报错信息。
    解决办法:只需要在 force_plot 函数中添加 matplotlib=True 参数即可 :::