-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
406 lines (307 loc) · 10.9 KB
/
metrics.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/env python
# coding: utf-8
# Initially Created with ChatGPT
import uuid
from abc import ABC, abstractmethod
import numpy as np
import pandas as pd
import binarybeech.math as math
class Metrics(ABC):
def __init__(self):
pass
def _classification_metrics(self, y_hat, df=None):
confmat = self._confusion_matrix(y_hat, df)
P = self._precision(confmat)
# print(f"precision: {P}")
R = self._recall(confmat)
# print(f"recall: {R}")
F = np.mean(self._F1(P, R))
# print(f"F-score: {F}")
A = self._accuracy(confmat)
return {"precision": P, "recall": R, "F-score": F, "accuracy": A}
@staticmethod
def _precision(m):
return np.diag(m) / np.sum(m, axis=1)
@staticmethod
def _recall(m):
return np.diag(m) / np.sum(m, axis=0)
@staticmethod
def _F1(P, R):
# F = np.zeros_like(P)
# for i in range(len(
return 2 * P * R / (P + R)
@staticmethod
def _accuracy(m):
return np.sum(np.diag(m)) / np.sum(np.sum(m))
@staticmethod
def output_transform(arr):
return arr
@staticmethod
def inverse_transform(arr):
return arr
@abstractmethod
def loss(self, y, y_hat, **kwargs):
pass
@abstractmethod
def loss_prune(self, y, y_hat, **kwargs):
pass
@abstractmethod
def node_value(self, y, **kwargs):
pass
@abstractmethod
def validate(self, y_hat, data):
pass
@abstractmethod
def goodness_of_fit(self, y_hat, data):
pass
@abstractmethod
def bins(self, df, y_name, attribute):
pass
@staticmethod
@abstractmethod
def check(arr):
pass
class RegressionMetrics(Metrics):
def __init__(self):
pass
def loss(self, y, y_hat, **kwargs):
# Implementation of the loss calculation for regression
if "weights" in kwargs.keys():
return math.mean_squared_error_weighted(y, y_hat, kwargs["weights"])
return math.mean_squared_error(y, y_hat)
def loss_prune(self, y, y_hat, **kwargs):
# Implementation of the loss pruning calculation for regression
return self.loss(y, y_hat, **kwargs)
def node_value(self, y, **kwargs):
# Implementation of the node value calculation for regression
return np.nanmean(y)
def validate(self, y, y_hat):
return self._regression_metrics(y, y_hat)
def _regression_metrics(self, y, y_hat):
R2 = math.r_squared(y, y_hat)
return {"R_squared": R2}
def goodness_of_fit(self, y, y_hat):
R2 = math.r_squared(y, y_hat)
return R2
def bins(self, df, y_name, attribute):
y = df[y_name]
kwargs = {}
if "__weights__" in df:
kwargs["weights"] = df["__weights__"].values
y_hat = self.node_value(y, **kwargs)
bins = [[], []]
unique = np.unique(df[attribute])
for u in unique:
y_u = df[df[attribute] == u][y_name]
kwargs = {}
if "__weights__" in df:
kwargs["weights"] = df[df[attribute] == u]["__weights__"].values
y_hat_u = self.node_value(y_u, **kwargs)
if y_hat_u > y_hat:
bins[0].append(u)
else:
bins[1].append(u)
return bins
@staticmethod
def check(x):
return math.check_interval(x)
class RegressionMetricsRegularized(RegressionMetrics):
def __init__(self):
super().__init__()
def node_value(self, y, **kwargs):
y = np.array(y).ravel()
n = y.shape[0]
lambda_l1 = kwargs.get("lambda_l1")
lambda_l2 = kwargs.get("lambda_l2")
y_sum = np.sum(y)
if y_sum < -lambda_l1:
return (y_sum + lambda_l1)/(n + lambda_l2)
elif y_sum > lambda_l1:
return (y_sum - lambda_l1)/(n + lambda_l2)
else:
return 0.
class LogisticMetrics(Metrics):
def __init__(self):
pass
def loss(self, y, y_hat, **kwargs):
# Implementation of the loss calculation for logistic
return math.logistic_loss(y, y_hat)
def loss_prune(self, y, y_hat, **kwargs):
# Implementation of the loss pruning calculation for logistic
return math.misclassification_cost(y)
def node_value(self, y, **kwargs):
# Implementation of the node value calculation for logistic
return math.max_probability(y)
def validate(self, y, y_hat):
return self._classification_metrics(y, y_hat)
def _confusion_matrix(self, y, y_hat):
m = np.zeros((2, 2), dtype=int)
y_hat = np.round(np.clip(y_hat, 0.0, 1.0)).astype(int)
for i, y_ in enumerate(y):
y_ = int(y_)
y_hat_i = y_hat[i]
m[y_, y_hat_i] += 1
return m
def goodness_of_fit(self, y, y_hat):
confmat = self._confusion_matrix(y, y_hat)
A = self._accuracy(confmat)
return A
@staticmethod
def output_transform(arr):
return math.logistic(arr)
@staticmethod
def inverse_transform(arr):
return math.logit(arr)
def bins(self, df, y_name, attribute):
y = df[y_name]
kwargs = {}
if "__weights__" in df:
kwargs ["weights"] = df["__weights__"].values
y_hat = self.node_value(y, **kwargs)
bins = [[], []]
unique = np.unique(df[attribute])
for u in unique:
y_u = df[df[attribute] == u][y_name]
kwargs = {}
if "__weights__" in df:
kwargs["weights"] = df[df[attribute] == u]["__weights__"].values
y_hat_u = self.node_value(y_u, **kwargs)
if y_hat_u == y_hat:
bins[0].append(u)
else:
bins[1].append(u)
return bins
@staticmethod
def check(arr):
x = arr[~pd.isna(arr)]
unique = np.unique(x)
L = len(unique)
# r = l / x.size
dtype = x.values.dtype
if (
np.issubdtype(dtype, np.number)
and L == 2
and np.min(x) == 0
and np.max(x) == 1
):
return True
return False
class ClassificationMetrics(Metrics):
def __init__(self):
pass
def loss(self, y, y_hat, **kwargs):
# Implementation of the loss calculation for classification
if "weights" in kwargs.keys():
return math.gini_impurity_weighted(y, kwargs["weights"])
return math.gini_impurity(y)
def loss_prune(self, y, y_hat, **kwargs):
# Implementation of the loss pruning calculation for classification
# if "weights" in kwargs.keys():
# print(len(y), len(y_hat), len(kwargs["weights"]))
# return math.misclassification_cost_weighted(y, kwargs["weights"])
return math.misclassification_cost(y)
def node_value(self, y, **kwargs):
# Implementation of the node value calculation for classification
if "weights" in kwargs.keys():
return math.majority_class_weighted(y, kwargs["weights"])
return math.majority_class(y)
def validate(self, y, y_hat):
return self._classification_metrics(y, y_hat)
def _confusion_matrix(self, y, y_hat):
unique = np.unique(y)
classes = unique.tolist() # self.tree.classes()
n_classes = len(classes)
confmat = np.zeros((n_classes, n_classes))
for i, y_ in enumerate(y):
val_pred = y_hat[i]
val_true = y_
i_pred = classes.index(val_pred)
i_true = classes.index(val_true)
confmat[i_true, i_pred] += 1
return confmat
def goodness_of_fit(self, y, y_hat):
confmat = self._confusion_matrix(y, y_hat)
A = self._accuracy(confmat)
return A
def bins(self, df, y_name, attribute):
y = df[y_name]
kwargs = {}
if "__weights__" in df:
kwargs["weights"] = df["__weights__"].values
y_hat = self.node_value(y, **kwargs)
bins = [[], []]
unique = np.unique(df[attribute])
for u in unique:
y_u = df[df[attribute] == u][y_name]
kwargs = {}
if "__weights__" in df:
kwargs["weights"] = df[df[attribute] == u]["__weights__"].values
y_hat_u = self.node_value(y_u, **kwargs)
if y_hat_u == y_hat:
bins[0].append(u)
else:
bins[1].append(u)
return bins
@staticmethod
def check(x):
return math.check_nominal(x)
class ClassificationMetricsEntropy(ClassificationMetrics):
def __init__(self):
pass
def loss(self, y, y_hat, **kwargs):
# Implementation of the loss calculation for classification
if "weights" in kwargs.keys():
return math.shannon_entropy_weighted(y, y_hat, kwargs["weights"])
return math.shannon_entropy(y)
# =============================
class UnsupervisedMetrics(Metrics):
def __init__(self):
pass
def loss(self, y, y_hat, **kwargs):
return np.inf
def loss_prune(self, y, y_hat, **kwargs):
return self.loss(y, y_hat, **kwargs)
def node_value(self, y, **kwargs):
return f"cluster {str(uuid.uuid4())}"
def validate(self, y, y_hat):
return {}
def goodness_of_fit(self, y, y_hat):
return 0.0
def bins(self, df, y_name, attribute):
bins = [[], []]
unique = np.unique(df[attribute])
L = len(unique)
for i, u in enumerate(unique):
if i > L / 2:
bins[0].append(u)
else:
bins[1].append(u)
return bins
@staticmethod
def check(arr):
if not arr:
return True
return False
# =============================
class MetricsFactory:
def __init__(self):
self.metrics = {}
def register(self, metrics_type, metrics_class):
self.metrics[metrics_type] = metrics_class
def create_metrics(self, metrics_type, algorithm_kwargs):
if metrics_type in self.metrics:
return self.metrics[metrics_type]()
else:
raise ValueError("Invalid metrics type")
def from_data(self, y, algorithm_kwargs):
for name, cls in self.metrics.items():
if cls.check(y):
return cls(), name
metrics_factory = MetricsFactory()
metrics_factory.register("regression", RegressionMetrics)
metrics_factory.register("regression:regularized", RegressionMetrics)
metrics_factory.register("classification:gini", ClassificationMetrics)
metrics_factory.register("classification:entropy", ClassificationMetricsEntropy)
metrics_factory.register("logistic", LogisticMetrics)
metrics_factory.register("classification", ClassificationMetrics)
metrics_factory.register("clustering", UnsupervisedMetrics)