shap icon indicating copy to clipboard operation
shap copied to clipboard

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

Open JSybrandt opened this issue 5 years ago • 8 comments

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

JSybrandt avatar Oct 14 '20 15:10 JSybrandt

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

zwag20 avatar Feb 11 '21 17:02 zwag20

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

thomastskng avatar May 05 '21 06:05 thomastskng

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'

ThomasWolf0701 avatar May 08 '21 12:05 ThomasWolf0701

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:

https://github.com/slundberg/shap/issues/1373#issuecomment-787999991

mmdfl avatar Jun 18 '21 17:06 mmdfl

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

return explanation

NatiSN avatar Dec 07 '21 17:12 NatiSN

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)

leoschet avatar Apr 04 '22 15:04 leoschet

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

ursulalujun avatar Jul 31 '22 07:07 ursulalujun

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 ?

thomastskng avatar Aug 15 '22 05:08 thomastskng

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

AstrosiosaurQ7 avatar Nov 29 '22 07:11 AstrosiosaurQ7