2

I am dealing with a Regression problem and I used StackingRegressor to train data and then make prediction on test set. For model explainability purpose, I used SHAP as follows:

import xgboost
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import StackingRegressor
import shap

# train a model
X, y = shap.datasets.boston()
stkr = StackingRegressor(
estimators = [('xgbr', xgboost.XGBRegressor()), ('rfr', RandomForestRegressor())],
final_estimator = xgboost.XGBRegressor(),
cv = 3
)
 
model = stkr.fit(X, y)

explainer = shap.Explainer(model)
shap_values = explainer(X)
shap.summary_plot(explainer(X), X)

After running this code, I face with the following error:

Exception: The passed model is not callable and cannot be analyzed directly with the given masker! Model: StackingRegressor

I have no idea why I got The passed model is not callable and cannot be analyzed directly with the given masker! Model: StackingRegressor error, while I could use the same code and replace StackingRegressor with RandomForestRegressor or XGBoostRegressor and run it without any problem.

Does anyone have any idea?

Amin Kiany
  • 722
  • 8
  • 17

2 Answers2

2

I have had the same issue with a different model. The solution that worked for me was to use KernelExplainer instead of explainer. Additionally you need to use the model.predict function instead of just the model. Note that to get the shaps values you need to use KernelExplainer.shap_values() uses a function

So I think this should work:

explainer = shap.KernelExplainer(model.predict, X)
shap_values = explainer.shap_values(X)
shap.summary_plot(shap_values, X_train, plot_type="bar")
Thilo
  • 109
  • 5
1

Which version of shap are you using? I just found this error and fixed it by upgrading the version from 0.39.0 to 0.40.0

Not sure can help.

PCP
  • 11
  • 1