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

Scikit-Learn · Function

accuracy_score

Computes classification accuracy by comparing predicted labels with true labels. It returns a fraction by default or a correct-sample count when normalize=False.

How it works

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.

Syntax

sklearn.metrics.accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None)

Parameters

NameTypeDescription
y_true1d array-like, or label indicator array / sparse matrixGround-truth labels. Accepts a one-dimensional array-like object, or a label-indicator array or sparse matrix for multilabel targets.
y_pred1d array-like, or label indicator array / sparse matrixPredicted labels produced by a classifier. Sparse matrices are supported only for multilabel targets.
normalizebool, default=TrueControls the output form: True returns a fraction; False returns the number of correct samples.
sample_weightarray-like of shape (n_samples,), default=NoneOptional per-sample weights.

Returns

score: float. With normalize=True, the fraction of correctly classified samples is returned; with normalize=False, the number of correctly classified samples is returned.

Key points

  • Use the function to evaluate how often predicted class labels agree with the ground truth.
  • Set normalize=False when you need a correct-prediction count instead of a proportion.
  • The default normalize=True produces the fraction of samples classified correctly.
  • The ideal normalized result is 1.0; without normalization, the ideal result equals the sample count.

Common mistakes

  • Multilabel accuracy is exact at the sample level: every label in a sample's predicted set must match the true set.
  • Sparse matrices are accepted only for multilabel targets.

Example: Compute classification accuracy

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.

When to use it

Standard classification evaluation

Assessing a multiclass or binary classifier on a held-out test set.

Count correct classifications

Reporting the number of correct predictions rather than a rate.

Exact multilabel agreement

Evaluating multilabel predictions where every label for a sample must match.

Interview questions

QWhat does accuracy_score compute, and what does normalize control?

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.

QWhat input forms can accuracy_score accept?

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.

QWhy would you pass sample_weight?

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_score
  • jaccard_score
  • precision_score

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