Fix: handle single-class classifier in threshold prediction (#14471) (#14473)

scikit-learn treats a single-class fit as binary, so predict_proba returns
[1 - p, p] for one class and argmax could index past classes_. Use only
column 0, the real class, so the confidence threshold still applies.
This commit is contained in:
Trenton H authored and GitHub committed 2026-10-16 10:47:58 -07:00
1 parent 1f3d451a3a
commit 0cb2cdbfdd
2 files changed
+48

No files matched your search

+4
View File
@@ -54,6 +54,10 @@ def _predict_with_threshold(classifier, X, threshold: float) -> int | None:
cutoff so that uncertain predictions are discarded rather than assigned.
"""
probas = classifier.predict_proba(X)[0]
if len(classifier.classes_) == 1:
# scikit-learn treats a single-class fit as binary, so predict_proba
# returns [1 - p, p] for one class. Column 0 is the real class.
probas = probas[:1]
best_idx = int(probas.argmax())
best_class = int(classifier.classes_[best_idx])