yonting commited on
Commit
48af2fc
β€’
1 Parent(s): c3d8ec8

Initial commit

Browse files
Files changed (5) hide show
  1. README.md +116 -3
  2. app.py +6 -0
  3. average_precision_score.py +169 -0
  4. requirements.txt +2 -0
  5. tests.py +22 -0
README.md CHANGED
@@ -1,12 +1,125 @@
1
  ---
2
  title: Average Precision Score
3
- emoji: πŸ‘€
4
  colorFrom: blue
5
- colorTo: blue
 
 
 
 
 
6
  sdk: gradio
7
  sdk_version: 3.18.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Average Precision Score
3
+ emoji: 🌍
4
  colorFrom: blue
5
+ colorTo: orange
6
+ tags:
7
+ - evaluate
8
+ - metric
9
+ - sklearn
10
+ description: "Average precision score."
11
  sdk: gradio
12
  sdk_version: 3.18.0
13
  app_file: app.py
14
  pinned: false
15
  ---
16
 
17
+ # Metric Card for `sklearn.metrics.average_precision_score`
18
+
19
+ ## Input Convention
20
+ To be consistent with the `evaluate` input conventions the scikit-learn inputs are renamed:
21
+ - `y_true`: `references`
22
+ - `y_pred`: `prediction_scores`
23
+
24
+ ## Usage
25
+
26
+ ```python
27
+ import evaluate
28
+
29
+ metric = evaluate.load("yonting/average_precision_score")
30
+ results = metric.compute(references=references, prediction_scores=prediction_scores)
31
+ ```
32
+
33
+ ## Description
34
+
35
+ Average precision score.
36
+
37
+ Compute average precision (AP) from prediction scores.
38
+ AP summarizes a precision-recall curve as the weighted mean of precisions
39
+ achieved at each threshold, with the increase in recall from the previous
40
+ threshold used as the weight:
41
+ .. math::
42
+ \\text{AP} = \\sum_n (R_n - R_{n-1}) P_n
43
+ where :math:`P_n` and :math:`R_n` are the precision and recall at the nth
44
+ threshold [1]_. This implementation is not interpolated and is different
45
+ from computing the area under the precision-recall curve with the
46
+ trapezoidal rule, which uses linear interpolation and can be too
47
+ optimistic.
48
+ Note: this implementation is restricted to the binary classification task
49
+ or multilabel classification task.
50
+ Read more in the :ref:`User Guide <precision_recall_f_measure_metrics>`.
51
+ Parameters
52
+ ----------
53
+ y_true : ndarray of shape (n_samples,) or (n_samples, n_classes)
54
+ True binary labels or binary label indicators.
55
+ y_score : ndarray of shape (n_samples,) or (n_samples, n_classes)
56
+ Target scores, can either be probability estimates of the positive
57
+ class, confidence values, or non-thresholded measure of decisions
58
+ (as returned by :term:`decision_function` on some classifiers).
59
+ average : {'micro', 'samples', 'weighted', 'macro'} or None, \
60
+ default='macro'
61
+ If ``None``, the scores for each class are returned. Otherwise,
62
+ this determines the type of averaging performed on the data:
63
+ ``'micro'``:
64
+ Calculate metrics globally by considering each element of the label
65
+ indicator matrix as a label.
66
+ ``'macro'``:
67
+ Calculate metrics for each label, and find their unweighted
68
+ mean. This does not take label imbalance into account.
69
+ ``'weighted'``:
70
+ Calculate metrics for each label, and find their average, weighted
71
+ by support (the number of true instances for each label).
72
+ ``'samples'``:
73
+ Calculate metrics for each instance, and find their average.
74
+ Will be ignored when ``y_true`` is binary.
75
+ pos_label : int or str, default=1
76
+ The label of the positive class. Only applied to binary ``y_true``.
77
+ For multilabel-indicator ``y_true``, ``pos_label`` is fixed to 1.
78
+ sample_weight : array-like of shape (n_samples,), default=None
79
+ Sample weights.
80
+ Returns
81
+ -------
82
+ average_precision : float
83
+ Average precision score.
84
+ See Also
85
+ --------
86
+ roc_auc_score : Compute the area under the ROC curve.
87
+ precision_recall_curve : Compute precision-recall pairs for different
88
+ probability thresholds.
89
+ Notes
90
+ -----
91
+ .. versionchanged:: 0.19
92
+ Instead of linearly interpolating between operating points, precisions
93
+ are weighted by the change in recall since the last operating point.
94
+ References
95
+ ----------
96
+ .. [1] `Wikipedia entry for the Average precision
97
+ <https://en.wikipedia.org/w/index.php?title=Information_retrieval&
98
+ oldid=793358396#Average_precision>`_
99
+ Examples
100
+ --------
101
+ >>> import numpy as np
102
+ >>> from sklearn.metrics import average_precision_score
103
+ >>> y_true = np.array([0, 0, 1, 1])
104
+ >>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
105
+ >>> average_precision_score(y_true, y_scores)
106
+ 0.83...
107
+
108
+
109
+
110
+ ## Citation
111
+ ```bibtex
112
+ @article{scikit-learn,
113
+ title={Scikit-learn: Machine Learning in {P}ython},
114
+ author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
115
+ and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
116
+ and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
117
+ Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
118
+ journal={Journal of Machine Learning Research},
119
+ volume={12},
120
+ pages={2825--2830},
121
+ year={2011}
122
+ }
123
+ ```
124
+ ## Further References
125
+ - Docs: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.average_precision_score.html
app.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ import evaluate
2
+ from evaluate.utils import launch_gradio_widget
3
+
4
+
5
+ module = evaluate.load("yonting/average_precision_score")
6
+ launch_gradio_widget(module)
average_precision_score.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 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
+ """Average precision score."""
15
+
16
+ import datasets
17
+ from sklearn.metrics import average_precision_score
18
+
19
+ import evaluate
20
+
21
+
22
+ _CITATION = """\
23
+ @article{scikit-learn,
24
+ title={Scikit-learn: Machine Learning in {P}ython},
25
+ author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
26
+ journal={Journal of Machine Learning Research},
27
+ volume={12},
28
+ pages={2825--2830},
29
+ year={2011}
30
+ }
31
+ """
32
+
33
+
34
+ _DESCRIPTION = """\
35
+ Average precision score.
36
+ """
37
+
38
+ _KWARGS_DESCRIPTION = """
39
+ Note: To be consistent with the `evaluate` input conventions the scikit-learn inputs are renamed:
40
+ - `y_true`: `references`
41
+ - `y_pred`: `prediction_scores`
42
+
43
+ Scikit-learn docstring:
44
+ Average precision score.
45
+
46
+ Compute average precision (AP) from prediction scores.
47
+ AP summarizes a precision-recall curve as the weighted mean of precisions
48
+ achieved at each threshold, with the increase in recall from the previous
49
+ threshold used as the weight:
50
+ .. math::
51
+ \\text{AP} = \\sum_n (R_n - R_{n-1}) P_n
52
+ where :math:`P_n` and :math:`R_n` are the precision and recall at the nth
53
+ threshold [1]_. This implementation is not interpolated and is different
54
+ from computing the area under the precision-recall curve with the
55
+ trapezoidal rule, which uses linear interpolation and can be too
56
+ optimistic.
57
+ Note: this implementation is restricted to the binary classification task
58
+ or multilabel classification task.
59
+ Read more in the :ref:`User Guide <precision_recall_f_measure_metrics>`.
60
+ Parameters
61
+ ----------
62
+ y_true : ndarray of shape (n_samples,) or (n_samples, n_classes)
63
+ True binary labels or binary label indicators.
64
+ y_score : ndarray of shape (n_samples,) or (n_samples, n_classes)
65
+ Target scores, can either be probability estimates of the positive
66
+ class, confidence values, or non-thresholded measure of decisions
67
+ (as returned by :term:`decision_function` on some classifiers).
68
+ average : {'micro', 'samples', 'weighted', 'macro'} or None, \
69
+ default='macro'
70
+ If ``None``, the scores for each class are returned. Otherwise,
71
+ this determines the type of averaging performed on the data:
72
+ ``'micro'``:
73
+ Calculate metrics globally by considering each element of the label
74
+ indicator matrix as a label.
75
+ ``'macro'``:
76
+ Calculate metrics for each label, and find their unweighted
77
+ mean. This does not take label imbalance into account.
78
+ ``'weighted'``:
79
+ Calculate metrics for each label, and find their average, weighted
80
+ by support (the number of true instances for each label).
81
+ ``'samples'``:
82
+ Calculate metrics for each instance, and find their average.
83
+ Will be ignored when ``y_true`` is binary.
84
+ pos_label : int or str, default=1
85
+ The label of the positive class. Only applied to binary ``y_true``.
86
+ For multilabel-indicator ``y_true``, ``pos_label`` is fixed to 1.
87
+ sample_weight : array-like of shape (n_samples,), default=None
88
+ Sample weights.
89
+ Returns
90
+ -------
91
+ average_precision : float
92
+ Average precision score.
93
+ See Also
94
+ --------
95
+ roc_auc_score : Compute the area under the ROC curve.
96
+ precision_recall_curve : Compute precision-recall pairs for different
97
+ probability thresholds.
98
+ Notes
99
+ -----
100
+ .. versionchanged:: 0.19
101
+ Instead of linearly interpolating between operating points, precisions
102
+ are weighted by the change in recall since the last operating point.
103
+ References
104
+ ----------
105
+ .. [1] `Wikipedia entry for the Average precision
106
+ <https://en.wikipedia.org/w/index.php?title=Information_retrieval&
107
+ oldid=793358396#Average_precision>`_
108
+ Examples
109
+ --------
110
+ >>> import numpy as np
111
+ >>> from sklearn.metrics import average_precision_score
112
+ >>> y_true = np.array([0, 0, 1, 1])
113
+ >>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
114
+ >>> average_precision_score(y_true, y_scores)
115
+ 0.83...
116
+ """
117
+
118
+
119
+ @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
120
+ class AveragePrecision(evaluate.Metric):
121
+ """Average precision score."""
122
+
123
+ def _info(self):
124
+ return evaluate.MetricInfo(
125
+ # This is the description that will appear on the modules page.
126
+ module_type="metric",
127
+ description=_DESCRIPTION,
128
+ citation=_CITATION,
129
+ inputs_description=_KWARGS_DESCRIPTION,
130
+ # This defines the format of each prediction and reference
131
+ features=[
132
+ datasets.Features(
133
+ {
134
+ "references": datasets.Value("int64"),
135
+ "prediction_scores": datasets.Value("float"),
136
+ }
137
+ ),
138
+ datasets.Features(
139
+ {
140
+ "references": datasets.Sequence(datasets.Value("int64")),
141
+ "prediction_scores": datasets.Sequence(datasets.Value("float")),
142
+ }
143
+ ),
144
+ ],
145
+ # Homepage of the module for documentation
146
+ homepage="https://scikit-learn.org/stable/modules/generated/sklearn.metrics.average_precision_score.html",
147
+ # Additional links to the codebase or references
148
+ codebase_urls=["https://github.com/scikit-learn/scikit-learn"],
149
+ reference_urls=["https://scikit-learn.org/stable/index.html"],
150
+ )
151
+
152
+ def _compute(
153
+ self,
154
+ references,
155
+ prediction_scores,
156
+ average="macro",
157
+ pos_label=1,
158
+ sample_weight=None,
159
+ ):
160
+ """Returns the scores."""
161
+ return {
162
+ "average_precision": average_precision_score(
163
+ y_true=references,
164
+ y_score=prediction_scores,
165
+ average=average,
166
+ pos_label=pos_label,
167
+ sample_weight=sample_weight,
168
+ )
169
+ }
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ evaluate
2
+ scikit-learn
tests.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ test_cases = [
2
+ {
3
+ "references": [1, 1],
4
+ "prediction_scores": [1, 0],
5
+ "result": {"average_precision": 0.0}
6
+ },
7
+ {
8
+ "references": [1, 1],
9
+ "prediction_scores": [1, 0],
10
+ "result": {"average_precision": 1.0}
11
+ },
12
+ {
13
+ "references": [1, 0],
14
+ "prediction_scores": [1, 1],
15
+ "result": {"average_precision": 0.5}
16
+ },
17
+ {
18
+ "references": [1, 1],
19
+ "prediction_scores": [0, 0],
20
+ "result": {"average_precision": 1.0}
21
+ }
22
+ ]