Shap: How to use Shap with GridsearchCV?

Created on 23 Dec 2019  路  4Comments  路  Source: slundberg/shap

Hello Everyone,

I was trying to use Shapley value approach for understanding the model predictions. I am trying this on a Xgboost model. However when I try this, I get the below error.

Exception: Model type not yet supported by TreeExplainer: > <'sklearn.model_selection._search.GridSearchCV'>

Here is my code below

clf = GridSearchCV(xg_clf, op_params, cv=10,scoring='f1',verbose=2, refit=True)
clf.fit(X_train_std,y_train)
y_pred = clf.predict(X_test_std)
cm = confusion_matrix(y_test, y_pred)
shap.initjs()
explainer = shap.TreeExplainer(clf)
shap_values = explainer.shap_values(X_train_std)
i = 1234
shap.force_plot(explainer.expected_value, shap_values[i], features=X_train_std.loc[1234], feature_names=X.columns)

Can you help me figure out what I am doing wrong?

can't shap be used when we do gridsearchcv?

How can I adpat my code to get this working?

Most helpful comment

@SSMK-wq
You just need model = clf.best_estimator_
No need of .model.

Then you can use the model in TreeExplainer:
explainer = shap.TreeExplainer(model)

All 4 comments

You need to pull the best estimated model from the gridsearch and send it to the TreeExplainer.

For example try,
model = clf.best_estimator_.model
explainer = shap.TreeExplainer(model)

@vgambeta - No I am using Xgboost model. It doesn't have model attribute. So I get the below error

AttributeError: 'XGBClassifier' object has no attribute 'model'

So when I used booster attribute as shown below

model = clf.best_estimator_.booster
explainer = shap.TreeExplainer(model),

I get the below error

Exception: Model type not yet supported by TreeExplainer: <class 'str'>

Can you help?

@SSMK-wq the error looks like model is a string and not a XGBoost model object. Could you try printing that object and make sure it is what you think?

@SSMK-wq
You just need model = clf.best_estimator_
No need of .model.

Then you can use the model in TreeExplainer:
explainer = shap.TreeExplainer(model)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

artemmavrin picture artemmavrin  路  4Comments

1vecera picture 1vecera  路  3Comments

yolle103 picture yolle103  路  3Comments

TdoubleG picture TdoubleG  路  4Comments

samupino picture samupino  路  3Comments