Spaces:
Sleeping
Sleeping
John Graham Reynolds
commited on
Commit
·
4ab442f
1
Parent(s):
7b47670
add file basics
Browse files- README.md +2 -2
- fixed_f1.py +51 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
title: FixedF1
|
| 3 |
emoji: 📈
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.5.0
|
| 8 |
app_file: app.py
|
|
|
|
| 1 |
---
|
| 2 |
title: FixedF1
|
| 3 |
emoji: 📈
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.5.0
|
| 8 |
app_file: app.py
|
fixed_f1.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datasets
|
| 2 |
+
import evaluate
|
| 3 |
+
from evaluate import evaluator, Metric
|
| 4 |
+
# from evaluate.metrics.f1 import F1
|
| 5 |
+
from sklearn.metrics import f1_score
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
_CITATION = """
|
| 9 |
+
@online{MarioBarbeque@HuggingFace,
|
| 10 |
+
author = {John Graham Reynolds aka @MarioBarbeque},
|
| 11 |
+
title = {{Fixed F1 Hugging Face Metric},
|
| 12 |
+
year = 2024,
|
| 13 |
+
url = {https://huggingface.co/spaces/MarioBarbeque/FixedF1},
|
| 14 |
+
urldate = {0000-00-00}
|
| 15 |
+
}
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
# could in principle subclass F1, but ideally we can work the fix into the HF main F1 class to maintain SOLID code
|
| 19 |
+
class FixedF1(evaluate.Metric):
|
| 20 |
+
|
| 21 |
+
def __init__(self, average="binary"):
|
| 22 |
+
super().__init__()
|
| 23 |
+
self.average = average
|
| 24 |
+
# additional values passed to compute() could and probably should (?) all be passed here so that the final computation is configured immediately at Metric instantiation
|
| 25 |
+
|
| 26 |
+
def _info(self):
|
| 27 |
+
return evaluate.MetricInfo(
|
| 28 |
+
description="Custom built F1 metric for true *multilabel* classification - the 'multilabel' config_name var in the evaluate.EvaluationModules class appears to better address multi-class classification, where features can fall under a plethora of labels. This class is implemented with the intention of enabling the evaluation of multiple multilabel classification metrics at the same time using the evaluate.CombinedEvaluations.combine method.",
|
| 29 |
+
citation="",
|
| 30 |
+
inputs_description="'average': This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Options include: {‘micro’, ‘macro’, ‘samples’, ‘weighted’, ‘binary’} or None.",
|
| 31 |
+
features=datasets.Features(
|
| 32 |
+
{
|
| 33 |
+
"predictions": datasets.Sequence(datasets.Value("int32")),
|
| 34 |
+
"references": datasets.Sequence(datasets.Value("int32")),
|
| 35 |
+
}
|
| 36 |
+
if self.config_name == "multilabel"
|
| 37 |
+
else {
|
| 38 |
+
"predictions": datasets.Value("int32"),
|
| 39 |
+
"references": datasets.Value("int32"),
|
| 40 |
+
}
|
| 41 |
+
),
|
| 42 |
+
reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"],
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# could remove specific kwargs like average, sample_weight from _compute() method of F1
|
| 46 |
+
|
| 47 |
+
def _compute(self, predictions, references, labels=None, pos_label=1, average="binary", sample_weight=None):
|
| 48 |
+
score = f1_score(
|
| 49 |
+
references, predictions, labels=labels, pos_label=pos_label, average=self.average, sample_weight=sample_weight
|
| 50 |
+
)
|
| 51 |
+
return {"f1": float(score) if score.size == 1 else score}
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn
|
| 2 |
+
evaluate
|
| 3 |
+
datasets
|