The ROC curve shows a classifier’s performance at every threshold, and the AUC (area under the curve) summarises it in one number. Unlike accuracy, AUC does not depend on a chosen threshold.
What You Will Learn
- TPR and FPR
- How the ROC curve is built
- Interpret AUC values
- Compare models with AUC
- Precision–recall curves for imbalanced data
Prerequisites
TPR and FPR
| Rate | Formula | Also called |
|---|---|---|
| True positive rate (TPR) | TP / (TP + FN) | Recall, sensitivity |
| False positive rate (FPR) | FP / (FP + TN) | 1 − specificity |
Lowering the threshold catches more positives (TPR ↑) but also creates more false alarms (FPR ↑). The ROC curve plots TPR against FPR as the threshold moves from 1 to 0.
Compare Models with AUC
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.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import roc_auc_score
X, y = load_breast_cancer(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
models = {"Logistic": make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)),
"Naive Bayes": GaussianNB(),
"Tree depth 2": DecisionTreeClassifier(max_depth=2, random_state=0)}
for name, m in models.items():
proba = m.fit(X_tr, y_tr).predict_proba(X_te)[:, 1] # probabilities, not labels!
print(f"{name:13s}", round(roc_auc_score(y_te, proba), 3))
Logistic 0.998 Naive Bayes 0.989 Tree depth 2 0.938
Explanation: AUC needs scores or probabilities, not 0/1 predictions. Logistic regression ranks almost every benign tumour above every malignant one. A shallow tree only produces a few distinct probability values, so its curve is coarse and its AUC lower.
| AUC | Meaning |
|---|---|
| 1.0 | Perfect ranking |
| 0.9–1.0 | Excellent |
| 0.8–0.9 | Good |
| 0.7–0.8 | Fair |
| 0.5 | No better than random guessing |
| < 0.5 | Worse than random (labels may be flipped) |
Plot the ROC Curve
from sklearn.metrics import RocCurveDisplay
import matplotlib.pyplot as plt
ax = plt.gca()
for name, m in models.items():
RocCurveDisplay.from_estimator(m, X_te, y_te, name=name, ax=ax)
ax.plot([0, 1], [0, 1], "k--") # random baseline
plt.show()
Imbalanced Data: Use Precision–Recall Too
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score, average_precision_score
Xi, yi = make_classification(5000, weights=[0.98], n_informative=4, flip_y=0.01, random_state=1)
a, b, c, d = train_test_split(Xi, yi, test_size=0.3, random_state=0, stratify=yi)
m = LogisticRegression(max_iter=1000).fit(a, c)
p = m.predict_proba(b)[:, 1]
print(round(yi.mean(), 3), round(accuracy_score(d, m.predict(b)), 3),
round(roc_auc_score(d, p), 3), round(average_precision_score(d, p), 3))
0.025 0.977 0.755 0.176
Explanation: Only 2.5% of rows are positive. Accuracy (97.7%) is barely above always predicting “negative” (97.5%). ROC AUC of 0.755 looks fair, but average precision (area under the precision–recall curve) of 0.176 shows how hard it really is to find the rare positives – though still 7× better than the 0.025 random baseline. For fraud, rare disease or defects, report PR-AUC as well.
Series
ML fundamentals series: What is ML · Types of ML · Train/Test Split · Overfitting & Bias-Variance · Regularization · Confusion Matrix · ROC & AUC · Cross-Validation · Ensembles · DBSCAN
Interview Questions
Q: What does the ROC curve plot?
True positive rate vs false positive rate across all thresholds.
Q: What does an AUC of 0.5 mean?
The model ranks no better than random.
Q: ROC-AUC vs PR-AUC?
ROC-AUC can look optimistic on heavily imbalanced data; PR-AUC focuses on the positive class.
Practice Exercises
- Find the threshold on the ROC curve that keeps FPR below 5% with the highest TPR (use roc_curve).
- Add a random forest to the comparison.
Summary
ROC curves show TPR vs FPR at every threshold; AUC summarises ranking quality from 0.5 (random) to 1 (perfect). For rare positives, also use precision–recall curves.
Machine Learning Tutorials, Courses and Certifications