Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Function

precision_recall_curve

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.

How it works

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.

Syntax

sklearn.metrics.precision_recall_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=False)

Parameters

NameTypeDescription
y_truearray-like of shape (n_samples,)True binary labels for the samples.
y_scorearray-like of shape (n_samples,)Positive-class probabilities or non-thresholded decision scores.
pos_labelint, float, bool or str, default=NoneLabel treated as positive when labels are not the default binary labels.
sample_weightarray-like of shape (n_samples,), default=NonePer-sample weights used in the calculations.
drop_intermediatebool, default=FalseWhether to drop some thresholds whose points are dominated by another point.

Returns

Returns precision, recall, and thresholds arrays. Precision and recall have shape (n_thresholds + 1); thresholds contains the distinct score thresholds.

Key points

  • Use the function to inspect the precision–recall trade-off across possible score cutoffs.
  • Precision is the share of predicted positives that are truly positive.
  • Recall is the share of actual positives recovered by the predictions.
  • Pass positive-class probabilities or another compatible target score for each sample.
  • Decision scores from classifiers can be used without first converting them to probabilities.
  • Set drop_intermediate to true when you want to omit some non-essential curve points.

Common mistakes

  • This function does not directly support multiclass classification; use an appropriate one-vs-rest or one-vs-one strategy before applying it.
  • The precision output has an additional final endpoint whose value is 1.
  • The recall output has an additional final endpoint whose value is 0.

Example: Compute a precision-recall curve

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.

When to use it

Compare precision and recall across decision thresholds

Binary classifier evaluation

Choose a cutoff that balances missed positives and false alarms

Imbalanced classification

Inspect score cutoffs before deploying a classifier

Threshold selection

Interview questions

QWhat does precision_recall_curve compute?

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.

QWhat are the key inputs?

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.

QHow are the outputs aligned?

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.

  • PrecisionRecallDisplay
  • average_precision_score
  • roc_auc_score
  • auc

Source: precision_recall_curve — scikit-learn 1.9.1 documentation. Checked 2026-10-15.