Statistical note
Classification metrics from the confusion matrix
Accuracy, balanced accuracy, precision, recall, specificity, F1, MCC, and choosing metrics under class imbalance.
Binary predictions produce true positives , false positives , true negatives , and false negatives .
from sklearn.metrics import (
confusion_matrix, classification_report, balanced_accuracy_score,
matthews_corrcoef,
)
y_true = [0, 0, 0, 0, 1, 1, 1, 1]
y_pred = [0, 0, 0, 1, 0, 1, 1, 1]
print(confusion_matrix(y_true, y_pred))
print(classification_report(y_true, y_pred, digits=3))
print(balanced_accuracy_score(y_true, y_pred))
print(matthews_corrcoef(y_true, y_pred))
Case study: rare mitosis detection
Accuracy can remain high when a model misses most rare events. Recall measures captured mitoses; precision measures how many alerts are real. Balanced accuracy averages class recalls. The Matthews correlation coefficient summarizes all four cells and remains informative under imbalance.
Always define the positive class, averaging rule (micro, macro, or weighted), decision threshold, and unit of evaluation.
Functions: sklearn.metrics.confusion_matrix, precision_recall_fscore_support, balanced_accuracy_score, matthews_corrcoef, and classification_report.