-
Notifications
You must be signed in to change notification settings - Fork 1
/
temp.py
716 lines (520 loc) · 25.7 KB
/
temp.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
"""
This file contains functions for performing running fair regression
algorithms and the set of baseline methods.
See end of file to see sample use of running fair regression.
"""
from __future__ import print_function
import functools
import numpy as np
import pandas as pd
import data_parser as parser
from sklearn.model_selection import train_test_split
# import matplotlib.pyplot as plt
import pickle
import eval as evaluate
import solvers as solvers
import exp_grad as fairlearn
from collections import Counter
import math
print = functools.partial(print, flush=True)
# Global Variables
TEST_SIZE = 0.5 # fraction of observations from each protected group
Theta = np.linspace(0, 1.0, 41)
eta = (Theta[1] - Theta[0])/2
DATA_SPLIT_SEED = 4
_SMALL = True # small scale dataset for speed and testing
_CORESET = True # fair coreset for the data
_UNIFORM = True # uniform sampled dataset
_DUPLICATE = True # to duplicate the sample points proportional to their weights
_TEST = True # whether you want to have test data or not
def coresetSample(x, a, y, rval):
"""
Randomly subsample a smaller dataset of certain size
"""
# gives sizes for each group
print("starting to get the coreset....")
AkSizes = Counter(a)
# AkSizes[0] and AkSizes[1]
lGroups = len(AkSizes)
print("computing svd now...")
# svd to get leverage scores
u,s,v = np.linalg.svd(x, full_matrices=False)
sqrtLevScores = np.linalg.norm(u, 2, 1)
levScores = np.square(sqrtLevScores)
print("Got the svd...")
d = sum(levScores)
logl = math.log(lGroups, 2)
log1ByEta = math.log(1.0/eta, 2)
tpart = d + logl + log1ByEta
invAkSizes = np.array([1.0/AkSizes[x] for x in a])
tprob = tpart * invAkSizes
samplingProb = levScores + tprob
# rvals = [2**i for i in range(0, int(math.log(x.shape[0], 2)-1))]
# rvals = [0.5, 1, 1.5, 2.0]
weights = {}
sampledX = {}
sampledA = {}
sampledY = {}
uniqueForRVal = {}
weightsOut = []
print("Going for sampling...")
# for j in range(len(rvals)):
# r = rvals[j]
r = rval
weights[r] = []
sampledX[r] = pd.DataFrame(columns=x.columns)
sampledA[r] = pd.Series([])
sampledY[r] = pd.Series([])
uniqPointsSampled = 0
for i in range(len(x)):
tossProb = min(r*samplingProb[i], 1)
uniSample = np.random.random_sample()
if uniSample <= tossProb:
uniqPointsSampled += 1
weight_i = 1.0/tossProb
weights[r].append(weight_i)
weight_i = math.floor(weight_i)
if weight_i < 1:
weight_i = 1
# weightsOut.append(weight_i)
if _DUPLICATE:
# add point weight many times
for w in range(weight_i):
sampledX[r] = sampledX[r].append(x.iloc[i])
sampledA[r] = sampledA[r].append(pd.Series([a[i]]))
sampledY[r] = sampledY[r].append(pd.Series([y[i]]))
else:
sampledX[r] = sampledX[r].append(x.iloc[i])
sampledA[r] = sampledA[r].append(pd.Series([a[i]]))
sampledY[r] = sampledY[r].append(pd.Series([y[i]]))
sampledX[r].index = range(len(sampledX[r]))
sampledA[r].index = range(len(sampledX[r]))
sampledY[r].index = range(len(sampledX[r]))
print("num Points Sampled -- for r = ", r, " and uniq num points = ", uniqPointsSampled)
print("num Points Sampled -- for r = ", r, " and num points = ", len(sampledX[r]))
uniqueForRVal[r] = uniqPointsSampled
# for i in weightsOut:
# print(i)
return sampledX[r], sampledA[r], sampledY[r], uniqueForRVal[r]
def uniformSample(x, a, y, sampleSize, rval):
"""
Randomly subsample a smaller dataset of certain size
"""
numPoints = x.shape[0]
# gives sizes for each group
print("starting to get the uniform sample of size ---....", sampleSize)
AkSizes = Counter(a)
# AkSizes[0] and AkSizes[1]
lGroups = len(AkSizes)
# rvals = [2**i for i in range(0, int(math.log(x.shape[0], 2)-1))]
# rvals = [0.5, 1.0, 1.5, 2.0]
weights = {}
sampledX = {}
sampledA = {}
sampledY = {}
weightsOut = []
print("Going for sampling...")
# for j in range(len(rvals)):
r = rval
weights[r] = []
sampledX[r] = pd.DataFrame(columns=x.columns)
sampledA[r] = pd.Series([])
sampledY[r] = pd.Series([])
uniqPointsSampled = 0
# for i in range(sampleSize):
uniSampledIds = np.random.choice(range(x.shape[0]), sampleSize)
for i in range(len(uniSampledIds)):
sampledId = uniSampledIds[i]
weight_i = math.floor(numPoints/sampleSize)
if _DUPLICATE:
for w in range(weight_i):
sampledX[r] = sampledX[r].append(x.iloc[sampledId])
sampledA[r] = sampledA[r].append(pd.Series([a[sampledId]]))
sampledY[r] = sampledY[r].append(pd.Series([y[sampledId]]))
else:
sampledX[r] = sampledX[r].append(x.iloc[sampledId])
sampledA[r] = sampledA[r].append(pd.Series([a[sampledId]]))
sampledY[r] = sampledY[r].append(pd.Series([y[sampledId]]))
sampledX[r].index = range(len(sampledX[r]))
sampledA[r].index = range(len(sampledX[r]))
sampledY[r].index = range(len(sampledX[r]))
# print("num Points Sampled -- for r = ", r, " and uniq num points = ", uniqPointsSampled)
print("num Points Uniformly Sampled after weighing -- for r = ", r, " and num points = ", len(sampledX[r]))
# for i in weightsOut:
# print(i)
return sampledX[r], sampledA[r], sampledY[r]
def train_test_split_groups(x, a, y, random_seed=DATA_SPLIT_SEED):
"""Split the input dataset into train and test sets
TODO: Need to make sure both train and test sets have enough
observations from each subgroup
"""
# size of the training data
groups = list(a.unique())
x_train_sets = {}
x_test_sets = {}
y_train_sets = {}
y_test_sets = {}
a_train_sets = {}
a_test_sets = {}
for g in groups:
x_g = x[a == g]
a_g = a[a == g]
y_g = y[a == g]
x_train_sets[g], x_test_sets[g], a_train_sets[g], a_test_sets[g], y_train_sets[g], y_test_sets[g] = train_test_split(x_g, a_g, y_g, test_size=TEST_SIZE, random_state=random_seed)
x_train = pd.concat(x_train_sets.values())
x_test = pd.concat(x_test_sets.values())
y_train = pd.concat(y_train_sets.values())
y_test = pd.concat(y_test_sets.values())
a_train = pd.concat(a_train_sets.values())
a_test = pd.concat(a_test_sets.values())
# resetting the index
x_train.index = range(len(x_train))
y_train.index = range(len(y_train))
a_train.index = range(len(a_train))
x_test.index = range(len(x_test))
y_test.index = range(len(y_test))
a_test.index = range(len(a_test))
return x_train, a_train, y_train, x_test, a_test, y_test
def subsample(x, a, y, size, random_seed=DATA_SPLIT_SEED):
"""
Randomly subsample a smaller dataset of certain size
"""
toss = 1 - size / (len(x))
x1, _, a1, _, y1 ,_ = train_test_split(x, a, y, test_size=toss, random_state=random_seed)
x1.index = range(len(x1))
y1.index = range(len(x1))
a1.index = range(len(x1))
return x1, a1, y1
def fair_train_test(dataset, size, eps_list, learner, rval, constraint="DP",
loss="square", random_seed=DATA_SPLIT_SEED, init_cache=[]):
"""
Input:
- dataset name
- size parameter for data parser
- eps_list: list of epsilons for exp_grad
- learner: the solver for CSC
- constraint: fairness constraint name
- loss: loss function name
- random_seed
Output: Results for
- exp_grad: (eps, loss) for training and test sets
- benchmark method: (eps, loss) for training and test sets
"""
if dataset == 'law_school':
x, a, y = parser.clean_lawschool_full()
elif dataset == 'communities':
x, a, y = parser.clean_communities_full()
elif dataset == 'adult':
x, a, y = parser.clean_adult_full()
else:
raise Exception('DATA SET NOT FOUND!')
if _SMALL:
x, a, y = subsample(x, a, y, size)
if _TEST:
x_train, a_train, y_train, x_test, a_test, y_test = train_test_split_groups(x, a, y, random_seed=DATA_SPLIT_SEED)
else:
x_train, a_train, y_train = x, a, y
x_train.index = range(len(x_train))
a_train.index = range(len(a_train))
y_train.index = range(len(y_train))
if _CORESET:
xcor, acor, ycor, uniqSamples = coresetSample(x_train, a_train, y_train, rval)
print("got the coreset")
if _UNIFORM:
xuni, auni, yuni = uniformSample(x_train, a_train, y_train, uniqSamples, rval)
print("got the unformly sampled points...")
fair_train_model = {}
fair_cor_model = {}
fair_uni_model = {}
train_evaluation = {}
cor_evaluation = {}
uni_evaluation = {}
cor_on_train_evaluation = {}
uni_on_train_evaluation = {}
train_on_cor_evaluation = {}
train_on_uni_evaluation = {}
if _TEST:
train_on_test_evaluation = {}
cor_on_test_evaluation = {}
uni_on_test_evaluation = {}
result = {}
'''
for eps in eps_list:
# ------------------------------------------------------------------------------------------
print("Running on full original data....")
fair_train_model[eps] = fairlearn.train_FairRegression(x_train, a_train, y_train, eps, Theta, learner, constraint, loss, init_cache=init_cache)
train_evaluation[eps] = evaluate.evaluate_FairModel(x_train, a_train, y_train, loss, fair_train_model[eps]['exp_grad_result'], Theta)
if _TEST:
train_on_test_evaluation[eps] = evaluate.evaluate_FairModel(x_test, a_test, y_test, loss, fair_train_model[eps]['exp_grad_result'], Theta)
# ------------------------------------------------------------------------------------------
print("Running on the coreset....")
fair_cor_model[eps] = fairlearn.train_FairRegression(xcor, acor, ycor, eps, Theta, learner, constraint, loss, init_cache=init_cache)
print("Evaluating Coreset model on Coreset Sketch...")
cor_evaluation[eps] = evaluate.evaluate_FairModel(xcor, acor, ycor, loss, fair_cor_model[eps]['exp_grad_result'], Theta)
print("Applying Coreset model on Train Data")
cor_on_train_evaluation[eps] = evaluate.evaluate_FairModel(x_train, a_train, y_train, loss, fair_cor_model[eps]['exp_grad_result'], Theta)
if _TEST:
print("Applying Coreset model on Test Data")
cor_on_test_evaluation[eps] = evaluate.evaluate_FairModel(x_test, a_test, y_test, loss, fair_cor_model[eps]['exp_grad_result'], Theta)
print("Applying Train model on Coreset")
train_on_cor_evaluation[eps] = evaluate.evaluate_FairModel(xcor, acor, ycor, loss, fair_train_model[eps]['exp_grad_result'], Theta)
# ------------------------------------------------------------------------------------------
print("Running on Uniform samples...")
fair_uni_model[eps] = fairlearn.train_FairRegression(xuni, auni, yuni, eps, Theta, learner, constraint, loss, init_cache=init_cache)
print("Evaluating Uniform model on Uni Sketch...")
uni_evaluation[eps] = evaluate.evaluate_FairModel(xuni, auni, yuni, loss, fair_uni_model[eps]['exp_grad_result'], Theta)
print("Applying Uni model on Train Data")
uni_on_train_evaluation[eps] = evaluate.evaluate_FairModel(x_train, a_train, y_train, loss, fair_uni_model[eps]['exp_grad_result'], Theta)
if _TEST:
print("Applying Uni model on Test Data")
uni_on_test_evaluation[eps] = evaluate.evaluate_FairModel(x_test, a_test, y_test, loss, fair_uni_model[eps]['exp_grad_result'], Theta)
print("Applying train model on Uniform Sketch")
train_on_uni_evaluation[eps] = evaluate.evaluate_FairModel(xuni, auni, yuni, loss, fair_train_model[eps]['exp_grad_result'], Theta)
# ------------------------------------------------------------------------------------------
print("Done with r = ", r)
print("done with epsilon", eps)
result['dataset'] = dataset
result['learner'] = learner.name
result['loss'] = loss
result['constraint'] = constraint
result['train_eval'] = train_evaluation
result['cor_eval'] = cor_evaluation
result['uni_eval'] = uni_evaluation
result['cor_on_train_eval'] = cor_on_train_evaluation
result['uni_on_train_eval'] = uni_on_train_evaluation
result['train_on_cor_eval'] = train_on_cor_evaluation
result['train_on_uni_eval'] = train_on_uni_evaluation
if _TEST:
result['train_on_test_eval'] = train_on_test_evaluation
result['cor_on_test_eval'] = cor_on_test_evaluation
result['uni_on_test_eval'] = uni_on_test_evaluation
'''
return result
def read_result_list(result_list):
"""
Parse the experiment a list of experiment result and print out info
"""
for result in result_list:
learner = result['learner']
dataset = result['dataset']
train_eval = result['train_eval']
cor_eval = result['cor_eval']
uni_eval = result['uni_eval']
cor_on_train_eval = result['cor_on_train_eval']
uni_on_train_eval = result['uni_on_train_eval']
train_on_cor_eval = result['train_on_cor_eval']
train_on_uni_eval = result['train_on_uni_eval']
if _TEST:
train_on_test_eval = result['train_on_test_eval']
cor_on_test_eval = result['cor_on_test_eval']
uni_on_test_eval = result['uni_on_test_eval']
loss = result['loss']
constraint = result['constraint']
learner = result['learner']
dataset = result['dataset']
eps_vals = train_eval.keys()
train_disp_dic = {}
cor_disp_dic = {}
uni_disp_dic = {}
cor_on_train_disp_dic = {}
uni_on_train_disp_dic = {}
train_on_cor_disp_dic = {}
train_on_uni_disp_dic = {}
train_err_dic = {}
cor_err_dic = {}
uni_err_dic = {}
cor_on_train_err_dic = {}
uni_on_train_err_dic = {}
train_on_cor_err_dic = {}
train_on_uni_err_dic = {}
if _TEST:
train_on_test_disp_dic = {}
train_on_test_err_dic = {}
cor_on_test_disp_dic = {}
cor_on_test_err_dic = {}
uni_on_test_disp_dic = {}
uni_on_test_err_dic = {}
test_loss_std_dic = {}
train_on_test_disp_dev_dic = {}
for eps in eps_vals:
train_disp = train_eval[eps]["DP_disp"]
cor_disp = cor_eval[eps]["DP_disp"]
uni_disp = uni_eval[eps]["DP_disp"]
cor_on_train_disp = cor_on_train_eval[eps]["DP_disp"]
uni_on_train_disp = uni_on_train_eval[eps]["DP_disp"]
train_on_cor_disp = train_on_cor_eval[eps]["DP_disp"]
train_on_uni_disp = train_on_uni_eval[eps]["DP_disp"]
if _TEST:
train_on_test_disp = train_on_test_eval[eps]["DP_disp"]
cor_on_test_disp = cor_on_test_eval[eps]["DP_disp"]
uni_on_test_disp = uni_on_test_eval[eps]["DP_disp"]
train_disp_dic[eps] = train_disp
cor_disp_dic[eps] = cor_disp
uni_disp_dic[eps] = uni_disp
cor_on_train_disp_dic[eps] = cor_on_train_disp
train_on_cor_disp_dic[eps] = train_on_cor_disp
uni_on_train_disp_dic[eps] = uni_on_train_disp
train_on_uni_disp_dic[eps] = train_on_uni_disp
if _TEST:
train_on_test_disp_dic[eps] = train_on_test_disp
cor_on_test_disp_dic[eps] = cor_on_test_disp
uni_on_test_disp_dic[eps] = uni_on_test_disp
# test_loss_std_dic[eps] = train_on_test_eval[eps]['loss_std']
# train_on_test_disp_dev_dic[eps] = train_on_test_eval[eps]['disp_std']
if loss == "square":
# taking the RMSE
train_err_dic[eps] = np.sqrt(train_eval[eps]['weighted_loss'])
cor_err_dic[eps] = np.sqrt(cor_eval[eps]['weighted_loss'])
uni_err_dic[eps] = np.sqrt(uni_eval[eps]['weighted_loss'])
cor_on_train_err_dic[eps] = np.sqrt(cor_on_train_eval[eps]['weighted_loss'])
train_on_cor_err_dic[eps] = np.sqrt(train_on_cor_eval[eps]['weighted_loss'])
uni_on_train_err_dic[eps] = np.sqrt(uni_on_train_eval[eps]['weighted_loss'])
train_on_uni_err_dic[eps] = np.sqrt(train_on_uni_eval[eps]['weighted_loss'])
if _TEST:
train_on_test_err_dic[eps] = np.sqrt(train_on_test_eval[eps]['weighted_loss'])
cor_on_test_err_dic[eps] = np.sqrt(cor_on_test_eval[eps]['weighted_loss'])
uni_on_test_err_dic[eps] = np.sqrt(uni_on_test_eval[eps]['weighted_loss'])
else:
train_err_dic[eps] = (train_eval[eps]['weighted_loss'])
cor_err_dic[eps] = (cor_eval[eps]['weighted_loss'])
uni_err_dic[eps] = (uni_eval[eps]['weighted_loss'])
cor_on_train_err_dic[eps] = (cor_on_train_eval[eps]['weighted_loss'])
train_on_cor_err_dic[eps] = (train_on_cor_eval[eps]['weighted_loss'])
uni_on_train_err_dic[eps] = (uni_on_train_eval[eps]['weighted_loss'])
train_on_uni_err_dic[eps] = (train_on_uni_eval[eps]['weighted_loss'])
if _TEST:
train_on_test_err_dic[eps] = (train_on_test_eval[eps]['weighted_loss'])
cor_on_test_err_dic[eps] = (cor_on_test_eval[eps]['weighted_loss'])
uni_on_test_err_dic[eps] = (uni_on_test_eval[eps]['weighted_loss'])
# taking the pareto frontier
train_disp_list = [train_disp_dic[k] for k in eps_vals]
cor_disp_list = [cor_disp_dic[k] for k in eps_vals]
uni_disp_list = [uni_disp_dic[k] for k in eps_vals]
cor_on_train_disp_list = [cor_on_train_disp_dic[k] for k in eps_vals]
train_on_cor_disp_list = [train_on_cor_disp_dic[k] for k in eps_vals]
uni_on_train_disp_list = [uni_on_train_disp_dic[k] for k in eps_vals]
train_on_uni_disp_list = [train_on_uni_disp_dic[k] for k in eps_vals]
if _TEST:
train_on_test_disp_list = [train_on_test_disp_dic[k] for k in eps_vals]
cor_on_test_disp_list = [cor_on_test_disp_dic[k] for k in eps_vals]
uni_on_test_disp_list = [uni_on_test_disp_dic[k] for k in eps_vals]
train_err_list = [train_err_dic[k] for k in eps_vals]
cor_err_list = [cor_err_dic[k] for k in eps_vals]
uni_err_list = [uni_err_dic[k] for k in eps_vals]
cor_on_train_err_list = [cor_on_train_err_dic[k] for k in eps_vals]
train_on_cor_err_list = [train_on_cor_err_dic[k] for k in eps_vals]
uni_on_train_err_list = [uni_on_train_err_dic[k] for k in eps_vals]
train_on_uni_err_list = [train_on_uni_err_dic[k] for k in eps_vals]
if _TEST:
train_on_test_err_list = [train_on_test_err_dic[k] for k in eps_vals]
cor_on_test_err_list = [cor_on_test_err_dic[k] for k in eps_vals]
uni_on_test_err_list = [uni_on_test_err_dic[k] for k in eps_vals]
if loss == "square":
show_loss = 'RMSE'
else:
show_loss = loss
info = str('Dataset: '+ dataset + '; loss: ' + loss + '; Solver: '+ learner)
print(info)
train_data = {'specified epsilon': list(eps_vals), 'SP disparity':train_disp_list, show_loss : train_err_list}
train_performance = pd.DataFrame(data=train_data)
cor_data = {'specified epsilon': list(eps_vals), 'SP disparity':cor_disp_list, show_loss : cor_err_list}
cor_performance = pd.DataFrame(data=cor_data)
uni_data = {'specified epsilon': list(eps_vals), 'SP disparity':uni_disp_list, show_loss : uni_err_list}
uni_performance = pd.DataFrame(data=uni_data)
cor_on_train_data = {'specified epsilon': list(eps_vals), 'SP disparity':cor_on_train_disp_list, show_loss : cor_on_train_err_list}
cor_on_train_performance = pd.DataFrame(data=cor_on_train_data)
train_on_cor_data = {'specified epsilon': list(eps_vals), 'SP disparity':train_on_cor_disp_list, show_loss : train_on_cor_err_list}
train_on_cor_performance = pd.DataFrame(data=train_on_cor_data)
uni_on_train_data = {'specified epsilon': list(eps_vals), 'SP disparity':uni_on_train_disp_list, show_loss : uni_on_train_err_list}
uni_on_train_performance = pd.DataFrame(data=uni_on_train_data)
train_on_uni_data = {'specified epsilon': list(eps_vals), 'SP disparity':train_on_uni_disp_list, show_loss : train_on_uni_err_list}
train_on_uni_performance = pd.DataFrame(data=train_on_uni_data)
if _TEST:
train_on_test_data = {'specified epsilon': list(eps_vals), 'SP disparity':train_on_test_disp_list, show_loss : train_on_test_err_list}
train_on_test_performance = pd.DataFrame(data=train_on_test_data)
cor_on_test_data = {'specified epsilon': list(eps_vals), 'SP disparity':cor_on_test_disp_list, show_loss : cor_on_test_err_list}
cor_on_test_performance = pd.DataFrame(data=cor_on_test_data)
uni_on_test_data = {'specified epsilon': list(eps_vals), 'SP disparity':uni_on_test_disp_list, show_loss : uni_on_test_err_list}
uni_on_test_performance = pd.DataFrame(data=uni_on_test_data)
# Print out experiment info.
print('Train set trade-off:')
print(train_performance)
print("------------------------------------------------")
print('Coreset set trade-off:')
print(cor_performance)
print("------------------------------------------------")
print('Uniform Sketch set trade-off:')
print(uni_performance)
print("------------------------------------------------")
print('Coreset Model on Train set trade-off:')
print(cor_on_train_performance)
print("------------------------------------------------")
print('Uniform Model on Train set trade-off:')
print(uni_on_train_performance)
print("------------------------------------------------")
if _TEST:
print('Train on Test set trade-off:')
print(train_on_test_performance)
print("------------------------------------------------")
print('Cor on Test set trade-off:')
print(cor_on_test_performance)
print("------------------------------------------------")
print('Uni on Test set trade-off:')
print(uni_on_test_performance)
print("------------------------------------------------")
print('Train Model on Coreset set trade-off:')
print(train_on_cor_performance)
print("------------------------------------------------")
print('Train on Uniform data set trade-off:')
print(train_on_uni_performance)
print("------------------------------------------------")
# Sample instantiation of running the fair regeression algorithm
# eps_list = [0.275, 0.31, 1] # range of specified disparity values
eps_list = [0.1] # range of specified disparity values
n = 2000 # size of the sub-sampled dataset, when the flag SMALL is True
dataset = 'adult' # name of the data set
constraint = "DP" # name of the constraint; so far limited to demographic parity (or statistical parity)
loss = "square" # name of the loss function
learner = solvers.LeastSquaresLearner(Theta) # Specify a supervised learning oracle oracle
info = str('Dataset: '+dataset + '; loss: ' + loss + '; eps list: '+str(eps_list)) + '; Solver: '+learner.name
print('Starting experiment. ' + info)
rvals = [0.5, 1, 1.5, 2]
allResults = []
for r in rvals:
# Run the fair learning algorithm the supervised learning oracle
result = fair_train_test(dataset, n, eps_list, learner, r,
constraint=constraint, loss=loss,
random_seed=DATA_SPLIT_SEED)
# read_result_list([result]) # A simple print out for the experiment
print("Done for r = ", r)
# Saving the result list
outfile = open(info + str(r) +'.pkl','wb')
pickle.dump(result, outfile)
outfile.close()
"""
# Other sample use:
learner1 = solvers.SVM_LP_Learner(off_set=alpha)
result1 = fair_train_test(dataset, n, eps_list, learner1,
constraint=constraint, loss=loss,
random_seed=DATA_SPLIT_SEED)
learner2 = solvers.LeastSquaresLearner(Theta)
result2 = fair_train_test(dataset, n, eps_list, learner2,
constraint=constraint, loss=loss,
random_seed=DATA_SPLIT_SEED)
learner3 = solvers.RF_Regression_Learner(Theta)
result3 = fair_train_test(dataset, n, eps_list, learner3,
constraint=constraint, loss=loss,
random_seed=DATA_SPLIT_SEED)
learner4 = solvers.XGB_Classifier_Learner(Theta)
result4 = fair_train_test(dataset, n, eps_list, learner4,
constraint=constraint, loss=loss,
random_seed=DATA_SPLIT_SEED)
learner5 = solvers.LogisticRegressionLearner(Theta)
result5 = fair_train_test(dataset, n, eps_list, learner5,
constraint=constraint, loss=loss,
random_seed=DATA_SPLIT_SEED)
learner6 = solvers.XGB_Regression_Learner(Theta)
result6 = fair_train_test(dataset, n, eps_list, learner6,
constraint=constraint, loss=loss,
random_seed=DATA_SPLIT_SEED)
"""