TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Compute ROC Curve from Scores

Metrics & Evaluation
Hard

Build a receiver operating characteristic curve from binary labels and prediction scores. Sort samples by descending score and group tied scores into one threshold. At each threshold compute:

TPR⁡=TPP\operatorname{TPR} = \frac{TP}{P}TPR=PTP​ FPR⁡=FPN\operatorname{FPR} = \frac{FP}{N}FPR=NFP​

Here, PPP and NNN are the total positive and negative counts. Begin with fpr=0, tpr=0, and an infinite threshold, then append one point per unique score. Return fpr, tpr, and thresholds as NumPy arrays in a dictionary.

Loading visualization...

Examples

Input: y_true = [0, 1], y_score = [0.1, 0.9]

Output: {"fpr": [0.0, 0.0, 1.0], "tpr": [0.0, 1.0, 1.0], "thresholds": ["inf", 0.9, 0.1]}

Explanation: The positive sample crosses first, followed by the negative sample at the lower threshold.

Input: y_true = [1, 0, 1, 0], y_score = [0.9, 0.7, 0.4, 0.2]

Output: {"fpr": [0.0, 0.0, 0.5, 0.5, 1.0], "tpr": [0.0, 0.5, 0.5, 1.0, 1.0], "thresholds": ["inf", 0.9, 0.7, 0.4, 0.2]}

Hint 1

Use cumulative positive and negative counts after a stable descending sort.

Hint 2

Select the final index of each tied-score group with np.diff(sorted_scores).

Requirements

  • Sort samples by descending score
  • Group equal scores into one threshold point
  • Prepend the all-negative point with an infinite threshold
  • Return exactly fpr, tpr, and thresholds in a dictionary of NumPy arrays

Constraints

  • y_true and y_score are equal-length nonempty one-dimensional lists
  • y_true contains both 0 and 1
  • Use NumPy only
Try Similar Problems
AucClassification MetricsExpected Calibration ErrorIsotonic CalibrationConfusion Matrix Norm

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Compute ROC Curve from Scores

Metrics & Evaluation
Hard

Build a receiver operating characteristic curve from binary labels and prediction scores. Sort samples by descending score and group tied scores into one threshold. At each threshold compute:

TPR⁡=TPP\operatorname{TPR} = \frac{TP}{P}TPR=PTP​ FPR⁡=FPN\operatorname{FPR} = \frac{FP}{N}FPR=NFP​

Here, PPP and NNN are the total positive and negative counts. Begin with fpr=0, tpr=0, and an infinite threshold, then append one point per unique score. Return fpr, tpr, and thresholds as NumPy arrays in a dictionary.

Loading visualization...

Examples

Input: y_true = [0, 1], y_score = [0.1, 0.9]

Output: {"fpr": [0.0, 0.0, 1.0], "tpr": [0.0, 1.0, 1.0], "thresholds": ["inf", 0.9, 0.1]}

Explanation: The positive sample crosses first, followed by the negative sample at the lower threshold.

Input: y_true = [1, 0, 1, 0], y_score = [0.9, 0.7, 0.4, 0.2]

Output: {"fpr": [0.0, 0.0, 0.5, 0.5, 1.0], "tpr": [0.0, 0.5, 0.5, 1.0, 1.0], "thresholds": ["inf", 0.9, 0.7, 0.4, 0.2]}

Hint 1

Use cumulative positive and negative counts after a stable descending sort.

Hint 2

Select the final index of each tied-score group with np.diff(sorted_scores).

Requirements

  • Sort samples by descending score
  • Group equal scores into one threshold point
  • Prepend the all-negative point with an infinite threshold
  • Return exactly fpr, tpr, and thresholds in a dictionary of NumPy arrays

Constraints

  • y_true and y_score are equal-length nonempty one-dimensional lists
  • y_true contains both 0 and 1
  • Use NumPy only
Try Similar Problems
AucClassification MetricsExpected Calibration ErrorIsotonic CalibrationConfusion Matrix Norm

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.