Skip to content

Commit

Permalink
FIX attribute error when qoi is None (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopfonseca committed Nov 14, 2024
1 parent a7e6478 commit 10d29eb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
10 changes: 8 additions & 2 deletions sharp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ShaRP(BaseEstimator):
----------
estimator : ML classifier
qoi : Quantity of interest
qoi : Quantity of interest, default: "rank"
measure : measure used to estimate feature contributions (unary, set, banzhaf, etc.)
Expand Down Expand Up @@ -85,7 +85,13 @@ def fit(self, X, y=None):

self._rng = check_random_state(self.random_state)

if isinstance(self.qoi, str):
if self.qoi is None:
self.qoi_ = check_qoi(
"rank",
target_function=self.target_function,
X=X_,
)
elif isinstance(self.qoi, str):
self.qoi_ = check_qoi(
self.qoi,
target_function=self.target_function,
Expand Down
16 changes: 16 additions & 0 deletions sharp/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Tests code in `base.py`.
"""

import numpy as np
from sharp import ShaRP


def test_default_qoi():
"""
Reproduces issue #44: Defining ShaRP without an explicit QoI raises an AttributeError
"""
_X = np.random.random((100, 3))
sharp = ShaRP(target_function=lambda x: x.sum(axis=1))
sharp.fit(_X)
sharp.all(_X[:5])

0 comments on commit 10d29eb

Please sign in to comment.