我正在研究损耗模型。我有多个具有高基数的分类特征。因此,我为此尝试了 Catboost 和 LightGBM。 我还找到了两者的 SHAP 图。以下是我的代码。
CatBoost:
import shap # package used to calculate Shap values
from catboost import CatBoostClassifier, Pool
shap_values = clf.get_feature_importance(Pool(X_test, label=Y_test,cat_features=cat_features) ,
type="ShapValues")
expected_value = shap_values[0,-1]
shap_values = shap_values[:,:-1]
shap.initjs()
lightGBM:
import shap
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)
base_value = explainer.expected_value[0]
shap.summary_plot(shap_values, X_test)
我的情节如下所示。
CatBoost
LightGBM
尽管是同一个分类问题,但为什么我的两个图看起来不同?我知道用于查找 SHAP 值的两种代码都不同,这可能是根本原因。但是我在互联网上找不到任何相关的东西。如何修改 lightGBM 图,使其看起来像 catboost 图?
beeswarm plot
。将文档中的示例改编为您的 LightGBM 代码可以得到:
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)
# new
shap.plots.beeswarm(shap_values)