ROC curve
An ROC curve evaluates a binary classifier across all classification thresholds by plotting True Positive Rate (TPR, recall) against False Positive Rate (FPR). The Area Under the ROC Curve (AUC-ROC) measures pure ranking discrimination—the probability that a randomly selected positive example receives a higher predicted score than a randomly selected negative example.
Study Card: ROC Curve (Receiver Operating Characteristic Curve)
Direct Answer
An ROC (Receiver Operating Characteristic) curve visualizes a binary classifier’s threshold-free discrimination by plotting True Positive Rate (TPR) against False Positive Rate (FPR) as the decision threshold varies from to :
The AUC (Area Under the Curve) summarizes the ROC curve into a single value in :
- AUC = 1.0: Perfect ranking separation between positives and negatives.
- AUC = 0.5: Random guessing (along the diagonal ).
- AUC < 0.5: Worse than random (inverting predictions improves performance).
Probabilistically, AUC equals the concordance probability (with ties weighted ): the chance a random positive sample ranks above a random negative sample. While ROC-AUC is class-prior invariant, Precision-Recall (PR) curves should also be inspected on severely imbalanced datasets because a huge number of true negatives can keep FPR deceptively low despite many false positives.
Key Terms
- True Positive Rate (TPR / Sensitivity / Recall): — fraction of actual positives correctly flagged.
- False Positive Rate (FPR / Fall-out): — fraction of actual negatives falsely flagged.
- Classification Threshold: Cutoff applied to predicted probabilities or scores to obtain binary predictions.
- AUC (Area Under the ROC Curve): Probability a positive instance ranks above a negative instance across all thresholds.
- ROC Curve: Parametric plot of sweeping across thresholds .
Example
In medical screening, raising sensitivity (TPR) captures more diseased patients, at the expense of raising false alarms (FPR) among healthy patients. Comparing two candidate models via their ROC curves reveals which achieves higher TPR for any tolerable FPR budget, and a higher AUC indicates superior ranking separation across thresholds prior to picking a deployment operating point.
Code Implementation
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, auc, roc_auc_score
from sklearn.datasets import make_classification
# Generate sample classification data
X, y = make_classification(n_samples=1000, n_features=2, n_informative=2, n_redundant=0, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Train classifier
model = LogisticRegression()
model.fit(X_train, y_train)
# Get predicted probabilities for class 1
y_prob = model.predict_proba(X_test)[:, 1]
# Compute ROC curve and AUC
fpr, tpr, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(fpr, tpr)
print(f"AUC using auc function: {roc_auc:.4f}")
print(f"AUC using roc_auc_score function: {roc_auc_score(y_test, y_prob):.4f}")
# Plot ROC curve
plt.figure(figsize=(8, 6))
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (AUC = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--', label='Random Baseline')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC)')
plt.legend(loc="lower right")
plt.grid(True)
plt.show()
Related Concepts
- Precision-Recall Curve: Plots Precision vs. Recall without involving TN; strongly preferred over ROC when positives are very rare (e.g., fraud or anomaly detection) and FP volume matters directly.
- Confusion Matrix: Provides the TP, FP, TN, FN counts at a chosen threshold corresponding to one point on the ROC curve.
- Threshold Selection: Selecting an operating point on the ROC curve using domain cost ratios or Youden’s .
- AUC Interpretation: Equivalent to the Mann-Whitney statistic / probability of ranking a random positive above a random negative.
- AUC vs. Log Loss (Calibration): ROC-AUC depends solely on rank ordering of scores and is invariant to any strictly monotonic probability transformation (e.g., dividing probabilities by 10 leaves AUC unchanged while ruining Log Loss). Thus a model can achieve high AUC with poor probability calibration, whereas Log Loss directly penalizes miscalibration.