Michael commited on
Commit
0019bfe
·
1 Parent(s): 19f6590
Files changed (2) hide show
  1. app.py +6 -0
  2. pr_auc.py +126 -0
app.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ import evaluate
2
+ from evaluate.utils import launch_gradio_widget
3
+
4
+
5
+ module = evaluate.load("michaelm16/pr_auc")
6
+ launch_gradio_widget(module)
pr_auc.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """Precision-recall AUC metric."""
15
+
16
+ import datasets
17
+ from sklearn.metrics import average_precision_score
18
+
19
+ import evaluate
20
+
21
+
22
+ _DESCRIPTION = """
23
+ Compute average precision (AP) from prediction scores.
24
+
25
+ AP summarizes a precision-recall curve as the weighted mean of precisions
26
+ achieved at each threshold, with the increase in recall from the previous
27
+ threshold used as the weight:
28
+
29
+ .. math::
30
+ \\text{AP} = \\sum_n (R_n - R_{n-1}) P_n
31
+
32
+ where :math:`P_n` and :math:`R_n` are the precision and recall at the nth
33
+ threshold [1]_. This implementation is not interpolated and is different
34
+ from computing the area under the precision-recall curve with the
35
+ trapezoidal rule, which uses linear interpolation and can be too
36
+ optimistic.
37
+ """
38
+
39
+ _KWARGS_DESCRIPTION = """
40
+ Parameters
41
+ ----------
42
+ references : array-like of shape (n_samples,)
43
+ True binary labels or binary label indicators.
44
+
45
+ prediction_scores : array-like of shape (n_samples,)
46
+ Target scores, can either be probability estimates of the positive
47
+ class, confidence values, or non-thresholded measure of decisions
48
+ (as returned by :term:`decision_function` on some classifiers).
49
+
50
+ average : {'micro', 'samples', 'weighted', 'macro'} or None, \
51
+ default='macro'
52
+ If ``None``, the scores for each class are returned. Otherwise,
53
+ this determines the type of averaging performed on the data:
54
+
55
+ ``'micro'``:
56
+ Calculate metrics globally by considering each element of the label
57
+ indicator matrix as a label.
58
+ ``'macro'``:
59
+ Calculate metrics for each label, and find their unweighted
60
+ mean. This does not take label imbalance into account.
61
+ ``'weighted'``:
62
+ Calculate metrics for each label, and find their average, weighted
63
+ by support (the number of true instances for each label).
64
+ ``'samples'``:
65
+ Calculate metrics for each instance, and find their average.
66
+
67
+ Will be ignored when ``references`` is binary.
68
+
69
+ pos_label : int, float, bool or str, default=1
70
+ The label of the positive class. Only applied to binary ``references``.
71
+ For multilabel-indicator ``references``, ``pos_label`` is fixed to 1.
72
+
73
+ sample_weight : array-like of shape (n_samples,), default=None
74
+ Sample weights.
75
+
76
+ Returns
77
+ -------
78
+ average_precision : float
79
+ Average precision score.
80
+ """
81
+
82
+ _CITATION = """\
83
+ @article{scikit-learn,
84
+ title={Scikit-learn: Machine Learning in {P}ython},
85
+ 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.},
86
+ journal={Journal of Machine Learning Research},
87
+ volume={12},
88
+ pages={2825--2830},
89
+ year={2011}
90
+ }
91
+ """
92
+
93
+
94
+ @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
95
+ class PRAUC(evaluate.Metric):
96
+ def _info(self):
97
+ return evaluate.MetricInfo(
98
+ description=_DESCRIPTION,
99
+ citation=_CITATION,
100
+ inputs_description=_KWARGS_DESCRIPTION,
101
+ features=datasets.Features(
102
+ {
103
+ "references": datasets.Value("int32"),
104
+ "prediction_scores": datasets.Value("float"),
105
+ }
106
+ ),
107
+ reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.average_precision_score.html"],
108
+ )
109
+
110
+ def _compute(
111
+ self,
112
+ references,
113
+ prediction_scores,
114
+ average="macro",
115
+ sample_weight=None,
116
+ pos_label=1,
117
+ ):
118
+ return {
119
+ "pr_auc": average_precision_score(
120
+ references,
121
+ prediction_scores,
122
+ average=average,
123
+ sample_weight=sample_weight,
124
+ pos_label=pos_label
125
+ )
126
+ }