Bekhouche commited on
Commit
8202170
·
1 Parent(s): d181a48

update ACC.py

Browse files
Files changed (1) hide show
  1. ACC.py +96 -0
ACC.py CHANGED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ import datasets
4
+ import evaluate
5
+
6
+
7
+ _DESCRIPTION = """
8
+ The Accuracy (ACC) metric is used to measure the proportion of correctly predicted sequences compared to the total number of sequences.
9
+ This metric can handle both integer and string inputs by converting them to strings for comparison.
10
+ The ACC ranges from 0 to 1, where 1 indicates perfect accuracy (all predictions are correct) and 0 indicates complete failure (no predictions are correct).
11
+ It is particularly useful in tasks such as OCR, digit recognition, sequence prediction, and any task where exact matches are required.
12
+ The accuracy can be calculated using the formula:
13
+
14
+ ACC = (Number of Correct Predictions) / (Total Number of Predictions)
15
+
16
+ Where a prediction is considered correct if it exactly matches the ground truth sequence after converting both to strings.
17
+ """
18
+
19
+ _KWARGS_DESCRIPTION = """
20
+ Args:
21
+ predictions (`list` of `str` or `int`): Predicted labels (can be strings or integers).
22
+ references (`list` of `str` or `int`): Ground truth labels (can be strings or integers).
23
+ Returns:
24
+ acc (`float`): Accuracy score. Minimum possible value is 0. Maximum possible value is 1.0.
25
+ Examples:
26
+ Example 1 - String inputs:
27
+ >>> acc_metric = evaluate.load("Bekhouche/ACC")
28
+ >>> results = acc_metric.compute(references=['123', '456', '789'], predictions=['123', '456', '789'])
29
+ >>> print(results)
30
+ {'acc': 1.0}
31
+
32
+ Example 2 - Integer inputs:
33
+ >>> results = acc_metric.compute(references=[123, 456, 789], predictions=[123, 456, 789])
34
+ >>> print(results)
35
+ {'acc': 1.0}
36
+
37
+ Example 3 - Mixed inputs:
38
+ >>> results = acc_metric.compute(references=['123', 456, '789'], predictions=[123, '456', 789])
39
+ >>> print(results)
40
+ {'acc': 1.0}
41
+ """
42
+
43
+ _CITATION = """
44
+ @inproceedings{accuracy_metric,
45
+ title={Accuracy as a fundamental metric for sequence prediction tasks},
46
+ author={Various Authors},
47
+ booktitle={Proceedings of various conferences},
48
+ pages={1--10},
49
+ year={2023},
50
+ organization={Various}
51
+ }
52
+ """
53
+
54
+
55
+ @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
56
+ class ACC(evaluate.Metric):
57
+ def _info(self):
58
+ return evaluate.MetricInfo(
59
+ description=_DESCRIPTION,
60
+ citation=_CITATION,
61
+ inputs_description=_KWARGS_DESCRIPTION,
62
+ features=datasets.Features(
63
+ {
64
+ "predictions": datasets.Sequence(datasets.Value("string")),
65
+ "references": datasets.Sequence(datasets.Value("string")),
66
+ }
67
+ if self.config_name == "multilabel"
68
+ else {
69
+ "predictions": datasets.Value("string"),
70
+ "references": datasets.Value("string"),
71
+ }
72
+ ),
73
+ reference_urls=["https://huggingface.co/spaces/Bekhouche/ACC"],
74
+ )
75
+
76
+ def _compute(self, predictions, references):
77
+ acc = 0.0
78
+ if isinstance(predictions, list) and isinstance(references, list):
79
+ correct_predictions = sum([self._compute_acc(prediction, reference) for prediction, reference in zip(predictions, references)])
80
+ acc = correct_predictions / len(predictions)
81
+ elif (isinstance(predictions, (str, int)) and isinstance(references, (str, int))):
82
+ acc = self._compute_acc(predictions, references)
83
+ else:
84
+ raise ValueError("Predictions and references must be either a list[str/int] or str/int")
85
+ return {
86
+ "acc": float(
87
+ acc
88
+ )
89
+ }
90
+
91
+ @staticmethod
92
+ def _compute_acc(prediction, reference):
93
+ # Convert both prediction and reference to strings for comparison
94
+ pred_str = str(prediction)
95
+ ref_str = str(reference)
96
+ return 1.0 if pred_str == ref_str else 0.0