Evaluate classification correctness
You need a single proportion summarizing ordinary single-label classification results.
Scikit-Learn · Module
accuracy_score evaluates classification predictions by comparing true and predicted labels. It returns either the fraction of correct samples or their count, with optional sample weighting.
accuracy_score compares true and predicted class labels. It returns either the fraction of correct predictions or, when normalize is false, their count; sample_weight can weight individual samples.
sklearn.metrics.accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None)| Name | Type | Description |
|---|---|---|
y_true | array-like | True class labels or multilabel targets. |
y_pred | array-like | Predicted class labels or multilabel targets. |
normalize | bool | Whether to return the fraction of correct predictions (True) or their count (False). Default: True. |
sample_weight | array-like of shape (n_samples,) | Weights assigned to individual samples. Default: None. |
score: float. The fraction of correctly classified samples when normalize=True, otherwise the number of correctly classified samples.
from sklearn.metrics import accuracy_score
y_true = [0, 1, 1, 0]
y_pred = [0, 1, 0, 0]
print(accuracy_score(y_true, y_pred))
print(accuracy_score(y_true, y_pred, normalize=False))Compute the fraction and then the count of correct classification predictions.
You need a single proportion summarizing ordinary single-label classification results.
You need an integer total for reporting how many predictions succeeded.
Observations have unequal importance or represent different amounts of data.
Each example has a set of labels and all labels must be right together.
accuracy_score measures classification accuracy by comparing y_true with y_pred. With normalize=True, it returns the fraction of correctly classified samples; with normalize=False, it returns the number of correct samples.
Follow-up: How can individual observations contribute unequally? Pass sample_weight, an array-like weight for each sample, so the score is weighted.
Set normalize=False to obtain the count of correctly classified samples instead of the fraction. The default, normalize=True, returns the fraction.
Follow-up: What happens when normalize is omitted? It remains True, so the result is the fraction rather than the count.
In multilabel classification, accuracy_score uses subset accuracy. Every label in a sample's predicted set must exactly match the corresponding true label set for that sample to count as correct.
Follow-up: Why can this be stricter than label-wise accuracy? A single incorrect or missing label makes the whole sample incorrect under subset accuracy.
balanced_accuracy_scoref1_scoreprecision_scorerecall_scorezero_one_loss