scikit-learn
ROC curve
machine learning
data visualization
python

Plotting a ROC curve in scikit yields only 3 points

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The Receiver Operating Characteristic (ROC) curve is a powerful tool used in binary classification problems to determine the performance of a classification model. The ROC curve is a graph showing the relationship between the true positive rate (sensitivity) and the false positive rate (1-specificity) across different thresholds. The area under this curve (AUC) provides a single measure of the model's ability to discriminate positive from negative classes. However, there are instances when plotting a ROC curve with scikit-learn yields only three discrete points. This article delves into the reasons behind this phenomenon, and provides clarification and guidance.

Understanding the Basics of ROC and AUC

The ROC curve is constructed by plotting the true positive rate (TPR) against the false positive rate (FPR) at various threshold settings. Here's the essential formulae to understand:

  • True Positive Rate (TPR): Also known as sensitivity or recall, it is defined as: TPR=True PositivesTrue Positives+False NegativesTPR = \frac{\text{True Positives}}{\text{True Positives} + \text{False Negatives}}
  • False Positive Rate (FPR): Defined as: FPR=False PositivesFalse Positives+True NegativesFPR = \frac{\text{False Positives}}{\text{False Positives} + \text{True Negatives}}

The AUC (Area Under the Curve) summarizes the ROC curve into a single value. A model with an AUC of 0.5 suggests no discriminative power, equivalent to random chance, while an AUC of 1.0 indicates perfect separability.

Scenarios Leading to Three Points on a ROC Curve

When using scikit-learn's roc_curve function, you might sometimes observe that the generated ROC curve consists of only three distinct points. This typically occurs in a few distinct scenarios:

  1. Binary Probabilities:
    • If the model outputs binary probabilities (e.g., only 0 and 1), the ROC curve will have limited points. A binary classifier producing only 0 or 1 is essentially a hard classifier without intermediate probability thresholds.
  2. Thresholds of Discrete Predictions:
    • When predictions are highly discrete, there might be very few unique thresholds to evaluate. For example, a perfect classifier or one that predicts the mean of the labels will also result in discrete ROC points.
  3. Small Dataset:
    • With extremely small datasets, the number of unique prediction scores is limited, which can also lead to fewer distinct threshold points on the curve.

Detailed Example

Consider a simple example using scikit-learn:

  • Consider using models that provide continuous output probability scores instead of discrete 0s and 1s.
  • Enhance the complexity or size of the dataset to generate a richer set of prediction scores.
  • Use cross-validation which might provide more varied predictions.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.