Skip to content

fix: SklearnModelInfo.target_values returns both classes for binary classifiers - #231

Open
agu2347 wants to merge 1 commit into
sassoftware:masterfrom
agu2347:fix-binary-classifier-target-values
Open

fix: SklearnModelInfo.target_values returns both classes for binary classifiers#231
agu2347 wants to merge 1 commit into
sassoftware:masterfrom
agu2347:fix-binary-classifier-target-values

Conversation

@agu2347

@agu2347 agu2347 commented Aug 15, 2026

Copy link
Copy Markdown

Problem

Registering a binary scikit-learn classifier via register_model() raises:

ValueError: Please provide all possible values for the target variable, including a no-event value.

Root cause

SklearnModelInfo.target_values (in src/sasctl/utils/model_info.py) returns only the single "positive" class for binary classifiers:

@property
def target_values(self):
    if self.is_binary_classifier:
        return [self.model.classes_[-1]]
    if self.is_classifier:
        return list(self.model.classes_)

_register_open_source_model (in tasks.py) passes this straight through to JSONFiles.write_model_properties_json(target_values=info.target_values), which explicitly requires either 0, 2, or >2 target values — and raises a ValueError for exactly 1:

elif isinstance(target_values, list) and len(target_values) == 2:
    ...
else:
    raise ValueError(
        "Please provide all possible values for the target variable, including"
        " a no-event value."
    )

So any binary classifier hits this branch and fails. info.target_values is also passed to ImportModel().import_model(target_values=info.target_values, ...), whose docstring says "the order of the target values should reflect the order of the related probabilities in the model" — i.e. it expects both classes too.

Closes #200

Fix

Return both classes for binary classifiers, in classes_ order ([non-event, event]), matching the ordering already used for output_column_names (e.g. ["P_benign", "P_malignant"]) and predict_proba columns.

Testing

  • Updated the existing test_sklearn_binary_classifier assertion, which previously pinned the single-value (buggy) behavior, to expect both classes.
  • Added test_sklearn_binary_classifier_target_values_registration, which reproduces the exact reported crash by calling write_model_properties_json with info.target_values for a fitted binary classifier and asserting it no longer raises.
  • Confirmed the new/updated tests fail with the reported error when the fix is reverted (stashed model_info.py only, tests unchanged): AssertionError: assert ['malignant'] == ['benign', 'malignant'] / assert 1 == 2.
  • All 18 tests in tests/unit/test_model_info_sklearn.py pass with the fix applied.
  • black --check passes on both changed files.

…lassifiers

`SklearnModelInfo.target_values` returned only the single "positive" class
for binary classifiers (e.g. `["malignant"]`), but
`JSONFiles.write_model_properties_json` requires either 0, 2, or >2 target
values, raising "Please provide all possible values for the target
variable, including a no-event value." for exactly 1. Since
`_register_open_source_model` passes `info.target_values` straight into
that function, registering any binary scikit-learn classifier via
`register_model()` crashed with a ValueError.

Return both classes (in `classes_` order, i.e. [non-event, event], matching
the ordering already used for `output_column_names`/`predict_proba`
columns) so binary classifiers register successfully.

Fixes sassoftware#200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueError when attempting to register scikit-learn model

1 participant