Skip to content

Commit

Permalink
fix shap_interaction_values for shap 0.45
Browse files Browse the repository at this point in the history
  • Loading branch information
oegedijk committed Mar 11, 2024
1 parent 50a37db commit 01bce04
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions explainerdashboard/explainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3068,9 +3068,21 @@ def shap_interaction_values(self, pos_label=None):
self._shap_interaction_values = self.shap_explainer.shap_interaction_values(
self.X
)

if len(self.labels) == 2:
if not isinstance(self._shap_interaction_values, list):
if (
isinstance(self._shap_interaction_values, np.ndarray)
and len(self._shap_interaction_values.shape) == 4
and self._shap_interaction_values.shape[3] == 2
):
# for binary classifier only keep positive class:
self._shap_interaction_values = [
self._shap_interaction_values[:, :, :, 1]
]
elif (
isinstance(self._shap_interaction_values, np.ndarray)
and len(self._shap_interaction_values.shape) == 3
):
# for binary classifier only keep positive class:
self._shap_interaction_values = [self._shap_interaction_values]
elif (
isinstance(self._shap_interaction_values, list)
Expand All @@ -3086,6 +3098,15 @@ def shap_interaction_values(self, pos_label=None):
"Adjust the labels parameter accordingly!"
)
else:
if (
isinstance(self._shap_interaction_values, np.ndarray)
and len(self._shap_interaction_values.shape) == 4
and self._shap_interaciton_values.shape[3] > 2
):
self._shap_interaciton_values = [
self._shap_interaction_values[:, :, :, i]
for i in range(self._shap_interaciton_values.shape)
]
assert len(self._shap_interaction_values) == len(self.labels), (
f"len(self.label)={len(self.labels)}, but "
f"shap returned shap values for {len(self._shap_interaction_values)} classes! "
Expand Down

0 comments on commit 01bce04

Please sign in to comment.