classification_report_imbalanced raises opaque IndexError on target_names length mismatch
Author: JayeshSuryavanshiCreated Jul 30, 2026Updated Aug 3, 2026
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 parameterExpected 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.12I'll open a PR with the fix.
Source: scikit-learn-contrib/imbalanced-learn