shap / shap

A game theoretic approach to explain the output of any machine learning model.
https://shap.readthedocs.io
MIT License
22.64k stars 3.26k forks source link

Calculating shap values with scikit learn svm regressor #811

Open mycarta opened 5 years ago

mycarta commented 5 years ago

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!

ssankarasubramanian commented 4 years ago

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

mycarta commented 4 years ago

I will try it

jennykathambi90 commented 3 years ago

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

Thank you so much this worked!

asifrazapk commented 2 years ago

Please check it out this official website for more detail: https://shap.readthedocs.io/en/latest/example_notebooks/tabular_examples/model_agnostic/Iris%20classification%20with%20scikit-learn.html

github-actions[bot] commented 1 day ago

This issue has been inactive for two years, so it's been automatically marked as 'stale'.

We value your input! If this issue is still relevant, please leave a comment below. This will remove the 'stale' label and keep it open.

If there's no activity in the next 90 days the issue will be closed.