initial commit
Browse files
anls.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2022 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 at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# 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 and
|
13 |
+
# limitations under the License.
|
14 |
+
"""ANLS - Average Normalized Levenshtein Similarity"""
|
15 |
+
|
16 |
+
import datasets
|
17 |
+
import numpy as np
|
18 |
+
from sklearn.metrics import mean_absolute_error
|
19 |
+
|
20 |
+
import evaluate
|
21 |
+
|
22 |
+
|
23 |
+
_CITATION = """\
|
24 |
+
@article{,
|
25 |
+
title = {Binary codes capable of correcting deletions, insertions, and reversals},
|
26 |
+
journal = {Soviet physics doklady},
|
27 |
+
volume = {10},
|
28 |
+
number = {8},
|
29 |
+
pages = {707--710},
|
30 |
+
year = {1966},
|
31 |
+
url = {https://nymity.ch/sybilhunting/pdf/Levenshtein1966a.pdf},
|
32 |
+
author = {V. I. Levenshtein},
|
33 |
+
}
|
34 |
+
"""
|
35 |
+
|
36 |
+
_DESCRIPTION = """\
|
37 |
+
ANLS refer to the average normalized Levenshtein similarity
|
38 |
+
"""
|
39 |
+
|
40 |
+
|
41 |
+
_KWARGS_DESCRIPTION = """
|
42 |
+
Args:
|
43 |
+
predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)
|
44 |
+
Estimated target values.
|
45 |
+
references: array-like of shape (n_samples,) or (n_samples, n_outputs)
|
46 |
+
Ground truth (correct) target values.
|
47 |
+
training: array-like of shape (n_train_samples,) or (n_train_samples, n_outputs)
|
48 |
+
In sample training data for naive forecast.
|
49 |
+
periodicity: int, default=1
|
50 |
+
Seasonal periodicity of training data.
|
51 |
+
sample_weight: array-like of shape (n_samples,), default=None
|
52 |
+
Sample weights.
|
53 |
+
multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average"
|
54 |
+
Defines aggregating of multiple output values. Array-like value defines weights used to average errors.
|
55 |
+
|
56 |
+
"raw_values" : Returns a full set of errors in case of multioutput input.
|
57 |
+
|
58 |
+
"uniform_average" : Errors of all outputs are averaged with uniform weight.
|
59 |
+
|
60 |
+
Returns:
|
61 |
+
mase : mean absolute scaled error.
|
62 |
+
If multioutput is "raw_values", then mean absolute percentage error is returned for each output separately. If multioutput is "uniform_average" or an ndarray of weights, then the weighted average of all output errors is returned.
|
63 |
+
MASE output is non-negative floating point. The best value is 0.0.
|
64 |
+
Examples:
|
65 |
+
|
66 |
+
>>> mase_metric = evaluate.load("mase")
|
67 |
+
>>> predictions = [2.5, 0.0, 2, 8, 1.25]
|
68 |
+
>>> references = [3, -0.5, 2, 7, 2]
|
69 |
+
>>> training = [5, 0.5, 4, 6, 3, 5, 2]
|
70 |
+
>>> results = mase_metric.compute(predictions=predictions, references=references, training=training)
|
71 |
+
>>> print(results)
|
72 |
+
{'mase': 0.18333333333333335}
|
73 |
+
|
74 |
+
If you're using multi-dimensional lists, then set the config as follows :
|
75 |
+
|
76 |
+
>>> mase_metric = evaluate.load("mase", "multilist")
|
77 |
+
>>> predictions = [[0, 2], [-1, 2], [8, -5]]
|
78 |
+
>>> references = [[0.5, 1], [-1, 1], [7, -6]]
|
79 |
+
>>> training = [[0.5, 1], [-1, 1], [7, -6]]
|
80 |
+
>>> results = mase_metric.compute(predictions=predictions, references=references, training=training)
|
81 |
+
>>> print(results)
|
82 |
+
{'mase': 0.18181818181818182}
|
83 |
+
>>> results = mase_metric.compute(predictions=predictions, references=references, training=training, multioutput='raw_values')
|
84 |
+
>>> print(results)
|
85 |
+
{'mase': array([0.10526316, 0.28571429])}
|
86 |
+
>>> results = mase_metric.compute(predictions=predictions, references=references, training=training, multioutput=[0.3, 0.7])
|
87 |
+
>>> print(results)
|
88 |
+
{'mase': 0.21935483870967742}
|
89 |
+
"""
|
90 |
+
|
91 |
+
|
92 |
+
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
93 |
+
class Mase(evaluate.Metric):
|
94 |
+
def _info(self):
|
95 |
+
return evaluate.MetricInfo(
|
96 |
+
description=_DESCRIPTION,
|
97 |
+
citation=_CITATION,
|
98 |
+
inputs_description=_KWARGS_DESCRIPTION,
|
99 |
+
features=datasets.Features(self._get_feature_types()),
|
100 |
+
reference_urls=["https://otexts.com/fpp3/accuracy.html#scaled-errors"],
|
101 |
+
)
|
102 |
+
|
103 |
+
def _get_feature_types(self):
|
104 |
+
if self.config_name == "multilist":
|
105 |
+
return {
|
106 |
+
"predictions": datasets.Sequence(datasets.Value("float")),
|
107 |
+
"references": datasets.Sequence(datasets.Value("float")),
|
108 |
+
}
|
109 |
+
else:
|
110 |
+
return {
|
111 |
+
"predictions": datasets.Value("float"),
|
112 |
+
"references": datasets.Value("float"),
|
113 |
+
}
|
114 |
+
|
115 |
+
def _compute(
|
116 |
+
self,
|
117 |
+
predictions,
|
118 |
+
references,
|
119 |
+
training,
|
120 |
+
periodicity=1,
|
121 |
+
sample_weight=None,
|
122 |
+
multioutput="uniform_average",
|
123 |
+
):
|
124 |
+
|
125 |
+
y_pred_naive = training[:-periodicity]
|
126 |
+
mae_naive = mean_absolute_error(training[periodicity:], y_pred_naive, multioutput=multioutput)
|
127 |
+
|
128 |
+
mae_score = mean_absolute_error(
|
129 |
+
references,
|
130 |
+
predictions,
|
131 |
+
sample_weight=sample_weight,
|
132 |
+
multioutput=multioutput,
|
133 |
+
)
|
134 |
+
|
135 |
+
epsilon = np.finfo(np.float64).eps
|
136 |
+
mase_score = mae_score / np.maximum(mae_naive, epsilon)
|
137 |
+
|
138 |
+
return {"mase": mase_score}
|
app.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import evaluate
|
2 |
+
from evaluate.utils import launch_gradio_widget
|
3 |
+
|
4 |
+
|
5 |
+
module = evaluate.load("anls")
|
6 |
+
launch_gradio_widget(module)
|