Search code examples
pythonmatplotlibmachine-learningloessshap

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?


Solution

  • 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