Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception: Model type not yet supported by TreeExplainer: <class 'sklearn.pipeline.Pipeline'> #1373

Open
Sajenthan opened this issue Aug 19, 2020 · 12 comments

Comments

@Sajenthan
Copy link

I'm getting the following error when i executed the TreeExplainer, could you help on this.

clf = Pipeline(steps=[('preprocessor', transformations),
('classifier', RandomForestRegressor(max_depth=6, random_state=0))])

model = clf.fit(X_train, Y_train)
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_train)

Exception: Model type not yet supported by TreeExplainer: <class 'sklearn.pipeline.Pipeline'>

@erickrf
Copy link

erickrf commented Aug 19, 2020

Your model is actually a pipeline (usually a sequence of preprocessing operations in sklearn followed by the actual classifier).
It seems you'll have to do the preprocessing by hand and only give the classifier itself to the explainer.

@slundberg
Copy link
Collaborator

@erickrf is right. If you need to explain a whole pipeline I recommend using a model agnostic approach, which under the new API can be explainer = shap.Explainer(f, X); explainer(X)

@Sajenthan
Copy link
Author

Thanks @erickrf & @slundberg , I'm getting the following error

Exception Traceback (most recent call last)
in
----> 1 explainer = shap.Explainer(model, X_train)
2 shap_values = explainer(X_train)

~\Anaconda3\lib\site-packages\shap\explainers_explainer.py in init(self, model, masker, link, algorithm, output_names)
123 # if we get here then we don't know how to handle what was given to us
124 else:
--> 125 raise Exception("The passed model is not callable and is not any known model type: " + str(model))
126
127 # build the right subclass

Exception: The passed model is not callable and is not any known model type: Pipeline(memory=None,
steps=[('preprocessor',
ColumnTransformer(n_jobs=None, remainder='drop',
sparse_threshold=0.3,
transformer_weights=None,
transformers=[('num',
Pipeline(memory=None,
steps=[('imputer',
SimpleImputer(add_indicator=False,
copy=True,
fill_value=None,
missing_values=nan,
strategy='median',
verbose=0)),
('scaler',
StandardScaler(copy=True,
with_mean...
RandomForestRegressor(bootstrap=True, ccp_alpha=0.0,
criterion='mse', max_depth=6,
max_features='auto', max_leaf_nodes=None,
max_samples=None,
min_impurity_decrease=0.0,
min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0,
n_estimators=100, n_jobs=None,
oob_score=False, random_state=0,
verbose=0, warm_start=False))],
verbose=False)

@Lamiane
Copy link

Lamiane commented Sep 3, 2020

I think with model agnostic explainer you need to pass a function responsible for giving predictions not the model itself. Try:
explainer = shap.Explainer(model.predict, X_train)

At least it worked for me with KernelExplainer in ver 0.35

@Sajenthan
Copy link
Author

Thanks @Lamiane I'm getting error in shap_values = explainer(X_train)
Shap error 1 1
Shap error 1 2

@Lamiane
Copy link

Lamiane commented Sep 4, 2020

I don't know what your data is and how your code exactly works but I assume X_train contains sth that is not a number. A string maybe?

It seems that the problem is raised by the isfinite function from numpy. You can try to run it on each row of X_train to see which row causes the error.

@zwag20
Copy link

zwag20 commented Feb 11, 2021

I tried the workaround using
explainer = shap.Explainer(model.predict, X_train)

but I am receiving warnings that say:

Compilation is falling back to object mode WITH looplifting enabled because Function "_build_fixed_multi_output" failed type inference due to: non-precise type pyobject

and

Compilation is falling back to object mode WITHOUT looplifting enabled because Function "_build_fixed_multi_output" failed type inference due to: Cannot determine Numba type of <class 'numba.core.dispatcher.LiftedLoop'>

I'm not sure what these mean.

@OGK0
Copy link

OGK0 commented Feb 13, 2021

@erickrf is right. If you need to explain a whole pipeline I recommend using a model agnostic approach, which under the new API can be explainer = shap.Explainer(f, X); explainer(X)

@slundberg Could you please provide a brief code example of how shap can be made to work with a sklearn Pipeline object ? I reviewed the shap documentation, but may have missed it.

@cmougan
Copy link

cmougan commented Mar 1, 2021

I solved this problem like this

explainer = shap.TreeExplainer(pipe.named_steps["model"])
shap_values = explainer.shap_values(pipe[:-1].transform(X_tr))


shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:])

@cmougan
Copy link

cmougan commented Oct 24, 2021

@slundberg I believe my above answer closed the issue, shall we close it?
Also there is a SO post https://stackoverflow.com/questions/55867862/how-to-use-shap-with-a-linear-svc-model-from-sklearn-using-pipeline

@tempdeltavalue
Copy link

tempdeltavalue commented Jun 9, 2022

quick question ..
Can we use our subclass of BaseEstimator or ClassifierMixin in shap ?

@mubrij
Copy link

mubrij commented Apr 3, 2023

i solved this problem like this

explainer = shap.Explainer(vr.predict, test)
shap_values = explainer(test)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants