Repository navigation
Description
Found while fixing #435. Distinct bug, distinct fix.
What happens
logistic_predict returns argmax class labels. Several callers pass its output to logistic_decide, which thresholds its input as a probability.
lib/scikit/model_selection.flow:247:
let probs: ptr<f32> = logistic_predict(model, X_test)
let preds: ptr<f32> = logistic_decide(model, probs, n_test, 0.5)
The variable is even named probs. The same pattern is at lines 382, 735, 739, 825 and 830, and in pipeline_predict_proba in lib/scikit/pipeline.flow, whose result pipeline_decide then thresholds.
Why it has not been noticed
For classes {0, 1} thresholding labels at 0.5 is the identity: label 0 is below the threshold and maps to classes[0], label 1 is above and maps to classes[1]. Correct by coincidence, and every fixture in the repo uses 0/1 labels.
For any other labelling it is wrong. With classes {2, 7} both labels exceed 0.5, so every sample is predicted as classes[1], i.e. 7. With classes {-1, 1} every negative label falls below and every positive above, which happens to work again. The failure is silent and depends entirely on the numeric values of the class labels.
Blast radius
cross_val_scoreand the other affected functions inmodel_selection.flowreport accuracy against predictions that are only correct when labels happen to be 0/1.pipeline_predict_probareturns labels, so its name is wrong regardless of the threshold issue, andpipeline_decideinherits it.- Four tests use these paths with 0/1 labels, so they pass and would keep passing.
The fix
Whatever it is, it should not be a local patch at each of the seven sites. The real problem is that logistic_predict (argmax labels) and logistic_predict_proba (probabilities) return values that are indistinguishable at the type level, and logistic_decide cannot tell which it was handed.
Two options:
- Change the callers to use
logistic_predict_probafor binary problems andlogistic_predictdirectly for multiclass, dropping thelogistic_decidecall where it is meaningless. This is the honest reading: cross-validation wants a prediction, not a thresholded probability. - Keep
logistic_decidein the path but feed it actual probabilities.
Note #435 just changed logistic_predict_proba to return P(classes[1]) to match scikit-learn, so option 1 or 2 must be written against the post-#435 semantics.
Test that would catch it
Any cross-validation test with class labels other than 0/1, for example {2, 7}, asserting the reported accuracy is not degenerate. All existing fixtures use 0/1, which is why seven call sites carry this untouched.
Activity
fix: iris demo calibrates on probabilities, not labels (#449)
Merge pull request #456 from godofecht/fix/449-labels-thresholded-as-…