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.
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:
- False Positive Rate (FPR): Defined as:
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:
- 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.
- 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.
- 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
- Plotting decision boundary for High Dimension Data
- Plotting learning curve in keras gives KeyError 'val_acc
- Pointers to some good SVM Tutorial
- Pointers to some good SVM Tutorial
- Plotting in a non-blocking way with Matplotlib
- Plotting numpy array using Seaborn
- Poetry fails to install tensorflow
- Pool.apply_async nested function is not executed
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.