Skip to content

Commit

Permalink
fix bug in convert_categorical_to_continuous that did not handle scen…
Browse files Browse the repository at this point in the history
…arios where there is only one continuous section
  • Loading branch information
paulbkoch committed Feb 29, 2024
1 parent a7e345a commit 0cf3f2f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python/interpret-core/interpret/glassbox/_ebm/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def convert_categorical_to_continuous(categories):
non_float_idxs = [idx for idx in non_float_idxs if idx not in clusters]
non_float_idxs.append(max(categories.values()) + 1)

if len(clusters) <= 1:
return np.empty(0, np.float64)
if len(clusters) == 0:
return np.empty(0, np.float64), [[0], [], non_float_idxs], np.nan, np.nan

cluster_bounds = sorted((min(cluster_list), max(cluster_list))
for cluster_list in clusters.values())
Expand Down
21 changes: 21 additions & 0 deletions python/interpret-core/tests/glassbox/ebm/test_ebm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ def test_make_bag_stratified():
bag = make_bag(y, test_size=0.25, rng=1, is_stratified=True)


def test_convert_categorical_to_continuous_none():
cuts, mapping, old_min, old_max = convert_categorical_to_continuous(
{"ABCD": 1, "EFGH": 2, "IJKL": 1}
)
assert len(cuts) == 0
assert mapping == [[0], [], [1, 2, 3]]
assert np.isnan(old_min)
assert np.isnan(old_max)



def test_convert_categorical_to_continuous_single():
cuts, mapping, old_min, old_max = convert_categorical_to_continuous(
{"10": 1, "+10": 2, "30": 1}
)
assert len(cuts) == 0
assert mapping == [[0], [1, 2], [3]]
assert old_min == 10
assert old_max == 30


def test_convert_categorical_to_continuous_easy():
cuts, mapping, old_min, old_max = convert_categorical_to_continuous(
{"10": 1, "20": 2, "30": 3}
Expand Down

0 comments on commit 0cf3f2f

Please sign in to comment.