Compare precision and recall across decision thresholds
Binary classifier evaluation
Scikit-Learn · Function
Computes precision-recall pairs over score thresholds for binary classification. It accepts positive-class probabilities or classifier decision scores and returns precision, recall, and thresholds.
The function orders prediction scores as classification cutoffs and evaluates the resulting positive predictions at each cutoff. It returns precision and recall arrays together with the thresholds; the endpoint values make the curve complete even though they do not each correspond to a threshold.
sklearn.metrics.precision_recall_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=False)| Name | Type | Description |
|---|---|---|
y_true | array-like of shape (n_samples,) | True binary labels for the samples. |
y_score | array-like of shape (n_samples,) | Positive-class probabilities or non-thresholded decision scores. |
pos_label | int, float, bool or str, default=None | Label treated as positive when labels are not the default binary labels. |
sample_weight | array-like of shape (n_samples,), default=None | Per-sample weights used in the calculations. |
drop_intermediate | bool, default=False | Whether to drop some thresholds whose points are dominated by another point. |
Returns precision, recall, and thresholds arrays. Precision and recall have shape (n_thresholds + 1); thresholds contains the distinct score thresholds.
from sklearn.datasets import make_classification
from sklearn.metrics import precision_recall_curve
X, y = make_classification(
n_samples=100, n_features=4, n_informative=3,
random_state=0
)
scores = X[:, 0]
precision, recall, thresholds = precision_recall_curve(y, scores)
print(precision.shape, recall.shape, thresholds.shape)Computes the precision and recall arrays, plus the score thresholds used to produce the curve.
Binary classifier evaluation
Imbalanced classification
Threshold selection
It returns precision-recall pairs for different probability thresholds. Precision measures the fraction of predicted positives that are true positives, while recall measures the fraction of actual positives that are found.
Follow-up: What can be supplied instead of probabilities? A classifier's non-thresholded decision scores, such as values returned by decision_function, are also valid.
y_true contains the true binary labels, and y_score contains one score per sample. The score may be a positive-class probability estimate or a non-thresholded classifier decision measure.
Follow-up: Why must the score be one-dimensional here? The documented input shape is one score for each of the n_samples, so class-score matrices require a separate binary reduction first.
The function returns precision, recall, and thresholds. Precision and recall each have one more element than thresholds, with endpoint values that complete the curve.
Follow-up: How can intermediate points be reduced? The documented drop_intermediate option can remove thresholds whose points are dominated by another point, reducing plotting workload without changing the curve's shape.
PrecisionRecallDisplayaverage_precision_scoreroc_auc_scoreauc