Sub topic 13
Measuring performance: accuracy, precision, recall, ROC AUC
This chapter assumes you can fit a logistic regression, split data into train and test sets, and read a predicted probability. Here you will learn which numbers describe how well a classifier works, and when each one misleads.
Start with the confusion matrix
A classifier turns a probability into a label using a threshold, usually 0.5. Every test example then lands in one of four cells.
| Row | Predicted positive | Predicted negative |
|---|---|---|
| Actually positive | True positive (TP) | False negative (FN) |
| Actually negative | False positive (FP) | True negative (TN) |
All the metrics below are ratios of these four counts.
Accuracy, precision, recall
- Accuracy = (TP + TN) / total. It is the share of correct predictions.
- Precision = TP / (TP + FP). Of the examples you called positive, how many were positive.
- Recall = TP / (TP + FN). Of the truly positive examples, how many you found.
- F1 is the harmonic mean of precision and recall. It is low if either one is low.
Accuracy hides trouble when classes are unbalanced. If 1% of transactions are fraud, a model that always predicts "not fraud" scores 99% accuracy and finds no fraud. Check precision and recall for the positive class.
ROC curve and AUC
Precision and recall describe one threshold. The ROC curve shows all of them. For each threshold it plots the true positive rate (the same as recall) against the false positive rate, FP / (FP + TN).
ROC AUC is the area under that curve. It equals the probability that a randomly chosen positive example gets a higher score than a randomly chosen negative one. A value of 0.5 means random ranking and 1.0 means perfect ranking. AUC uses probabilities, not labels, so it does not depend on the threshold.
Compute them in scikit-learn
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score, classification_report, confusion_matrix, roc_auc_score
)
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=0
)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)
proba = model.predict_proba(X_test)[:, 1]
pred = model.predict(X_test)
print(confusion_matrix(y_test, pred))
print("accuracy", accuracy_score(y_test, pred))
print("roc auc", roc_auc_score(y_test, proba))
print(classification_report(y_test, pred))confusion_matrix returns rows as actual classes and columns as predicted classes, in label order. Pass proba, not pred, to roc_auc_score. Passing hard labels collapses the curve to one point and gives a misleading value.
Common errors
- Reporting accuracy alone on imbalanced data.
- Computing AUC from predicted labels instead of probabilities.
- Reading precision and recall for the wrong class. The report lists both classes, so find the one you care about.
- Tuning the threshold on the test set. Use a separate validation set, or the test score will be too optimistic.
Exercise
A test set has 95 negatives and 5 positives. A model predicts 4 positives, 3 of them correct, and 2 positives are missed. Compute precision and recall for the positive class.
Show answer
TP = 3, FP = 1, FN = 2. Precision = 3 / (3 + 1) = 0.75. Recall = 3 / (3 + 2) = 0.6.
Exercise
Why can a model have a high ROC AUC and still show poor recall at the 0.5 threshold?
Show answer
AUC measures how well the model ranks positives above negatives across all thresholds. Recall is measured at one threshold. If the probabilities are shifted low, the ranking is good but 0.5 is too high a cutoff, so many positives fall below it. Lowering the threshold would raise recall.