Model discrimination analysis
Comparing binary classifiers by their ranking behavior across operating points.
Scikit-Learn · Function
Computes the receiver operating characteristic for binary classification by evaluating scores at decision thresholds. Returns false-positive rates, true-positive rates, and the thresholds that produce them.
roc_curve evaluates a binary classifier across decision thresholds. It returns the false-positive rate and true-positive rate at each decreasing threshold, optionally using sample weights and omitting thresholds that would create collinear plotted points.
sklearn.metrics.roc_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=True)| Name | Type | Description |
|---|---|---|
y_true | array-like | True binary labels, shape (n_samples). |
y_score | array-like of shape (n_samples) | Scores for the positive class: probabilities, confidence values, or non-thresholded decision values. |
pos_label | int, float, bool or str, default=None | Label of the positive class; inferred when labels are -1/1 or 0/1. |
sample_weight | array-like of shape (n_samples,), default=None | Per-sample weights. |
drop_intermediate | bool, default=True | Whether to omit thresholds whose points are collinear with neighboring ROC points. |
A tuple containing fpr, tpr, and thresholds arrays: increasing false-positive rates, increasing true-positive rates, and decreasing decision thresholds.
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve
X, y = make_classification(random_state=0)
model = LogisticRegression(max_iter=1000).fit(X, y)
fpr, tpr, thresholds = roc_curve(y, model.predict_proba(X)[:, 1])
print(fpr)
print(tpr)
print(thresholds)Fits a binary classifier, supplies positive-class probabilities to roc_curve, and prints the false-positive rates, true-positive rates, and thresholds.
Comparing binary classifiers by their ranking behavior across operating points.
Choosing a threshold while examining the trade-off between false positives and true positives.
Evaluating observations that contribute unequally to the metric.
roc_curve returns three arrays: false-positive rates, true-positive rates, and the decision thresholds used to calculate them. The rates are ordered as thresholds decrease.
Follow-up: How would you reduce the number of plotted points? Set drop_intermediate=True, which removes thresholds whose ROC points are collinear with neighboring points.
y_score must be a continuous ranking or score for the positive class, such as a probability estimate or classifier confidence. It can also be a non-thresholded decision value.
Follow-up: What constraint applies to decision_function values? Values at least zero should represent the positive class.
The function is limited to binary classification. If labels are not -1/1 or 0/1, explicitly identify the positive class with pos_label.
Follow-up: How can class imbalance in observations be reflected? Supply sample_weight with one weight per sample.
roc_auc_scoreRocCurveDisplayRocCurveDisplay.from_estimatorRocCurveDisplay.from_predictions