# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """HTER metric.""" import datasets import evaluate _DESCRIPTION = """ HTER (Half Total Error Rate) is a metric that combines the False Accept Rate (FAR) and False Reject Rate (FRR) to provide a comprehensive evaluation of a system's performance. It can be computed with: HTER = (FAR + FRR) / 2 Where: FAR (False Accept Rate) = FP / (FP + TN) FRR (False Reject Rate) = FN / (FN + TP) TP: True positive TN: True negative FP: False positive FN: False negative """ _KWARGS_DESCRIPTION = """ Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. Returns: HTER (`float` or `int`): HTER score. Minimum possible value is 0. Maximum possible value is 1.0, or the number of examples input. Examples: Example 1-A simple example >>> hter_metric = evaluate.load("murinj/hter") >>> results = hter_metric.compute(references=[0, 0], predictions=[0, 1]) >>> print(results) {'HTER': 0.5} """ _CITATION = """ } """ @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION) class Accuracy(evaluate.Metric): def _info(self): return evaluate.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { "predictions": datasets.Value("int32"), "references": datasets.Value("int32"), } ), ) def _compute(self, predictions, references): TP = 0 # True Positive TN = 0 # True Negative FP = 0 # False Positive FN = 0 # False Negative for pred, ref in zip(predictions, references): if pred == 1 and ref == 1: TP += 1 elif pred == 0 and ref == 0: TN += 1 elif pred == 1 and ref == 0: FP += 1 elif pred == 0 and ref == 1: FN += 1 # 计算 FAR 和 FRR FAR = FP / (FP + TN) if (FP + TN) > 0 else 0 FRR = FN / (FN + TP) if (FN + TP) > 0 else 0 # 计算 HTER HTER = (FAR + FRR) / 2.0 return { "HTER": HTER }