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

fix: Explanation hstack undefined name error #86

Merged
merged 2 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions shap/_explanation.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,25 +583,24 @@ def hstack(self, other):
"""
assert self.shape[0] == other.shape[0], "Can't hstack explanations with different numbers of rows!"
assert np.max(np.abs(self.base_values - other.base_values)) < 1e-6, "Can't hstack explanations with different base values!"

new_exp = Explanation(
np.hstack([self.values, other.values]),
np.hstack([self.values, other.values]),
self.base_values,
self.data,
self.display_data,
self.instance_names,
self.feature_names,
self.output_names,
self.output_indexes,
self.lower_bounds,
self.upper_bounds,
self.error_std,
self.main_effects,
self.hierarchical_values,
self.clustering
values=np.hstack([self.values, other.values]),
base_values=self.base_values,
data=self.data,
display_data=self.display_data,
instance_names=self.instance_names,
feature_names=self.feature_names,
output_names=self.output_names,
output_indexes=self.output_indexes,
lower_bounds=self.lower_bounds,
upper_bounds=self.upper_bounds,
error_std=self.error_std,
main_effects=self.main_effects,
hierarchical_values=self.hierarchical_values,
clustering=self.clustering,
)
return self._numpy_func("min", axis=axis)
return new_exp

# def reshape(self, *args):
# return self._numpy_func("reshape", newshape=args)
Expand Down
59 changes: 59 additions & 0 deletions tests/test_explanation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""This file contains tests for the `shap._explanation` module.
"""

import numpy as np
import pytest
import shap


def test_explanation_hstack():
"""Checks that `hstack` works as expected with two valid Explanation objects.
And that it returns an Explanation object.
"""
# generate 2 Explanation objects for stacking
rs = np.random.RandomState(0)
base_vals = np.ones(20) * 0.123
exp1 = shap.Explanation(
values=rs.randn(20, 7),
base_values=base_vals,
)
exp2 = shap.Explanation(
values=rs.randn(20, 5),
base_values=base_vals,
)
new_exp = exp1.hstack(exp2)

assert isinstance(new_exp, shap.Explanation)
assert new_exp.values.shape == (20, 12)


def test_explanation_hstack_errors():
"""Checks that `hstack` throws errors on invalid input.
"""
# generate 2 Explanation objects for stacking
rs = np.random.RandomState(1)
base_vals = np.ones(20) * 0.123
base_exp = shap.Explanation(
values=rs.randn(20, 5),
base_values=base_vals,
)

with pytest.raises(
AssertionError,
match="Can't hstack explanations with different numbers of rows",
):
exp2 = shap.Explanation(
values=rs.randn(7, 5),
base_values=np.ones(7),
)
_ = base_exp.hstack(exp2)

with pytest.raises(
AssertionError,
match="Can't hstack explanations with different base values",
):
exp2 = shap.Explanation(
values=rs.randn(20, 5),
base_values=np.ones(20) * 0.987,
)
_ = base_exp.hstack(exp2)