Building the ROC Curve

Metrics & EvaluationHard+35 XP+25

Building the ROC Curve

A classifier that outputs scores isn't one classifier — it's a whole family of them, one per threshold you might pick. The ROC curve plots what happens across all of them at once.

At each threshold, everything scoring >= t is called positive, and you record two rates:

  • TPR (recall) = TP / (TP + FN) — of the real positives, how many did you catch?
  • FPR = FP / (FP + TN) — of the real negatives, how many did you wrongly flag?

Task: write roc_points(y_true, scores) where y_true holds 0/1 labels. Return a list of [fpr, tpr] pairs, each rounded to 4 decimal places, built exactly like this:

  1. Start the list with [0.0, 0.0] — the threshold above every score, where nothing is predicted positive.
  2. Then take the unique score values in descending order, and append one [fpr, tpr] pair per threshold.

The word unique is doing real work. Tied scores are indistinguishable to the model — it cannot separate them, so they must all cross the threshold together as a single step, not one at a time.

  • If there are no positive labels, treat TPR as 0.0; if there are no negatives, treat FPR as 0.0.

Draw the ROC Curve

Drag the threshold across its full range. Each stop plots one point.

Threshold: 1.00
FPR 0.00TPR 0.00

Examples

y_true = [1, 0, 1, 0]
scores = [0.9, 0.8, 0.4, 0.2]
returns [[0.0, 0.0], [0.0, 0.5], [0.5, 0.5], [0.5, 1.0], [1.0, 1.0]]
y_true = [1, 1, 0, 0]
scores = [0.9, 0.8, 0.4, 0.2]
returns [[0.0, 0.0], [0.0, 0.5], [0.0, 1.0], [0.5, 1.0], [1.0, 1.0]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run