Evaluate imbalanced classifiers
Class distributions are uneven and minority-class performance must matter as much as majority-class performance.
Scikit-Learn · Function
Computes balanced accuracy for binary or multiclass classification by averaging recall across classes. It is designed to provide a more informative score than ordinary accuracy on imbalanced datasets.
The function computes recall separately for every class and averages those recalls, preventing classes with more samples from dominating the result. With adjustment disabled, the score ranges from 0 to 1; enabling adjustment accounts for chance-level performance.
balanced_accuracy_score(y_true, y_pred, *, sample_weight=None, adjusted=False)| Name | Type | Description |
|---|---|---|
y_true | array-like of shape (n_samples,) | True target labels. |
y_pred | array-like of shape (n_samples,) | Estimated target labels returned by a classifier. |
sample_weight | array-like of shape (n_samples,), default=None | Sample weights. |
adjusted | bool, default=False | Whether to adjust the result for chance. |
float: Balanced accuracy score.
from sklearn.metrics import balanced_accuracy_score
y_true = [0, 1, 0, 0, 1, 0]
y_pred = [0, 1, 0, 0, 0, 1]
score = balanced_accuracy_score(y_true, y_pred)
print(score) # 0.625Computes the class-balanced score for the supplied true and predicted labels.
Class distributions are uneven and minority-class performance must matter as much as majority-class performance.
A classifier produces binary labels and both sensitivity and specificity should influence one metric.
Predictions should be compared against a chance-adjusted baseline.
It is the average of recall values computed independently for each class. This makes it useful when class frequencies are uneven, because frequent classes do not determine the score by themselves.
Follow-up: How does it differ from ordinary accuracy? Ordinary accuracy weights each sample equally, whereas balanced accuracy gives each class equal influence through per-class recall.
The signature is balanced_accuracy_score(y_true, y_pred, *, sample_weight=None, adjusted=False). y_true and y_pred contain the actual and estimated targets, while sample_weight optionally weights samples and adjusted controls chance adjustment.
Follow-up: Why is sample_weight keyword-only? The documented signature places it after *, so callers must name it explicitly, reducing accidental argument misplacement.
With adjusted=False, perfect classification gives 1 and the worst value is 0. With adjusted=True, the score is adjusted for chance so random performance scores 0 while perfect performance remains 1.
Follow-up: What is the adjusted lower bound for n classes? It is 1/(1 - n_classes), so the range can extend below zero for multiclass classification.
accuracy_scorerecall_scoreprecision_scoreclassification_report