PIXELBANKv8.2.1
Menu

ROC Curve Points

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 threshold is 0.

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:
  • First, we sort the predicted probabilities in descending order and use them as thresholds: [0.9,0.65,0.4,0.3][0.9, 0.65, 0.4, 0.3].
  • Then, we calculate the TPR and FPR at each threshold:
    • At threshold 0.90.9, only the first prediction is \geq threshold, so TP=1TP = 1, FP=0FP = 0, TN=1TN = 1, FN=1FN = 1, resulting in TPR=11+1=0.5TPR = \frac{1}{1+1} = 0.5 and FPR=00+1=0FPR = \frac{0}{0+1} = 0.
    • At threshold 0.650.65, the first two predictions are \geq threshold, so TP=2TP = 2, FP=0FP = 0, TN=1TN = 1, FN=0FN = 0, resulting in TPR=22+0=1TPR = \frac{2}{2+0} = 1 and FPR=00+1=0FPR = \frac{0}{0+1} = 0.
    • At threshold 0.40.4, the first three predictions are \geq threshold, so TP=2TP = 2, FP=1FP = 1, TN=0TN = 0, FN=0FN = 0, resulting in TPR=22+0=1TPR = \frac{2}{2+0} = 1 and FPR=11+0=0.5FPR = \frac{1}{1+0} = 0.5.
    • At threshold 00, all predictions are \geq threshold, so TP=2TP = 2, FP=2FP = 2, TN=0TN = 0, FN=0FN = 0, resulting in TPR=22+0=1TPR = \frac{2}{2+0} = 1 and FPR=22+0=1FPR = \frac{2}{2+0} = 1.
  • The final output includes the starting point (0,0)(0, 0) when the threshold is above the max prediction and the calculated points, resulting in [(0.0,0.0),(0.0,0.5),(0.0,1.0),(0.5,1.0),(1.0,1.0)][(0.0, 0.0), (0.0, 0.5), (0.0, 1.0), (0.5, 1.0), (1.0, 1.0)].

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
Editor

Test Results

0/0
Run code to see test results.