CoolFace
Apppublic

Vlasta/pr_auc

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
pr_auc.py83 linesDownload Raw Back to root
1# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""TODO: Add a description here."""15 16import evaluate17import datasets18from sklearn.metrics import precision_recall_curve, auc19 20 21_CITATION = """\22@InProceedings{huggingface:module,23title = {A great new module},24authors={huggingface, Inc.},25year={2020}26}27"""28 29_DESCRIPTION = """\30Computes the area under precision-recall curve. Implementation details taken from https://sinyi-chou.github.io/python-sklearn-precision-recall/31"""32 33 34# TODO: Add description of the arguments of the module here35_KWARGS_DESCRIPTION = """36Calculates how good are predictions given some references, using certain scores37Args:38    prediction_scores: Model predictions39    references: list of reference for each prediction. Each40        reference should be a string with tokens separated by spaces.41Returns:42    pr_auc: area under the precision-recall curve,43Examples:44    No examples45"""46 47BAD_WORDS_URL = ""48 49 50@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)51class PRAUC(evaluate.Metric):52    def _info(self):53        # TODO: Specifies the evaluate.EvaluationModuleInfo object54        return evaluate.MetricInfo(55            # This is the description that will appear on the modules page.56            module_type="metric",57            description=_DESCRIPTION,58            citation=_CITATION,59            inputs_description=_KWARGS_DESCRIPTION,60            # This defines the format of each prediction and reference61            features=datasets.Features({62                'prediction_scores': datasets.Value("float"),63                'references': datasets.Value('int32'),64            }),65            # Homepage of the module for documentation66            homepage="http://module.homepage",67            # Additional links to the codebase or references68            codebase_urls=["http://github.com/path/to/codebase/of/new_module"],69            reference_urls=["http://path.to.reference.url/new_module"]70        )71 72    def _download_and_prepare(self, dl_manager):73        """Optional: download external resources useful to compute the scores"""74        # TODO: Download external resources if needed75        pass76 77    def _compute(self, prediction_scores, references):78        """Returns the scores"""79        precision, recall, thresholds = precision_recall_curve(references, prediction_scores)80        auc_precision_recall = auc(recall, precision)81        return {82            "pr_auc": auc_precision_recall,83        }