AttributeError: 'Explanation' object has no attribute '_old_format'
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
I am receiving the same error, has this been addressed?
I ran into the same issue as well when using shap.PermutationExplainer and try to compute the shap_values. Is this being fixed ?
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 "
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'
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)
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
I removed the method ._old_format() and that solved the problem so it looks like
return explanation
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)
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
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.valuesto obtain shap_values
Thanks for the code. My only question would be how do i set the npermutations to a different value here ?
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