Click4Ai

71.

Hard

Implement the **ROC-AUC Score** (Area Under the Receiver Operating Characteristic Curve) using the trapezoidal rule.

Algorithm:

1. Sort samples by predicted scores in descending order

2. Sweep through thresholds, computing TPR and FPR at each point

3. Compute the area under the TPR vs FPR curve using the trapezoidal rule

Definitions:

TPR (True Positive Rate) = TP / (TP + FN)

FPR (False Positive Rate) = FP / (FP + TN)

Write a function roc_auc_score(y_true, y_scores) where:

  • `y_true`: Binary labels (0 or 1)
  • `y_scores`: Predicted scores/probabilities (higher = more likely positive)
  • Example:

    y_true = [0, 0, 1, 1, 0, 1]

    y_scores = [0.1, 0.4, 0.35, 0.8, 0.3, 0.9]

    roc_auc_score(y_true, y_scores) → 0.8889

    **Explanation:** AUC = 1.0 means perfect ranking, AUC = 0.5 means random ranking.

    Constraints:

  • Return a float between 0.0 and 1.0
  • Use the trapezoidal rule for area calculation
  • Test Cases

    Test Case 1
    Input: y_true=[0,0,1,1,0,1], y_scores=[0.1,0.4,0.35,0.8,0.3,0.9]
    Expected: 0.8889
    Test Case 2
    Input: y_true=[0,1], y_scores=[0.0,1.0]
    Expected: 1.0
    Test Case 3
    Input: y_true=[1,0], y_scores=[0.0,1.0]
    Expected: 0.0
    + 2 hidden test cases