How to Use Shap Kernal Explainer with Pipeline models?
I have a pandas DataFrame X. I would like to find the prediction explanation of a a particular model.
My model is given below:
pipeline = Pipeline(steps= [
('imputer', imputer_function()),
('classifier', RandomForestClassifier()
])
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=0)
y_pred = pipeline.fit(x_train, y_train).predict(x_test)
Now for prediction explainer, I use Kernal Explainer from Shap.
This is the following:
# use Kernel SHAP to explain test set predictions
shap.initjs()
explainer = shap.KernelExplainer(pipeline.predict_proba, x_train, link="logit")
shap_values = explainer.shap_values(x_test, nsamples=10)
# # plot the SHAP values for the Setosa output of the first instance
shap.force_plot(explainer.expected_value[0], shap_values[0][0,:], x_test.iloc[0,:], link="logit")
When I run the code, I get the error:
ValueError: Specifying the columns using strings is only supported for pandas DataFrames.
Provided model function fails when applied to the provided data set.
ValueError: Specifying the columns using strings is only supported for pandas DataFrames
Can anyone please help me? I'm really stuck with this. Both x_train and x_test are pandas data frames.
Topic data-science-model machine-learning-model ipython machine-learning
Category Data Science