将回归线添加到 shap.dependence_plot
Add regression line to a shap.dependence_plot
是否可以在 shap
Python 包的 shap.dependence_plot
结果中添加回归线?
玩具示例:
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 这样的回归线?
你可以试试:
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)
是否可以在 shap
Python 包的 shap.dependence_plot
结果中添加回归线?
玩具示例:
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 这样的回归线?
你可以试试:
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)