Shap: Calculating shap values with scikit learn svm regressor

Created on 17 Sep 2019  路  2Comments  路  Source: slundberg/shap

I am having problems using KernelExplainer with scikit-learn's svm.SVR regressor, and passing the best_estimator_ to the explainer after GridSearchCV.

I've noticed other issues: i.e. 147, I am not sure if this is exactly the same problem.

Below is a minimal reproducible example:

import pandas as pd
from sklearn.model_selection import GridSearchCV, LeaveOneOut
from sklearn import svm, preprocessing
import shap

url= 'https://raw.githubusercontent.com/mycarta/Data-science-tools-petroleum-exploration-and-production/master/Python/data/Table2_Hunt_2013_edit.csv'
data = pd.read_csv(url).astype('float64')

X = data.drop('Production',axis=1)
scaled_X = preprocessing.StandardScaler().fit_transform(X)
scaled_X_df = pd.DataFrame(scaled_X, index=X.index, columns=X.columns)
y = data['Production']

SVM_regressor = svm.SVR(cache_size = 800)
loo = LeaveOneOut()

parm_grid={'C': [0.1, 0.5, 1, 3, 5, 7, 9, 11, 13, 15],
            'gamma':[0.0005, 0.0002, 0.0001, 0.001, 0.01, 0.1, 1, 2]}

grid_search = GridSearchCV(SVM_regressor,
                           param_grid=parm_grid,
                           scoring='neg_mean_squared_error',
                           cv=loo)

rgr = grid_search.fit(scaled_X_df, y)

r = rgr.best_estimator_

explainer = shap.KernelExplainer(r, scaled_X_df)
shap_values = explainer.shap_values(scaled_X_df)

which throws the error:

image

Can you suggest a workaround? Thanks!

Most helpful comment

If i am right for kernel explainer, you have to pass in either model.predict or model.predict_proba or model.decision_function - in your case - r.predict since you want to explain the regression predictions Refer #506

All 2 comments

If i am right for kernel explainer, you have to pass in either model.predict or model.predict_proba or model.decision_function - in your case - r.predict since you want to explain the regression predictions Refer #506

I will try it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

artemmavrin picture artemmavrin  路  4Comments

shoaibkhanz picture shoaibkhanz  路  4Comments

ArpitSisodia picture ArpitSisodia  路  3Comments

Nithanaroy picture Nithanaroy  路  4Comments

gabrielcs picture gabrielcs  路  3Comments