gpt4 book ai didi

python - SHAP 摘要图和平均值一起显示

转载 作者:行者123 更新时间:2023-12-02 18:12:29 31 4
gpt4 key购买 nike

使用以下 Python 代码创建 SHAP summary_plot:

explainer = shap.TreeExplainer(model2)
shap_values = explainer.shap_values(X_sampled)
shap.summary_plot (shap_values, X_sampled, max_display=X_sampled.shape[1])

得到了一个像这样的情节: Python Plot

enter image description here

在 R 中,绘图如下所示: R Plot

enter image description here

如何修改我的 Python 脚本以包含与同一图中每个特征相对应的平均值(|SHAP value|)(就像 R 输出一样)?

最佳答案

SHAP 图的自定义有点棘手,除非您愿意修改源代码,但以下内容就可以了:

import xgboost
import shap

X, y = shap.datasets.adult()
model = xgboost.XGBClassifier().fit(X, y)
explainer = shap.TreeExplainer(model, X)
shap_values = explainer(X)
feature_names = [
a + ": " + str(b) for a,b in zip(X.columns, np.abs(shap_values.values).mean(0).round(2))
]

shap.summary_plot(shap_values, X, max_display=X.shape[1],
feature_names=feature_names)

enter image description here

关于python - SHAP 摘要图和平均值一起显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72087719/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com