v9.1.1
Menu

ROC Curve Points

mediumFREEModel EvaluationMachine Learning 1

Compute the ROC curve points (TPR and FPR at each threshold).

Given true labels and predicted probabilities, compute the True Positive Rate (TPR) and False Positive Rate (FPR) at each unique threshold.

For each unique predicted probability (used as threshold, sorted descending), classify all predictions ≥\geq threshold as positive:

TPR=TPTP+FN,FPR=FPFP+TNTPR = \frac{TP}{TP + FN}, \quad FPR = \frac{FP}{FP + TN}

Return a list of (fpr, tpr) tuples rounded to 4 decimal places, starting from (0, 0) when threshold is above the max prediction, ending at (1, 1) when every prediction is classified positive. The point (1, 1) appears exactly once: the lowest threshold already produces it, so do not append a second copy.

Example

Input
y_true = [1, 0, 1, 0]
y_scores = [0.9, 0.4, 0.65, 0.3]
Output
[(0.0, 0.0), (0.0, 0.5), (0.0, 1.0), (0.5, 1.0), (1.0, 1.0)]
Reasoning
  • y_true says items 1 and 3 are real positives, so there are P=2P = 2 positives and N=2N = 2 negatives; y_scores are the model's confidences.
  • The unique scores, highest first, give the thresholds 0.9,0.65,0.4,0.30.9, 0.65, 0.4, 0.3. Above 0.90.9 nothing is predicted positive, which is the starting point (0,0)(0, 0).
  • At 0.90.9 only the item scored 0.90.9 (a real positive) passes: TP=1TP = 1, FP=0FP = 0, so TPR=12=0.5TPR = \frac{1}{2} = 0.5, FPR=02=0FPR = \frac{0}{2} = 0, giving (0.0,0.5)(0.0, 0.5).
  • At 0.650.65 the second positive also passes: TP=2TP = 2, FP=0FP = 0, giving (0.0,1.0)(0.0, 1.0).
  • At 0.40.4 the first negative passes: TP=2TP = 2, FP=1FP = 1, FPR=12FPR = \frac{1}{2}, giving (0.5,1.0)(0.5, 1.0).
  • At 0.30.3, the lowest score, every item is predicted positive: TP=2TP = 2, FP=2FP = 2, giving (1.0,1.0)(1.0, 1.0). That is already the end point, so it appears only once.
  • Notice that both positives outrank both negatives, so the curve reaches TPR=1TPR = 1 before any false positive appears: a perfect ranking.

Constraints

  • y_true: list of 0s and 1s
  • y_scores: list of predicted probabilities
  • Return sorted list of (fpr, tpr) tuples from (0,0) to (1,1)
  • Round to 4 decimal places

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.