Add hierarchical calculation with test ancestors to MetricTemplate1
Browse files- metric_template_1.py +29 -7
metric_template_1.py
CHANGED
@@ -15,6 +15,7 @@
|
|
15 |
|
16 |
import evaluate
|
17 |
import datasets
|
|
|
18 |
|
19 |
|
20 |
# TODO: Add BibTeX citation
|
@@ -70,15 +71,17 @@ class MetricTemplate1(evaluate.Metric):
|
|
70 |
citation=_CITATION,
|
71 |
inputs_description=_KWARGS_DESCRIPTION,
|
72 |
# This defines the format of each prediction and reference
|
73 |
-
features=datasets.Features(
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
# Homepage of the module for documentation
|
78 |
homepage="http://module.homepage",
|
79 |
# Additional links to the codebase or references
|
80 |
codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
|
81 |
-
reference_urls=["http://path.to.reference.url/new_module"]
|
82 |
)
|
83 |
|
84 |
def _download_and_prepare(self, dl_manager):
|
@@ -89,7 +92,26 @@ class MetricTemplate1(evaluate.Metric):
|
|
89 |
def _compute(self, predictions, references):
|
90 |
"""Returns the scores"""
|
91 |
# TODO: Compute the different scores of the module
|
92 |
-
accuracy = sum(i == j for i, j in zip(predictions, references)) / len(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
return {
|
94 |
"accuracy": accuracy,
|
95 |
-
|
|
|
|
|
|
|
|
15 |
|
16 |
import evaluate
|
17 |
import datasets
|
18 |
+
import ham
|
19 |
|
20 |
|
21 |
# TODO: Add BibTeX citation
|
|
|
71 |
citation=_CITATION,
|
72 |
inputs_description=_KWARGS_DESCRIPTION,
|
73 |
# This defines the format of each prediction and reference
|
74 |
+
features=datasets.Features(
|
75 |
+
{
|
76 |
+
"predictions": datasets.Value("int64"),
|
77 |
+
"references": datasets.Value("int64"),
|
78 |
+
}
|
79 |
+
),
|
80 |
# Homepage of the module for documentation
|
81 |
homepage="http://module.homepage",
|
82 |
# Additional links to the codebase or references
|
83 |
codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
|
84 |
+
reference_urls=["http://path.to.reference.url/new_module"],
|
85 |
)
|
86 |
|
87 |
def _download_and_prepare(self, dl_manager):
|
|
|
92 |
def _compute(self, predictions, references):
|
93 |
"""Returns the scores"""
|
94 |
# TODO: Compute the different scores of the module
|
95 |
+
accuracy = sum(i == j for i, j in zip(predictions, references)) / len(
|
96 |
+
predictions
|
97 |
+
)
|
98 |
+
|
99 |
+
ancestors = { # The ancestors for each class, excluding the root
|
100 |
+
"G": {"B", "C", "E"},
|
101 |
+
"F": {"C"},
|
102 |
+
}
|
103 |
+
|
104 |
+
# Calculate hierarchical measures
|
105 |
+
hP, hR, hF = ham.hierarchical_precision_recall_fmeasure(
|
106 |
+
references, predictions, ancestors
|
107 |
+
)
|
108 |
+
print(f"Hierarchical Precision (hP): {hP}")
|
109 |
+
print(f"Hierarchical Recall (hR): {hR}")
|
110 |
+
print(f"Hierarchical F-measure (hF): {hF}")
|
111 |
+
|
112 |
return {
|
113 |
"accuracy": accuracy,
|
114 |
+
"hierarchical_precision": hP,
|
115 |
+
"hierarchical_recall": hR,
|
116 |
+
"hierarchical_fmeasure": hF,
|
117 |
+
}
|