ROC AUC from Scratch

medium
Unit 2 · Evaluation & Preprocessing0/3 to milestone

ROC AUC summarises how well a model ranks positives above negatives, across every possible threshold. It has a neat probabilistic meaning:

AUC is the probability that a randomly chosen positive gets a higher score than a randomly chosen negative.

So you can compute it by comparing every (positive, negative) pair: count 1 when the positive scores higher, 0.5 when they tie, and divide by the number of pairs.

Implement roc_auc(y_true, scores), where y_true contains 0s and 1s. If there are no positives or no negatives, AUC is undefined, so return float("nan").

Example

roc_auc([0, 0, 1, 1], [0.1, 0.4, 0.35, 0.8])   # 0.75
#evaluation#classification#numpy3 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
ROC AUC from Scratch · TechGradify