ROC-AUC Score

~20 mincode completion

Implement .

  • y_true is a 1D array of 0/1 labels.
  • y_score is a 1D array of real-valued scores (higher = more likely positive).
  • Return the ROC-AUC as a .
  • If only one class is present the metric is undefined, so return 0.5.

Hint: you do not need a loop over thresholds. twice gives you ordinal ranks; averaging ranks within each group of equal scores handles ties.

Examples

Perfect ranking: every positive outscores every negative -> 1.0

Input
roc_auc([0, 0, 1, 1], [0.1, 0.2, 0.8, 0.9])
Output
1

Perfectly inverted ranking -> 0.0

Input
roc_auc([1, 1, 0, 0], [0.1, 0.2, 0.8, 0.9])
Output
0

Constant score: all ranks tie -> 0.5

Input
roc_auc([0, 1, 0, 1], [0.5, 0.5, 0.5, 0.5])
Output
0.5

Hints

Hint 1

You need the index of the extreme value, not the value itself.

Hint 2

A common slip here: broke ties by index instead of averaging ranks.

Requirements

  • y_true: 1D array of 0/1 labels

  • y_score: 1D array of predicted scores (higher = more positive)

  • Return ROC-AUC as a float. Returns 0.5 if only one class is present.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~20 min

8 employers weight this skill

3 AI product companies, 2 frontier labs, 1 health and bio company, 1 big tech firm, 1 autonomy company. Top match scores 92.

Python
import numpy as np

def roc_auc(y_true: np.ndarray, y_score: np.ndarray) -> float:
    """
    Compute the ROC-AUC using the rank-sum formula.

    Args:
        y_true:  1D array of 0/1 labels
        y_score: 1D array of predicted scores (higher = more positive)

    Returns:
        ROC-AUC as a float. Returns 0.5 if only one class is present.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.