16
from sklearn.metrics import accuracy_score
22
Accuracy is the proportion of correct predictions among the total number of cases processed. It can be computed with:
23
Accuracy = (TP + TN) / (TP + TN + FP + FN)
32
_KWARGS_DESCRIPTION = """
34
predictions (`list` of `int`): Predicted labels.
35
references (`list` of `int`): Ground truth labels.
36
normalize (`boolean`): If set to False, returns the number of correctly classified samples. Otherwise, returns the fraction of correctly classified samples. Defaults to True.
37
sample_weight (`list` of `float`): Sample weights Defaults to None.
40
accuracy (`float` or `int`): Accuracy score. Minimum possible value is 0. Maximum possible value is 1.0, or the number of examples input, if `normalize` is set to `True`.. A higher score means higher accuracy.
44
Example 1-A simple example
45
>>> accuracy_metric = datasets.load_metric("accuracy")
46
>>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0])
50
Example 2-The same as Example 1, except with `normalize` set to `False`.
51
>>> accuracy_metric = datasets.load_metric("accuracy")
52
>>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], normalize=False)
56
Example 3-The same as Example 1, except with `sample_weight` set.
57
>>> accuracy_metric = datasets.load_metric("accuracy")
58
>>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], sample_weight=[0.5, 2, 0.7, 0.5, 9, 0.4])
60
{'accuracy': 0.8778625954198473}
66
title={Scikit-learn: Machine Learning in {P}ython},
67
author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
68
and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
69
and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
70
Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
71
journal={Journal of Machine Learning Research},
79
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
80
class Accuracy(datasets.Metric):
82
return datasets.MetricInfo(
83
description=_DESCRIPTION,
85
inputs_description=_KWARGS_DESCRIPTION,
86
features=datasets.Features(
88
"predictions": datasets.Sequence(datasets.Value("int32")),
89
"references": datasets.Sequence(datasets.Value("int32")),
91
if self.config_name == "multilabel"
93
"predictions": datasets.Value("int32"),
94
"references": datasets.Value("int32"),
97
reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html"],
100
def _compute(self, predictions, references, normalize=True, sample_weight=None):
103
accuracy_score(references, predictions, normalize=normalize, sample_weight=sample_weight)