Skip to content

Fix confusion_matrix crash on single-class predictions#85

Open
sniper-noob wants to merge 1 commit into
It-s-AI:mainfrom
sniper-noob:fix/confusion-matrix-single-class
Open

Fix confusion_matrix crash on single-class predictions#85
sniper-noob wants to merge 1 commit into
It-s-AI:mainfrom
sniper-noob:fix/confusion-matrix-single-class

Conversation

@sniper-noob
Copy link
Copy Markdown

When all labels or predictions are the same class, sklearn's confusion_matrix returns a 1x1 matrix. Unpacking into 4 variables crashes with ValueError.

Fix: Pass labels=[0, 1] to force 2x2 matrix, handle empty input, and guard average_precision_score against single-class y_true.

Example:
y_true = [0, 0, 0, 0], y_pred = [0.1, 0.2, 0.1, 0.3]
Before: confusion_matrix -> [[4]] -> ValueError on unpack
After: confusion_matrix(labels=[0,1]) -> [[4,0],[0,0]] -> works

When all labels or predictions are the same class, sklearn's
confusion_matrix returns a 1x1 matrix. Unpacking into 4 variables
crashes with ValueError.

Fix: Pass labels=[0, 1] to force 2x2 matrix, handle empty input,
and guard average_precision_score against single-class y_true.

Example:
  y_true = [0, 0, 0, 0], y_pred = [0.1, 0.2, 0.1, 0.3]
  Before: confusion_matrix -> [[4]] -> ValueError on unpack
  After:  confusion_matrix(labels=[0,1]) -> [[4,0],[0,0]] -> works
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.

1 participant