Given binary labels and real-valued scores, compute the ROC curve: arrays of FPR, TPR, and thresholds.
The ROC (Receiver Operating Characteristic) curve plots True Positive Rate vs False Positive Rate across different classification thresholds. Your function should sweep through all unique score values as thresholds and compute the corresponding FPR and TPR values.
ROC Metrics:
True Positive Rate (TPR):
TPR=TP+FNTPFalse Positive Rate (FPR):
FPR=FP+TNFPy_true: array-like, shape (N,) - Binary labels {0,1}y_score: array-like, shape (N,) - Real-valued scores (logits or probabilities)Tuple of three lists/arrays: (fpr, tpr, thresholds)
fpr - False Positive Rates, starting at 0tpr - True Positive Rates corresponding to each FPR pointthresholds - Score thresholds in descending order, starting at infEach unique score value produces one threshold point. The first point is always (FPR=0, TPR=0, threshold=inf), representing the case where all samples are predicted negative. For tied scores, all items with the same score are grouped into a single threshold point.
Input: y_true = [0, 1], y_score = [0.1, 0.9]
Output: fpr = [0, 0, 1], tpr = [0, 1, 1], thresholds = [inf, 0.9, 0.1]
At threshold inf: all negative → (0,0). At 0.9: TP=1,FP=0 → (0,1). At 0.1: TP=1,FP=1 → (1,1).
Input: y_true = [1, 0, 1, 0], y_score = [0.9, 0.7, 0.4, 0.2]
Output: fpr = [0, 0, 0.5, 0.5, 1.0], tpr = [0, 0.5, 0.5, 1.0, 1.0], thresholds = [inf, 0.9, 0.7, 0.4, 0.2]
Sweeping down: at 0.9 (TP=1,FP=0), at 0.7 (TP=1,FP=1), at 0.4 (TP=2,FP=1), at 0.2 (TP=2,FP=2).
Sort indices by descending score using np.lexsort() to handle ties properly.
Use np.cumsum() on sorted labels to get cumulative true positives.
Find unique thresholds with np.where() and np.diff().
Sign in to take notes on this problem
Accepts: array
Accepts: array
Given binary labels and real-valued scores, compute the ROC curve: arrays of FPR, TPR, and thresholds.
The ROC (Receiver Operating Characteristic) curve plots True Positive Rate vs False Positive Rate across different classification thresholds. Your function should sweep through all unique score values as thresholds and compute the corresponding FPR and TPR values.
ROC Metrics:
True Positive Rate (TPR):
TPR=TP+FNTPFalse Positive Rate (FPR):
FPR=FP+TNFPy_true: array-like, shape (N,) - Binary labels {0,1}y_score: array-like, shape (N,) - Real-valued scores (logits or probabilities)Tuple of three lists/arrays: (fpr, tpr, thresholds)
fpr - False Positive Rates, starting at 0tpr - True Positive Rates corresponding to each FPR pointthresholds - Score thresholds in descending order, starting at infEach unique score value produces one threshold point. The first point is always (FPR=0, TPR=0, threshold=inf), representing the case where all samples are predicted negative. For tied scores, all items with the same score are grouped into a single threshold point.
Input: y_true = [0, 1], y_score = [0.1, 0.9]
Output: fpr = [0, 0, 1], tpr = [0, 1, 1], thresholds = [inf, 0.9, 0.1]
At threshold inf: all negative → (0,0). At 0.9: TP=1,FP=0 → (0,1). At 0.1: TP=1,FP=1 → (1,1).
Input: y_true = [1, 0, 1, 0], y_score = [0.9, 0.7, 0.4, 0.2]
Output: fpr = [0, 0, 0.5, 0.5, 1.0], tpr = [0, 0.5, 0.5, 1.0, 1.0], thresholds = [inf, 0.9, 0.7, 0.4, 0.2]
Sweeping down: at 0.9 (TP=1,FP=0), at 0.7 (TP=1,FP=1), at 0.4 (TP=2,FP=1), at 0.2 (TP=2,FP=2).
Sort indices by descending score using np.lexsort() to handle ties properly.
Use np.cumsum() on sorted labels to get cumulative true positives.
Find unique thresholds with np.where() and np.diff().
Sign in to take notes on this problem
Accepts: array
Accepts: array