PeCaDe
PeCaDe

Reputation: 406

Add regression line to a shap.dependence_plot

Is it possible to add a regression line to the result of shap.dependence_plot of the shap Python package?

Toy example:

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)

would be possible to plot in a simple way a regression line like LOESS?

Upvotes: 2

Views: 3291

Answers (1)

Sergey Bushmanov
Sergey Bushmanov

Reputation: 25249

You can try:

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)

enter image description here

Upvotes: 3

Related Questions