-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_evaluation.py
489 lines (426 loc) · 16.8 KB
/
model_evaluation.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
from aequitas.flow.optimization import Result
from aequitas.flow.methods import PreProcessing, InProcessing, PostProcessing
from aequitas.flow.methods.postprocessing import Threshold
from aequitas.flow.evaluation import evaluate_fairness, evaluate_performance
from datasets import IIDDataset
from sklearn.metrics import confusion_matrix, accuracy_score
from constants import ABBREVIATIONS
from tqdm import tqdm
import numpy as np
import pandas as pd
import pickle
import os
def evaluate_model(
artifacts_folder: str,
model: tuple[PreProcessing, InProcessing, PostProcessing, Threshold],
X: pd.DataFrame,
y: pd.Series,
s: pd.Series = None,
validation=False,
) -> dict:
"""
Evaluate the performance and fairness of a trained model in a set of data.
Parameters
----------
artifacts_folder : str
Folder where the results should be stored.
model : tuple[PreProcessing, InProcessing, PostProcessing, Threshold]
Model to be evaluated.
X : pd.DataFrame
Features of the data.
y : pd.Series
Labels of the data.
s : pd.Series, optional
Sensitive attribute of the data, by default None.
validation : bool, optional
Whether the data is validation or test data, by default False.
Returns
-------
dict
Dictionary with the results of the evaluation.
"""
if model[0].used_in_inference:
X, y, s = model[0].transform(X.copy(), y.copy(), s.copy())
y_pred = model[1].predict_proba(X, s)
y_pred.to_frame().to_csv(
f"{artifacts_folder}/{'validation' if validation else 'test'}_scores.csv"
)
y_pred = model[2].transform(X, y_pred, s)
if sorted(y_pred.unique().tolist()) != [0, 1]:
y_pred = model[3].transform(X, y_pred, s)
y_pred.to_frame().to_csv(
f"{artifacts_folder}/{'validation' if validation else 'test'}_bin.csv"
)
results = evaluate_performance(y, y_pred)
if s is not None:
results.update(evaluate_fairness(y, y_pred, s, True))
return results
def save_noisy_test_results(
dataset_name: str,
variant: str,
labels: list[list[int]],
noise_rates: list[tuple[int]],
methods: list[str],
experiment_name: str,
n_trials: int,
):
"""
Take a trained model and evaluate its performance and fairness on noisy validation
and test sets.
Parameters
----------
dataset_name : str
Name of the dataset.
variant : str
Variant of the dataset.
labels : list[list[int]]
List of cases of Y-dependant noise injection to consider. Each list should
contain in which classes noise was injected.
noise_rates : list[tuple[int]]
List of noise rates to consider.
methods : list[str]
List of methods to evaluate.
experiment_name : str
Name of the experiment.
n_trials : int
Number of trials set for the experiment.
Examples
--------
>>> save_noisy_test_results(
... "BankAccountFraud",
... "TypeII",
... [[0], [1], [0, 1]],
... [(0, 0), (5, 5), (10, 10), (20, 20)],
... ["lightgbm", "OBNC", "Fair-OBNC"],
... "experiment",
... 50,
... )
"""
data = IIDDataset(dataset_name, variant)
data.load_data()
data.create_splits()
experiment_folder = f"artifacts/{experiment_name}"
t = tqdm(total=len(labels) * len(noise_rates) * len(methods) * n_trials)
for label in labels:
for noise_rate in noise_rates:
dataset_folder = (
f"{ABBREVIATIONS[dataset_name]}_{str.lower(variant)}_"
f"label_{label[0] if len(label)==1 else 'both'}_"
f"{noise_rate[0]}_{noise_rate[1]}"
)
for method in methods:
noisy_test_folder = (
f"{experiment_folder}_noisy_test/{dataset_folder}/{method}"
)
artifacts_folder = f"{experiment_folder}/{dataset_folder}/{method}"
with open(f"{artifacts_folder}/results.pickle", "rb") as f:
clean_results = pickle.load(f)
noisy_results = []
for trial_n in range(n_trials):
os.makedirs(f"{noisy_test_folder}/{trial_n}", exist_ok=True)
with open(
f"{artifacts_folder}/{trial_n}/preprocessing.pickle", "rb"
) as f:
preprocessing = pickle.load(f)
with open(
f"{artifacts_folder}/{trial_n}/inprocessing.pickle", "rb"
) as f:
inprocessing = pickle.load(f)
with open(
f"{artifacts_folder}/{trial_n}/postprocessing.pickle", "rb"
) as f:
postprocessing = pickle.load(f)
with open(
f"{artifacts_folder}/{trial_n}/threshold.pickle", "rb"
) as f:
threshold = pickle.load(f)
model = (preprocessing, inprocessing, postprocessing, threshold)
noisy_test_labels = pd.read_csv(
f"data/{dataset_name}/{variant}/noisy/label_"
f"{label[0] if len(label) == 1 else 'both'}/"
f"test_{noise_rate[0]/100}_{noise_rate[1]/100}.csv",
index_col=0,
)["0"]
noisy_val_labels = pd.read_csv(
f"data/{dataset_name}/{variant}/noisy/label_"
f"{label[0] if len(label) == 1 else 'both'}/validation_"
f"{noise_rate[0]/100}_{noise_rate[1]/100}.csv",
index_col=0,
)["0"]
noisy_test_y = data.test.y.copy()
noisy_val_y = data.validation.y.copy()
if noisy_test_labels.shape[0] > 0:
noisy_test_y.loc[noisy_test_labels.index] = (
noisy_test_labels.values
)
if noisy_val_labels.shape[0] > 0:
noisy_val_y.loc[noisy_val_labels.index] = (
noisy_val_labels.values
)
test_results = evaluate_model(
f"{noisy_test_folder}/{trial_n}",
model,
data.test.X,
noisy_test_y,
data.test.s,
validation=False,
)
val_results = evaluate_model(
f"{noisy_test_folder}/{trial_n}",
model,
data.validation.X,
noisy_val_y,
data.validation.s,
validation=True,
)
noisy_results.append(
Result(
id=trial_n,
test_results=test_results,
hyperparameters=clean_results[
trial_n
].hyperparameters.copy(),
validation_results=val_results,
)
)
t.update()
with open(f"{noisy_test_folder}/results.pickle", "wb") as f:
pickle.dump(noisy_results, f)
t.close()
def get_transformed_labels(
dataset_name: str,
variant: str,
labels: list[list[int]],
noise_rates: list[tuple[int]],
methods: list[str],
experiment_name: str,
n_trials: int,
):
"""
Store the corrected labels obtained by transforming the noisy training set using
each considered label noise correction method.
Parameters
----------
dataset_name : str
Name of the dataset.
variant : str
Variant of the dataset.
labels : list[list[int]]
List of cases of Y-dependant noise injection to consider. Each list should
contain in which classes noise was injected.
noise_rates : list[tuple[int]]
List of noise rates to consider.
methods : list[str]
List of methods to evaluate. Note that only OBNC, Fair-OBNC and Massaging
transform the labels.
experiment_name : str
Name of the experiment.
n_trials : int
Number of trials set for the experiment.
Examples
--------
>>> get_transformed_labels(
... "BankAccountFraud",
... "TypeII",
... [[0], [1], [0, 1]],
... [(0, 0), (5, 5), (10, 10), (20, 20)],
... ["OBNC", "Fair-OBNC", "Massaging"],
... "experiment",
... 50
... )
"""
data = IIDDataset(dataset_name, variant)
data.load_data()
data.create_splits()
t = tqdm(total=len(labels) * len(noise_rates) * len(methods) * n_trials)
for label in labels:
for noise_rate in noise_rates:
for method in methods:
artifacts_folder = (
f"artifacts/{experiment_name}/{ABBREVIATIONS[dataset_name]}_"
f"{str.lower(variant)}_label_"
f"{label[0] if len(label)==1 else 'both'}_"
f"{noise_rate[0]}_{noise_rate[1]}/{method}"
)
for trial_n in range(n_trials):
with open(
f"{artifacts_folder}/{trial_n}/preprocessing.pickle", "rb"
) as f:
preprocessing = pickle.load(f)
noisy_labels = pd.read_csv(
f"data/{dataset_name}/{variant}/noisy/label_"
f'{label[0] if len(label) == 1 else "both"}/train_'
f"{noise_rate[0]/100}_{noise_rate[1]/100}.csv",
index_col=0,
)["0"]
noisy_y = data.train.y.copy()
if noisy_labels.shape[0] > 0:
noisy_y.loc[noisy_labels.index] = noisy_labels.values
_, y_transformed, _ = preprocessing.transform(
data.train.X, noisy_y, data.train.s
)
y_transformed.to_csv(
f"{artifacts_folder}/{trial_n}/transformed_labels.csv"
)
t.update()
t.close()
def fpr(true, corrected):
cm = confusion_matrix(true, corrected)
if len(cm) == 2:
return cm[0][1] / np.sum(cm[0])
elif len(cm) == 0:
return 0
else:
if corrected.unique()[0] == 0:
return 0
else:
return 1
def fnr(true, corrected):
cm = confusion_matrix(true, corrected)
if len(cm) == 2:
return cm[1][0] / np.sum(cm[1])
elif len(cm) == 0:
return 0
else:
if corrected.unique()[0] == 0:
return 1
else:
return 0
def save_reconstruction_error_df(
dataset: str,
variant: str,
labels: list[str],
noise_rates: list[tuple[int]],
methods: list[str],
experiment_name: str,
n_trials: int,
destination_folder: str = "reconstruction_scores",
):
"""
Create csv tables with the reconstruction error, FPR, FNR and percentage of flipped
instances for the conducted experiments.
Parameters
----------
dataset : str
Name of the dataset.
variant : str
Variant of the dataset.
labels : list[str]
List of cases of Y-dependant noise injection to consider. Can include '0', '1'
or 'both'.
noise_rates : list[tuple[int]]
List of noise rates to consider.
methods : list[str]
List of methods to evaluate. Note that only OBNC, Fair-OBNC and Massaging
transform the labels.
n_trials : int
Number of trials set for the experiment.
destination_folder : str, optional
Folder where the results should be stored, by default "reconstruction_scores".
Examples
--------
>>> save_reconstruction_error_df(
... "BankAccountFraud",
... "TypeII",
... ["0", "1", "both"],
... [(0, 0), (5, 5), (10, 10), (20, 20)],
... ["OBNC", "Fair-OBNC", "Massaging"],
... 'noise_injection_experiments',
... 50,
... )
"""
data = IIDDataset(dataset, variant)
data.load_data()
data.create_splits()
os.makedirs(destination_folder, exist_ok=True)
clean_y = data.train.y.copy()
t = tqdm(total=(len(labels) * len(noise_rates) * len(methods) * n_trials))
for label in labels:
os.makedirs(f"{destination_folder}/label_{label}", exist_ok=True)
for nr in noise_rates:
df = pd.DataFrame(
columns=methods,
index=[
"Reconstruction Score",
"FPR",
"FNR",
"Flipped instances",
"Flipped instances %",
"Reconstruction Score (group 0)",
"FPR (group 0)",
"FNR (group 0)",
"Flipped instances (group 0)",
"Flipped instances % (group 0)",
"Reconstruction Score (group 1)",
"FPR (group 1)",
"FNR (group 1)",
"Flipped instances (group 1)",
"Flipped instances % (group 1)",
],
)
noisy_labels = pd.read_csv(
f"data/{dataset}/{variant}/noisy/label_{label}/"
f"train_{nr[0]/100}_{nr[1]/100}.csv",
index_col=0,
)["0"]
noisy_y = data.train.y.copy()
if noisy_labels.shape[0] > 0:
noisy_y.loc[noisy_labels.index] = noisy_labels
noisy_0 = noisy_y.loc[~data.train.s.astype(bool)]
noisy_1 = noisy_y.loc[data.train.s]
metrics = {
metric: {method: [] for method in methods} for metric in df.index
}
for method in methods:
for trial in range(n_trials):
corrected_y = pd.read_csv(
f"artifacts/{experiment_name}/"
f"{ABBREVIATIONS[dataset]}_{str.lower(variant)}_label_{label}_"
f"{nr[0]}_{nr[1]}/{method}/{trial}/transformed_labels.csv",
index_col=0,
)["fraud_bool"]
metrics["Reconstruction Score"][method].append(
accuracy_score(clean_y, corrected_y)
)
metrics["FPR"][method].append(fpr(clean_y, corrected_y))
metrics["FNR"][method].append(fnr(clean_y, corrected_y))
metrics["Flipped instances"][method].append(
(noisy_y != corrected_y).sum()
)
metrics["Flipped instances %"][method].append(
(noisy_y != corrected_y).sum() / noisy_y.shape[0]
)
clean_0 = clean_y.loc[~data.train.s.astype(bool)]
corrected_0 = corrected_y.loc[~data.train.s.astype(bool)]
metrics["Reconstruction Score (group 0)"][method].append(
accuracy_score(clean_0, corrected_0)
)
metrics["FPR (group 0)"][method].append(fpr(clean_0, corrected_0))
metrics["FNR (group 0)"][method].append(fnr(clean_0, corrected_0))
metrics["Flipped instances (group 0)"][method].append(
(noisy_0 != corrected_0).sum()
)
metrics["Flipped instances % (group 0)"][method].append(
(noisy_0 != corrected_0).sum() / noisy_0.shape[0]
)
clean_1 = clean_y.loc[data.train.s]
corrected_1 = corrected_y.loc[data.train.s]
metrics["Reconstruction Score (group 1)"][method].append(
accuracy_score(clean_1, corrected_1)
)
metrics["FPR (group 1)"][method].append(fpr(clean_1, corrected_1))
metrics["FNR (group 1)"][method].append(fnr(clean_1, corrected_1))
metrics["Flipped instances (group 1)"][method].append(
(noisy_1 != corrected_1).sum()
)
metrics["Flipped instances % (group 1)"][method].append(
(noisy_1 != corrected_1).sum() / noisy_1.shape[0]
)
t.update()
for metric, values in metrics.items():
df.loc[metric] = [np.mean(values[method]) for method in df.columns]
df.to_csv(
f"{destination_folder}/label_{label}/noise_rate_{nr[0]}_{nr[1]}.csv",
index=True,
)
t.close()