Problems
Loading...
1 / 1

Compute ROC Curve from Scores

Metrics & Evaluation
Hard

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=TPTP+FN\mathrm{TPR} = \frac{\mathrm{TP}}{\mathrm{TP} + \mathrm{FN}}

False Positive Rate (FPR):

FPR=FPFP+TN\mathrm{FPR} = \frac{\mathrm{FP}}{\mathrm{FP} + \mathrm{TN}}

Function Arguments

  • y_true: array-like, shape (N,) - Binary labels {0,1}
  • y_score: array-like, shape (N,) - Real-valued scores (logits or probabilities)

Returns

Tuple of three lists/arrays: (fpr, tpr, thresholds)

  • fpr - False Positive Rates, starting at 0
  • tpr - True Positive Rates corresponding to each FPR point
  • thresholds - Score thresholds in descending order, starting at inf

Each 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.

Loading visualization...

Examples

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).

Hint 1

Sort indices by descending score using np.lexsort() to handle ties properly.

Hint 2

Use np.cumsum() on sorted labels to get cumulative true positives.

Hint 3

Find unique thresholds with np.where() and np.diff().

Requirements

  • Return tuple: (fpr, tpr, thresholds) all same length
  • Sort by descending y_score; sweep unique score values
  • Include endpoints: (FPR=0,TPR=0) and (FPR=1,TPR=1)
  • Thresholds are the actual score values used as cutoffs
  • Treat ties correctly (group identical scores)
  • Vectorized; no Python loops over samples

Constraints

  • N ≤ 1e6; NumPy only
  • Time limit: 500ms; Memory: 256MB
Try Similar Problems