2

shap.dependence_plot是否可以在shapPython 包的结果中添加回归线?

玩具示例:

import xgboost
import shap

# train XGBoost model
X,y = shap.datasets.adult()
model = xgboost.XGBClassifier().fit(X, y)

# compute SHAP values
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)

# The shap dependence plot
shap.dependence_plot("Age", shap_values, X)

是否可以以简单的方式绘制像 LOESS 这样的回归线?

4

1 回答 1

1

你可以试试:

import xgboost
import shap

# train XGBoost model
X,y = shap.datasets.adult()
model = xgboost.XGBClassifier().fit(X, y)

# compute SHAP values
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)

import statsmodels.api as sm

idx = np.where(X.columns=="Age")[0][0]
x = X.iloc[:,idx]
y_sv = shap_values[:,idx]
lowess = sm.nonparametric.lowess(y_sv, x, frac=.3)

_,ax = plt.subplots()
ax.plot(*list(zip(*lowess)), color="red", )

shap.dependence_plot("Age", shap_values, X, ax=ax)

在此处输入图像描述

于 2020-11-28T07:11:23.820 回答