Measure positive-prediction quality
Binary classification
Scikit-Learn · Function
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.
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.
sklearn.metrics.precision_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn')| Name | Type | Description |
|---|---|---|
y_true | 1d array-like, or label indicator array / sparse matrix | Ground-truth target values; sparse matrices are supported only for multilabel targets. |
y_pred | 1d array-like, or label indicator array / sparse matrix | Estimated target values produced by a classifier. |
labels | array-like, default=None | Labels to include when average is not binary; also sets output order when average is None. |
pos_label | int, float, bool or str, default=1 | Binary 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_weight | array-like, default=None | Per-sample weights. |
zero_division | {'warn', 0.0, 1.0, np.nan}, default='warn' | Controls behavior when no positive predictions produce an undefined score. |
float when average is not None, or an array of floats with shape (n_unique_labels,) when average=None.
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.
Binary classification
Multiclass evaluation
Multilabel evaluation
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.
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.
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_scoref1_scoreprecision_recall_fscore_supportaverage_precision_score