/
_skm_wrappers.py
176 lines (138 loc) · 7.46 KB
/
_skm_wrappers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import sklearn.metrics as skm
from math import sqrt
from ._metrics_engine import metric_by_group
def group_accuracy_score(y_true, y_pred, group_membership, *,
normalize=True,
sample_weight=None):
"""Wrap the :py:func:`sklearn.metrics.accuracy_score` routine.
The arguments remain the same, with `group_membership` added.
However, the only positional arguments supported are `y_true`,
`y_pred` and `group_membership`.
All others must be specified by name.
"""
def internal_acc_wrapper(y_true, y_pred, sample_weight=None):
return skm.accuracy_score(y_true, y_pred,
normalize,
sample_weight)
return metric_by_group(internal_acc_wrapper, y_true, y_pred, group_membership, sample_weight)
def group_confusion_matrix(y_true, y_pred, group_membership, *,
labels=None,
sample_weight=None):
"""Wrap the :py:func:`sklearn.metrics.confusion_matrix` routine.
The arguments remain the same, with `group_membership` added.
However, the only positional arguments supported are `y_true`,
`y_pred` and `group_membership`.
All others must be specified by name.
"""
def internal_cm_wrapper(y_true, y_pred, sample_weight=None):
return skm.confusion_matrix(y_true, y_pred,
labels,
sample_weight)
return metric_by_group(internal_cm_wrapper, y_true, y_pred, group_membership, sample_weight)
def group_precision_score(y_true, y_pred, group_membership, *,
labels=None, pos_label=1, average='binary',
sample_weight=None):
"""Wrap the :py:func:`sklearn.metrics.precision_score` routine.
The arguments remain the same, with `group_membership` added.
However, the only positional arguments supported are `y_true`,
`y_pred` and `group_membership`.
All others must be specified by name.
"""
def internal_prec_wrapper(y_true, y_pred, sample_weight=None):
return skm.precision_score(y_true, y_pred,
labels=labels, pos_label=pos_label, average=average,
sample_weight=sample_weight)
return metric_by_group(internal_prec_wrapper, y_true, y_pred, group_membership, sample_weight)
def group_recall_score(y_true, y_pred, group_membership, *,
labels=None, pos_label=1, average='binary',
sample_weight=None):
"""Wrap the :py:func:`sklearn.metrics.recall_score` routine.
The arguments remain the same, with `group_membership` added.
However, the only positional arguments supported are `y_true`,
`y_pred` and `group_membership`.
All others must be specified by name.
"""
def internal_recall_wrapper(y_true, y_pred, sample_weight=None):
return skm.recall_score(y_true, y_pred,
labels=labels, pos_label=pos_label, average=average,
sample_weight=sample_weight)
return metric_by_group(internal_recall_wrapper,
y_true, y_pred, group_membership, sample_weight)
def group_roc_auc_score(y_true, y_pred, group_membership, *,
average='macro', max_fpr=None,
sample_weight=None):
"""Wrap the :py:func:`sklearn.metrics.roc_auc_score` routine.
The arguments remain the same, with `group_membership` added.
However, the only positional arguments supported are `y_true`,
`y_pred` and `group_membership`.
All others must be specified by name.
"""
def internal_ras_wrapper(y_true, y_pred, sample_weight=None):
return skm.roc_auc_score(y_true, y_pred,
average=average, max_fpr=max_fpr,
sample_weight=sample_weight)
return metric_by_group(internal_ras_wrapper,
y_true, y_pred, group_membership, sample_weight)
def group_zero_one_loss(y_true, y_pred, group_membership, *,
normalize=True,
sample_weight=None):
"""Wrap the :py:func:`sklearn.metrics.zero_one_loss` routine.
The arguments remain the same, with `group_membership` added.
However, the only positional arguments supported are `y_true`,
`y_pred` and `group_membership`.
All others must be specified by name.
"""
def internal_zol_wrapper(y_true, y_pred, sample_weight=None):
return skm.zero_one_loss(y_true, y_pred,
normalize=normalize,
sample_weight=sample_weight)
return metric_by_group(internal_zol_wrapper, y_true, y_pred, group_membership, sample_weight)
# --------------------------------------------------------------------------------------
def group_mean_squared_error(y_true, y_pred, group_membership, *,
multioutput='uniform_average',
sample_weight=None):
"""Wrap the :py:func:`sklearn.metrics.mean_squared_error` routine.
The arguments remain the same, with `group_membership` added.
However, the only positional arguments supported are `y_true`,
`y_pred` and `group_membership`.
All others must be specified by name.
"""
def internal_mse_wrapper(y_true, y_pred, sample_weight=None):
return skm.mean_squared_error(y_true, y_pred,
multioutput=multioutput,
sample_weight=sample_weight)
return metric_by_group(internal_mse_wrapper,
y_true, y_pred, group_membership, sample_weight=sample_weight)
def group_root_mean_squared_error(y_true, y_pred, group_membership, *,
multioutput='uniform_average',
sample_weight=None):
"""Wrap the :py:func:`sklearn.metrics.mean_squared_error` routine.
The arguments remain the same, with `group_membership` added.
The result is then square rooted.
However, the only positional arguments supported are `y_true`,
`y_pred` and `group_membership`.
All others must be specified by name.
"""
def internal_rmse_wrapper(y_true, y_pred, sample_weight=None):
return sqrt(skm.mean_squared_error(y_true, y_pred,
multioutput=multioutput,
sample_weight=sample_weight))
return metric_by_group(internal_rmse_wrapper,
y_true, y_pred, group_membership, sample_weight=sample_weight)
def group_r2_score(y_true, y_pred, group_membership, *,
multioutput='uniform_average',
sample_weight=None):
"""Wrap the :py:func:`sklearn.metrics.r2_score` routine.
The arguments remain the same, with `group_membership` added.
However, the only positional arguments supported are `y_true`,
`y_pred` and `group_membership`.
All others must be specified by name.
"""
def internal_r2_wrapper(y_true, y_pred, sample_weight=None):
return skm.r2_score(y_true, y_pred,
multioutput=multioutput,
sample_weight=sample_weight)
return metric_by_group(internal_r2_wrapper,
y_true, y_pred, group_membership, sample_weight=sample_weight)