WRITE · TRACE · UNDERSTAND

Treat tied ranking scores explicitly in ROC AUC

Implement roc_auc(labels,scores) for equal nonempty lists, binary integer labels and finite scores.

Read lesson: Treat tied ranking scores explicitly in ROC AUC →

Your experiment

Implement roc_auc(labels,scores) for equal nonempty lists, binary integer labels and finite scores. Compute exact positive-negative pair credit, half for ties. Reject single-class data.

Python runs in your browser. Your draft stays on this device.

What to look for

The quadratic reference is transparent for tiny evaluation fixtures. Large datasets should use a tie-aware rank algorithm. A high AUC can coexist with poor calibration or unacceptable errors at the chosen threshold.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Treat tied ranking scores explicitly in ROC AUC works

The idea

ROC AUC measures ranking, not decisions. It is the probability that a randomly chosen positive example gets a higher score than a randomly chosen negative one. A value of 1.0 means every positive outranks every negative, 0.5 is what random scores give, and 0 means the ranking is exactly reversed.

roc_auc(labels, scores) computes that definition directly. It separates the scores into pos and neg, compares every positive with every negative, and gives 1 for a win, 0.5 for a tie and 0 for a loss. Counting a tie as half is what makes the answer equal the area under the ROC curve. With only one class there are no pairs to compare, so the function raises ValueError.

How it works

  1. Validate the input: equal nonempty lists, integer labels of 0 or 1, and finite scores.
  2. Collect the scores of positive rows in pos and the scores of negative rows in neg.
  3. Raise ValueError if either list is empty, because AUC is undefined for a single class.
  4. For every pair of one positive and one negative, add 1 when p > n and 0.5 when p == n.
  5. Divide the total credit by len(pos) * len(neg), the number of pairs.

What it costs

Time
O(P·N)
Space
O(n)

The Lab compares all P positives with all N negatives; sorting the scores and working from ranks gives the same number in O(n log n).

Where it is used

  • scikit-learn's roc_auc_score returns the same value, half credit for ties included, by integrating the ROC curve.
  • The Mann-Whitney U statistic from scipy.stats.mannwhitneyu, divided by the number of positive-negative pairs, equals this AUC.
  • Clinical risk models report this number as the c-statistic: the chance that a patient with the outcome is scored above one without.

Watch out for

  • Giving tied pairs full or zero credit: a model that outputs one constant score should get 0.5, not 1.0 or 0.
  • Returning 0.5 or 0 for single-class data instead of raising an error; the metric has no pairs to compare.
  • Reading a high AUC as good probabilities. AUC depends only on the order of the scores, so badly calibrated scores can still rank well.