Spaces:
Runtime error
Runtime error
Commit
·
cd9eb64
1
Parent(s):
c9c9f4b
modified: average_precision.py
Browse files- average_precision.py +39 -13
- requirements.txt +2 -1
average_precision.py
CHANGED
|
@@ -11,30 +11,50 @@
|
|
| 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 |
-
"""
|
| 15 |
|
| 16 |
import evaluate
|
| 17 |
import datasets
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
# TODO: Add BibTeX citation
|
| 21 |
_CITATION = """\
|
| 22 |
@InProceedings{huggingface:module,
|
| 23 |
title = {A great new module},
|
| 24 |
-
authors={
|
| 25 |
-
year={
|
| 26 |
}
|
| 27 |
"""
|
| 28 |
|
| 29 |
# TODO: Add description of the module here
|
| 30 |
_DESCRIPTION = """\
|
| 31 |
-
|
| 32 |
"""
|
| 33 |
|
| 34 |
|
| 35 |
# TODO: Add description of the arguments of the module here
|
| 36 |
_KWARGS_DESCRIPTION = """
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
Args:
|
| 39 |
predictions: list of predictions to score. Each predictions
|
| 40 |
should be a string with tokens separated by spaces.
|
|
@@ -57,7 +77,9 @@ Examples:
|
|
| 57 |
BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
|
| 58 |
|
| 59 |
|
| 60 |
-
@evaluate.utils.file_utils.add_start_docstrings(
|
|
|
|
|
|
|
| 61 |
class AveragePrecision(evaluate.Metric):
|
| 62 |
"""TODO: Short description of my evaluation module."""
|
| 63 |
|
|
@@ -70,15 +92,17 @@ class AveragePrecision(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 +113,9 @@ class AveragePrecision(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 |
-
}
|
|
|
|
| 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 |
+
"""Average Precision"""
|
| 15 |
|
| 16 |
import evaluate
|
| 17 |
import datasets
|
| 18 |
+
from sklearn.metrics import average_precision_score
|
| 19 |
|
| 20 |
|
| 21 |
# TODO: Add BibTeX citation
|
| 22 |
_CITATION = """\
|
| 23 |
@InProceedings{huggingface:module,
|
| 24 |
title = {A great new module},
|
| 25 |
+
authors={chanelcolgate, Inc.},
|
| 26 |
+
year={2023}
|
| 27 |
}
|
| 28 |
"""
|
| 29 |
|
| 30 |
# TODO: Add description of the module here
|
| 31 |
_DESCRIPTION = """\
|
| 32 |
+
Average Precision
|
| 33 |
"""
|
| 34 |
|
| 35 |
|
| 36 |
# TODO: Add description of the arguments of the module here
|
| 37 |
_KWARGS_DESCRIPTION = """
|
| 38 |
+
Note: To be consistent with the `evaluate` input conventions the scikit-learn inputs are renamed:
|
| 39 |
+
- `y_true`: `references`
|
| 40 |
+
- `y_pred`: `prediction_scores`
|
| 41 |
+
|
| 42 |
+
Scikit-learn docstring:
|
| 43 |
+
Average precision score.
|
| 44 |
+
|
| 45 |
+
Compute average precision (AP) from prediction scores.
|
| 46 |
+
AP summarizes a precision-recall curve as the weighted mean of precisions
|
| 47 |
+
achieved at each threshold, with the increase in recall from the previous
|
| 48 |
+
threshold used as the weight:
|
| 49 |
+
.. math::
|
| 50 |
+
\\text{AP} = \\sum_n (R_n - R_{n-1}) P_n
|
| 51 |
+
where :math:`P_n` and :math:`R_n` are the precision and recall at the nth
|
| 52 |
+
threshold [1]_. This implementation is not interpolated and is different
|
| 53 |
+
from computing the area under the precision-recall curve with the
|
| 54 |
+
trapezoidal rule, which uses linear interpolation and can be too optimistic.
|
| 55 |
+
Note: this implementation is restricted to the binary classification task or
|
| 56 |
+
multilabel classification task.
|
| 57 |
+
Read more in the :ref:`User Guide <precision_recall_f_measure_metrics`.
|
| 58 |
Args:
|
| 59 |
predictions: list of predictions to score. Each predictions
|
| 60 |
should be a string with tokens separated by spaces.
|
|
|
|
| 77 |
BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
|
| 78 |
|
| 79 |
|
| 80 |
+
@evaluate.utils.file_utils.add_start_docstrings(
|
| 81 |
+
_DESCRIPTION, _KWARGS_DESCRIPTION
|
| 82 |
+
)
|
| 83 |
class AveragePrecision(evaluate.Metric):
|
| 84 |
"""TODO: Short description of my evaluation module."""
|
| 85 |
|
|
|
|
| 92 |
citation=_CITATION,
|
| 93 |
inputs_description=_KWARGS_DESCRIPTION,
|
| 94 |
# This defines the format of each prediction and reference
|
| 95 |
+
features=datasets.Features(
|
| 96 |
+
{
|
| 97 |
+
"predictions": datasets.Value("int64"),
|
| 98 |
+
"references": datasets.Value("int64"),
|
| 99 |
+
}
|
| 100 |
+
),
|
| 101 |
# Homepage of the module for documentation
|
| 102 |
homepage="http://module.homepage",
|
| 103 |
# Additional links to the codebase or references
|
| 104 |
codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
|
| 105 |
+
reference_urls=["http://path.to.reference.url/new_module"],
|
| 106 |
)
|
| 107 |
|
| 108 |
def _download_and_prepare(self, dl_manager):
|
|
|
|
| 113 |
def _compute(self, predictions, references):
|
| 114 |
"""Returns the scores"""
|
| 115 |
# TODO: Compute the different scores of the module
|
| 116 |
+
accuracy = sum(i == j for i, j in zip(predictions, references)) / len(
|
| 117 |
+
predictions
|
| 118 |
+
)
|
| 119 |
return {
|
| 120 |
"accuracy": accuracy,
|
| 121 |
+
}
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
git+https://github.com/huggingface/evaluate@main
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/evaluate@main
|
| 2 |
+
scikit-learn
|