Spaces:
Running
Running
# 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. | |
"""TODO: MCC is a correlation coefficient between the observed and predicted binary classifications, and takes into account true and false positives and negatives.""" | |
import evaluate | |
import datasets | |
from sklearn.metrics import matthews_corrcoef | |
# TODO: Add BibTeX citation | |
_CITATION = """\ | |
@InProceedings{huggingface:module, | |
title = {MCC Metric}, | |
authors={huggingface, Inc.}, | |
year={2020} | |
} | |
""" | |
# TODO: Add description of the module here | |
_DESCRIPTION = """\ | |
MCC (Matthews Correlation Coefficient) is a correlation coefficient between the observed and predicted binary classifications, and takes into account true and false positives and negatives. It can be computed with the equation: | |
MCC = (TP * TN - FP * FN) / sqrt((TP+FP) * (TP+FN) * (TN+FP) * (TN+FN)) | |
Where TP is the true positives, TN is the true negatives, FP is the false positives, and FN is the false negatives. | |
""" | |
# TODO: Add description of the arguments of the module here | |
_KWARGS_DESCRIPTION = """ | |
Calculates how good are predictions given some references, using certain scores | |
Args: | |
- **predictions** (`list` of `int`): The predicted labels. | |
- **references** (`list` of `int`): The ground truth labels. | |
Returns: | |
- **mcc** (`float`): The MCC score. Minimum possible value is -1. Maximum possible value is 1. A higher MCC means that the predicted and observed binary classifications agree better, while a negative MCC means that they agree worse than chance. | |
Examples: | |
Example 1-A simple example with some errors | |
>>> mcc_metric = evaluate.load('mcc') | |
>>> results = mcc_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) | |
>>> print(results) | |
{'mcc': 0.16666666666666666} | |
Example 2-The same example as Example 1, but with some different labels | |
>>> mcc_metric = evaluate.load('mcc') | |
>>> results = mcc_metric.compute(references=[0, 1, 2, 2, 2], predictions=[0, 2, 2, 1, 2]) | |
>>> print(results) | |
{'mcc': 0.2041241452319315} | |
""" | |
# TODO: Define external resources urls if needed | |
BAD_WORDS_URL = "https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html" | |
class MCC(evaluate.Metric): | |
"""Compute MCC Scores""" | |
def _info(self): | |
return evaluate.MetricInfo( | |
module_type="metric", | |
description=_DESCRIPTION, | |
citation=_CITATION, | |
inputs_description=_KWARGS_DESCRIPTION, | |
features=datasets.Features({ | |
'predictions': datasets.Value('int64'), | |
'references': datasets.Value('int64'), | |
}), | |
# Homepage of the module for documentation | |
homepage="https://huggingface.co/evaluate-metric?message=Request%20sent", | |
# Additional links to the codebase or references | |
codebase_urls=[], | |
reference_urls=[] | |
) | |
def _compute(self, predictions, references): | |
"""Returns the mcc scores""" | |
# Computes the MCC score using matthews_corrcoef from sklearn | |
return {"mcc": matthews_corrcoef(references, predictions)} |