📘
ROC Curve Points
MediumModel Evaluation
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 ≥ threshold as positive:
TPR=TP+FNTP,FPR=FP+TNFP
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].
- Then, we calculate the TPR and FPR at each threshold:
- At threshold 0.9, only the first prediction is ≥ threshold, so TP=1, FP=0, TN=1, FN=1, resulting in TPR=1+11=0.5 and FPR=0+10=0.
- At threshold 0.65, the first two predictions are ≥ threshold, so TP=2, FP=0, TN=1, FN=0, resulting in TPR=2+02=1 and FPR=0+10=0.
- At threshold 0.4, the first three predictions are ≥ threshold, so TP=2, FP=1, TN=0, FN=0, resulting in TPR=2+02=1 and FPR=1+01=0.5.
- At threshold 0, all predictions are ≥ threshold, so TP=2, FP=2, TN=0, FN=0, resulting in TPR=2+02=1 and FPR=2+02=1.
- The final output includes the starting point (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)].
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
Python 3.13.1
Test Results
0/0Run code to see test results.