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?
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)
Most helpful comment
@SSMK-wq
You just need
model = clf.best_estimator_No need of
.model.Then you can use the
modelinTreeExplainer:explainer = shap.TreeExplainer(model)