-
Notifications
You must be signed in to change notification settings - Fork 0
/
fair_maxmin_maxsum.py
77 lines (63 loc) · 2.16 KB
/
fair_maxmin_maxsum.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
import numpy as np
def create_dict(l): #create a dictionary for elements in l
idx = [i for i in range(0, len(l))]
zobj = zip(l, idx)
hashmap = dict(zobj)
return hashmap
def maxmin_fair(df, attrib, dist, flor=None, cel=None, sample=100):
cat_count = dict.fromkeys(flor.keys(), 0)
slack = sample - (sum(flor.values()))
subset = df[0:0]
count = 1
idx = 0
allpair_dist = dist
candidate = np.ones(df.shape[0])
first = np.random.choice(np.arange(0, df.shape[0]), 1)
dist = allpair_dist[first, :]
candidate[first] = 0
subset = subset.append(df.iloc[first, :])
while(count < sample):
#print(df.shape, idx)
idx = np.argmax(dist)
candidate[idx] = 0
dist = np.minimum(dist, allpair_dist[idx, :]) * candidate
nxt = df.iloc[idx, :]
c = nxt[attrib]
if(cat_count[c] < flor[c]):
subset = subset.append(nxt)
count += 1
cat_count[c] += 1
elif(slack > 0):
subset = subset.append(nxt)
count += 1
cat_count[c] += 1
slack -= 1
return subset
def maxsum_fair(df, attrib, dist, flor=None, cel=None, sample=100):
cat_count = dict.fromkeys(flor.keys(), 0)
slack = sample - (sum(flor.values()))
subset = df[0:0]
count = 1
idx = 0
allpair_dist = dist
candidate = np.ones(df.shape[0])
first = np.random.choice(np.arange(0, df.shape[0]), 1)
dist = allpair_dist[first, :]
candidate[first] = 0
subset = subset.append(df.iloc[first, :])
while(count < sample):
idx = np.argmax(dist)
candidate[idx] = 0
dist = (dist + allpair_dist[idx, :]) * candidate
nxt = df.iloc[idx, :]
c = nxt[attrib]
if(cat_count[c] < flor[c]):
subset = subset.append(nxt)
count += 1
cat_count[c] += 1
elif(slack > 0):
subset = subset.append(nxt)
count += 1
cat_count[c] += 1
slack -= 1
return subset