Skip to content

classification_report_imbalanced raises opaque IndexError on target_names length mismatch #1191

Description

@JayeshSuryavanshi

Describe the bug

imblearn.metrics.classification_report_imbalanced raises an opaque IndexError: list index out of range when target_names has a different length than the number of classes, whereas the scikit-learn function it mirrors — sklearn.metrics.classification_report — raises a clear, actionable ValueError on the same input.

Steps/Code to Reproduce

from imblearn.metrics import classification_report_imbalanced
from sklearn.metrics import classification_report

y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]

classification_report_imbalanced(y_true, y_pred, target_names=["a", "b"])
# -> IndexError: list index out of range

classification_report(y_true, y_pred, target_names=["a", "b"])
# -> ValueError: Number of classes, 3, does not match size of target_names, 2.
#    Try specifying the labels parameter

Expected behavior

A clear ValueError matching scikit-learn, e.g. "Number of classes, 3, does not match size of target_names, 2. Try specifying the labels parameter".

Versions

imbalanced-learn 0.15.dev0
scikit-learn 1.9.0
Python 3.12

I'll open a PR with the fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions