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

AttributeError: 'Explanation' object has no attribute '_old_format' #1526

Closed
JSybrandt opened this issue Oct 14, 2020 · 9 comments
Closed

AttributeError: 'Explanation' object has no attribute '_old_format' #1526

JSybrandt opened this issue Oct 14, 2020 · 9 comments

Comments

@JSybrandt
Copy link

Hit an error while testing shap:

Code:

explainer = shap.PermutationExplainer(model.predict, shap.maskers.Independent(observation_df, max_samples=10))
shap_values = explainer.shap_values(observation_df, npermutations=5, silent=True)

Causes:

AttributeError: 'Explanation' object has no attribute '_old_format'

Error at line:
https://github.com/slundberg/shap/blob/491d46a540d16fb5a9868de6be2913599c850167/shap/explainers/_permutation.py#L135

@zwag20
Copy link

zwag20 commented Feb 11, 2021

I am receiving the same error, has this been addressed?

@thomastskng
Copy link

I ran into the same issue as well when using shap.PermutationExplainer and try to compute the shap_values. Is this being fixed ?

@ThomasWolf0701
Copy link

Also getting the same error with scikit catboost model.

localExplainer = shap.PermutationExplainer(bestModel["m"].predict,transformedMatrix.iloc[:,0:2214])# masker, link=CPUDispatcher())

localExplainer.shap_values(transformedMatrix.iloc[1:2,0:2214])
Permutation explainer: 2it [00:16, 16.16s/it]
Traceback (most recent call last):

File "", line 1, in
localExplainer.shap_values(transformedMatrix.iloc[1:2,0:2214])

File "C:\Users\Thomas Wolf\anaconda3\envs\ChemPipeline\lib\site-packages\shap\explainers_permutation.py", line 155, in shap_values
return explanation._old_format()

AttributeError: 'Explanation' object has no attribute '_old_format'

@mmdfl
Copy link

mmdfl commented Jun 18, 2021

Same error here... My code:
explainer = shap.Explainer(model.predict, X_train)
shap_values = explainer.shap_values(X_test.sample(n=100, random_state=1))

Error:
Permutation explainer: 101it [01:42, 1.12s/it]
AttributeError Traceback (most recent call last)
in
----> 1 shap_values = explainer.shap_values(X_test.sample(n=100, random_state=1))

c:\python38\lib\site-packages\shap\explainers_permutation.py in shap_values(self, X, npermutations, main_effects, error_bounds, batch_evals, silent)
139
140 explanation = self(X, max_evals=npermutations * X.shape[1], main_effects=main_effects)
--> 141 return explanation._old_format()
142
143

AttributeError: 'Explanation' object has no attribute '_old_format'

If you are using RandomForest as model, inside a pipeline, like me, this could help:

#1373 (comment)

@NatiSN
Copy link

NatiSN commented Dec 7, 2021

I removed the method ._old_format() and that solved the problem so it looks like

return explanation

@leoschet
Copy link

leoschet commented Apr 4, 2022

For shap.Explainer getting the shap values by calling the explainer directly solved for me. To be clear:

explainer = shap.Explainer(model.predict, X)
shap_values = explainer(X)

@ursulalujun
Copy link

explainer = shap.explainers.Permutation(f, X)
shap_values = explainer(X)

Replacing with the code above, you will get an explanation with this format as
`.values =
array([-0.00857, -0.00452, -0.00724, -0.0065 , -0.00375, -0.00324,
-0.00152, -0.00787, -0.00562, -0.009 ])

.base_values =
array([62.848, 62.848, 62.848, 62.848, 62.848, 62.848, 62.848, 62.848,
62.848, 62.848])

.data =
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])`

then use shap_values = shap_values.values to obtain shap_values

@thomastskng
Copy link

explainer = shap.explainers.Permutation(f, X)
shap_values = explainer(X)

Replacing with the code above, you will get an explanation with this format as `.values = array([-0.00857, -0.00452, -0.00724, -0.0065 , -0.00375, -0.00324, -0.00152, -0.00787, -0.00562, -0.009 ])

.base_values = array([62.848, 62.848, 62.848, 62.848, 62.848, 62.848, 62.848, 62.848, 62.848, 62.848])

.data = array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])`

then use shap_values = shap_values.values to obtain shap_values

Thanks for the code. My only question would be how do i set the npermutations to a different value here ?

@AstrosiosaurQ7
Copy link

I removed the method ._old_format() and that solved the problem so it looks like

return explanation

hi I got same question, and I'm not sure how to remove the methon. Would you mind to explain a little more specific? T.T

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

10 participants