Standard classification evaluation
Assessing a multiclass or binary classifier on a held-out test set.
Scikit-Learn · Function
Computes classification accuracy by comparing predicted labels with true labels. It returns a fraction by default or a correct-sample count when normalize=False.
accuracy_score compares each predicted label with its corresponding ground-truth label. With the default normalization it returns the weighted or unweighted fraction of correct samples; disabling normalization returns the number of correct samples instead. For multilabel inputs, a sample is correct only when its complete predicted label set matches the true set.
sklearn.metrics.accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None)| Name | Type | Description |
|---|---|---|
y_true | 1d array-like, or label indicator array / sparse matrix | Ground-truth labels. Accepts a one-dimensional array-like object, or a label-indicator array or sparse matrix for multilabel targets. |
y_pred | 1d array-like, or label indicator array / sparse matrix | Predicted labels produced by a classifier. Sparse matrices are supported only for multilabel targets. |
normalize | bool, default=True | Controls the output form: True returns a fraction; False returns the number of correct samples. |
sample_weight | array-like of shape (n_samples,), default=None | Optional per-sample weights. |
score: float. With normalize=True, the fraction of correctly classified samples is returned; with normalize=False, the number of correctly classified samples is returned.
from sklearn.metrics import accuracy_score
true = [0, 1, 2, 3]
predicted = [0, 2, 1, 3]
print(accuracy_score(true, predicted))
print(accuracy_score(true, predicted, normalize=False))The first call returns the fraction of correct predictions; the second returns their count.
Assessing a multiclass or binary classifier on a held-out test set.
Reporting the number of correct predictions rather than a rate.
Evaluating multilabel predictions where every label for a sample must match.
It measures classification accuracy by comparing predicted labels with ground-truth labels. By default it returns the fraction of correctly classified samples; setting normalize=False returns the count instead.
Follow-up: How does it behave for multilabel classification? It uses subset accuracy, so a sample counts only when its entire predicted label set exactly matches the true set.
y_true contains the correct labels and y_pred contains the labels produced by a classifier. Both may be one-dimensional array-like values or multilabel indicator arrays or sparse matrices.
Follow-up: What restriction applies to sparse inputs? Sparse matrices are supported only when the labels are multilabel.
sample_weight supplies one weight per sample and allows the accuracy calculation to account for differing sample importance. Without it, samples contribute equally.
Follow-up: What does normalize=False change when weights are supplied? It returns the weighted number of correctly classified samples rather than a normalized fraction.
top_k_accuracy_scorejaccard_scoreprecision_score