Loading content...
In the evaluation of binary classification models, the Receiver Operating Characteristic (ROC) Curve provides a comprehensive visualization of a classifier's performance across all possible decision thresholds. The area under this curve (AUC) distills this rich information into a single scalar value that quantifies the classifier's ability to discriminate between positive and negative classes.
The ROC curve is constructed by plotting the True Positive Rate (TPR) against the False Positive Rate (FPR) at various threshold settings:
$$TPR = \frac{\text{True Positives}}{\text{True Positives} + \text{False Negatives}}$$
$$FPR = \frac{\text{False Positives}}{\text{False Positives} + \text{True Negatives}}$$
Interpreting the ROC-AUC Score:
Algorithm Overview: To compute the AUC, you must:
Your Task: Implement a function that takes binary ground truth labels and corresponding prediction probability scores, then returns the Area Under the ROC Curve. If all labels belong to a single class (all 0s or all 1s), return 0.0 since the ROC curve is undefined in this degenerate case.
y_true = [0, 0, 1, 1]
y_scores = [0.1, 0.4, 0.35, 0.8]0.75When predictions are sorted by score in descending order: scores=[0.8, 0.4, 0.35, 0.1] with corresponding labels=[1, 0, 1, 0]. The ROC curve traces through points as we lower the threshold:
• Threshold above 0.8: TPR=0, FPR=0 → Point (0, 0) • Threshold at 0.8: First sample (label=1) is positive → TPR=0.5, FPR=0 → Point (0, 0.5) • Threshold at 0.4: Second sample (label=0) is false positive → TPR=0.5, FPR=0.5 → Point (0.5, 0.5) • Threshold at 0.35: Third sample (label=1) is true positive → TPR=1.0, FPR=0.5 → Point (0.5, 1.0) • Threshold below 0.1: All samples classified positive → TPR=1.0, FPR=1.0 → Point (1, 1)
Using trapezoidal integration on points (0,0)→(0,0.5)→(0.5,0.5)→(0.5,1.0)→(1,1), the area equals 0.75.
y_true = [0, 0, 1, 1, 0, 1]
y_scores = [0.1, 0.2, 0.8, 0.9, 0.15, 0.85]1.0This represents a perfect classifier where all positive samples have higher scores than all negative samples. When sorted by score: [0.9, 0.85, 0.8, 0.2, 0.15, 0.1] with labels [1, 1, 1, 0, 0, 0]. The ROC curve goes straight from (0,0) → (0,0.33) → (0,0.67) → (0,1.0) → (0.33,1.0) → (0.67,1.0) → (1,1). The resulting area under this curve is exactly 1.0, indicating perfect discrimination ability.
y_true = [0, 1, 0, 1, 0, 1, 0, 1]
y_scores = [0.5, 0.5, 0.6, 0.4, 0.3, 0.7, 0.45, 0.55]0.66With mixed prediction quality: sorted scores are [0.7, 0.6, 0.55, 0.5, 0.5, 0.45, 0.4, 0.3] with labels [1, 0, 1, 0, 1, 0, 1, 0]. This classifier shows moderate discriminative ability—better than random (0.5) but far from perfect. The ROC curve traces a path that captures 66% of the total area, reflecting that while the model tends to assign higher scores to positive samples, there is significant overlap between the score distributions of both classes.
Constraints