SHAP 汇总图和平均值一起显示
SHAP Summary Plot and Mean Values displaying together
为 SHAP
summary_plot
使用了以下 Python 代码:
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
而在 R 中,绘图如下所示:
R Plot
如何修改我的 Python 脚本以在同一图中包含与每个特征对应的平均值(|SHAP 值|)(就像 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)
为 SHAP
summary_plot
使用了以下 Python 代码:
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
而在 R 中,绘图如下所示: R Plot
如何修改我的 Python 脚本以在同一图中包含与每个特征对应的平均值(|SHAP 值|)(就像 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)