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

Scikit-Learn · Function

precision_score

Compute classification precision: the ratio of true positives to all predicted positives. It supports binary, multiclass, and multilabel targets through configurable label selection and averaging.

How it works

precision_score divides true positives by all predicted positives, combining true positives and false positives. With the default binary averaging, it reports the precision of class 1; other averaging modes support multiclass and multilabel targets.

Syntax

sklearn.metrics.precision_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn')

Parameters

NameTypeDescription
y_true1d array-like, or label indicator array / sparse matrixGround-truth target values; sparse matrices are supported only for multilabel targets.
y_pred1d array-like, or label indicator array / sparse matrixEstimated target values produced by a classifier.
labelsarray-like, default=NoneLabels to include when average is not binary; also sets output order when average is None.
pos_labelint, float, bool or str, default=1Binary class reported when average='binary'; ignored otherwise.
average{'micro', 'macro', 'samples', 'weighted', 'binary'} or None, default='binary'Averaging strategy for binary, multiclass, or multilabel targets.
sample_weightarray-like, default=NonePer-sample weights.
zero_division{'warn', 0.0, 1.0, np.nan}, default='warn'Controls behavior when no positive predictions produce an undefined score.

Returns

float when average is not None, or an array of floats with shape (n_unique_labels,) when average=None.

Key points

  • Precision counts the fraction of predicted positives that are truly positive.
  • A high value indicates fewer false-positive positive predictions.
  • pos_label selects which binary class is treated as positive.
  • labels restricts scoring to chosen classes for non-binary averaging.
  • When no averaging is requested, labels also define the output order.
  • A scalar result represents binary precision or an aggregate multiclass precision, depending on the target.

Common mistakes

  • Do not expect pos_label to select a class when using a non-binary averaging mode.
  • Sparse target matrices are valid only for multilabel inputs.
  • Interpret the returned score as bounded from 0 to 1.

Example: Binary precision

from sklearn.metrics import precision_score

y_true = [0, 1, 1, 0]
y_pred = [0, 1, 0, 0]

score = precision_score(y_true, y_pred)
print(score)

This computes binary precision for the default positive class, label 1.

When to use it

Measure positive-prediction quality

Binary classification

Compare or aggregate class precision

Multiclass evaluation

Summarize label-wise prediction precision

Multilabel evaluation

Interview questions

QWhat does precision_score measure?

Precision is the ratio of true positives to true positives plus false positives: \(tp / (tp + fp)\). It measures how well a classifier avoids labeling negative samples as positive.

Follow-up: How would you evaluate multiclass precision? Use an averaging mode such as macro or weighted, or set average=None to obtain one value per label.

QWhat do average and pos_label control?

The default average is binary, so the function reports the precision for the positive class. The default positive class is selected by pos_label=1.

Follow-up: What changes when average=None? The function returns an array containing precision for the selected labels instead of one aggregate value.

QHow are multiclass and multilabel inputs handled?

For multiclass or multilabel targets, average determines whether scores are aggregated and how. The labels parameter selects the labels to score and determines their order when average=None.

Follow-up: Why might weighted averaging be useful? It weights each class's precision by its support, reflecting the number of true instances in each class.

  • recall_score
  • f1_score
  • precision_recall_fscore_support
  • average_precision_score

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