Skip to content

Commit 4c4afde

Browse files
authored
Merge pull request #231 from agu2347/fix-binary-classifier-target-values
2 parents e75a0d8 + 3794cda commit 4c4afde

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎src/sasctl/utils/model_info.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,12 @@ def target_column(self):
612612
@property
613613
def target_values(self):
614614
if self.is_binary_classifier:
615-
return [self.model.classes_[-1]]
615+
# Model Manager requires both the event and no-event values for binary
616+
# classification models (see write_model_properties_json), so return
617+
# both classes rather than just the "positive" one. Order matches
618+
# `classes_`, which is also the order used for probability columns
619+
# (see `output_column_names`), i.e. [non-event, event].
620+
return list(self.model.classes_)
616621
if self.is_classifier:
617622
return list(self.model.classes_)
618623

‎tests/unit/test_model_info_sklearn.py‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_sklearn_binary_classifier(cancer_dataset, model, algorithm):
8888
assert info.analytic_function == "classification"
8989
assert info.algorithm == algorithm
9090
assert info.output_column_names == [target] # target_variable
91-
assert info.target_values == ["malignant"] # target_event
91+
assert info.target_values == ["benign", "malignant"] # [non-event, event]
9292
assert info.predict_function == model.predict
9393

9494
# If output frame contains a column name then it should be retained
@@ -132,6 +132,33 @@ def test_sklearn_binary_classifier_target_column_name(cancer_dataset):
132132
assert info.output_column_names == ["I_Target"]
133133

134134

135+
def test_sklearn_binary_classifier_target_values_registration(cancer_dataset):
136+
"""Regression test: registering a binary classifier should not raise ValueError.
137+
138+
write_model_properties_json requires either 0, 2, or >2 target values (raising
139+
a ValueError otherwise), but SklearnModelInfo.target_values used to return only
140+
the single event class for binary classifiers, which broke register_model.
141+
See: https://github.com/sassoftware/python-sasctl/issues/200
142+
"""
143+
from sasctl.pzmm import JSONFiles
144+
145+
target = "Type"
146+
X = cancer_dataset.drop(columns=target)
147+
y = cancer_dataset[target]
148+
149+
model = LogisticRegression(max_iter=5000).fit(X, y)
150+
info = get_model_info(model, X, y)
151+
152+
assert len(info.target_values) == 2
153+
154+
properties = JSONFiles().write_model_properties_json(
155+
model_name="test_model",
156+
target_variable=info.target_column,
157+
target_values=info.target_values,
158+
)
159+
assert properties is not None
160+
161+
135162
@pytest.mark.parametrize(
136163
"model, algorithm",
137164
[

0 commit comments

Comments
 (0)