-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
265 lines (201 loc) · 7.44 KB
/
analysis.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 1 21:30:27 2020
@author: lavanyasingh
"""
# In[]:
import csv
import pandas as pd
lines = []
with open("hirevue.csv", "r") as fi:
reader = csv.reader(fi)
headers = next(reader)[1:]
for line in reader:
lines.append(line[1:])
res = pd.DataFrame(lines, columns=headers)
print(res.head(5))
# In[]:
nnb = []
twonb = []
with open("results.csv", "r") as fi:
reader = csv.reader(fi)
print(next(reader))
for line in reader:
nnb.append(line[2])
twonb.append(line[3])
res["nnb"] = nnb
res["twonb"] = twonb
print(res.head(5))
res["race"] = res["race"].apply(pd.to_numeric)
res_old = res.copy()
print(res_old.race.unique())
res["race"] = [3 if i == 4 or i == 5 else i for i in res["race"]]
res["race"] = [i-2 if i > 3 else i for i in res["race"]]
print(res.race.unique())
# In[]:
from sklearn.metrics import confusion_matrix
models = ['basic', 'multirace', 'hirevue', 'nnb', 'twonb']
acc = pd.DataFrame(models, columns=['model'])
ppv = []
npv = []
for model in models:
conf = confusion_matrix(res["income"], res[model])
ppv.append((conf[0][1])/(conf[1][1]+conf[0][1]))
npv.append(conf[1][0]/(conf[1][0]+conf[0][0]))
acc["ppv"] = ppv
acc["npv"] = npv
print(acc)
# In[]:
fp = []
fn = []
accuracy = []
for model in models:
pos = res[(res["income"] == '0') & (res[model] == '1')]
fp.append(pos.shape[0]/res.shape[0])
neg = res[(res["income"] == '1') & (res[model] == '0')]
fn.append(neg.shape[0]/res.shape[0])
diffs = res[res["income"] == res[model]]
accuracy.append(diffs.shape[0]/res.shape[0])
acc["accuracy"] = accuracy
print(acc)
# In[]:
races_ppv = [[] for i in res["race"].unique()]
races_npv = [[] for i in res["race"].unique()]
races_acc = [[] for i in res["race"].unique()]
for model in models:
for race in res["race"].unique():
data = res[res["race"] == race]
conf = confusion_matrix(data["income"], data[model])
races_ppv[int(race) - 1].append((conf[0][1])/(conf[1][1]+conf[0][1]))
races_npv[int(race) - 1].append(conf[1][0]/(conf[1][0]+conf[0][0]))
diffs = data[data["income"] == data[model]]
races_acc[int(race)-1].append(diffs.shape[0]/data.shape[0])
for race in res["race"].unique():
acc["ppv_"+str(race)] = races_ppv[race-1]
acc["npv_"+str(race)] = races_npv[race-1]
acc["accuracy_"+str(race)] = races_acc[race-1]
print(acc)
# In[]:
nonwhite_ppv = []
nonwhite_npv = []
nonwhite_acc = []
for model in models:
data = res[res["race"] != '1']
conf = confusion_matrix(data["income"], data[model])
nonwhite_ppv.append((conf[0][1])/(conf[1][1]+conf[0][1]))
nonwhite_npv.append(conf[1][0]/(conf[1][0]+conf[0][0]))
diffs = data[data["income"] == data[model]]
nonwhite_acc.append(diffs.shape[0]/data.shape[0])
acc["nonwhite_ppv"] = nonwhite_ppv
acc["nonwhite_npv"] = nonwhite_npv
acc["nonwhite_accuracy"] = nonwhite_acc
print(acc)
# In[]:
# differential ppv
race_map = {"1":"white", "2":"black", "3":"indigenous", "4":"asian", "5":"pacific islander", "6":"other", "7":"mixed"}
tograph = acc[["ppv_"+str(i) for i in res.race.unique()] + ["model"]]
tograph = tograph.rename(columns={"ppv_"+str(key):value for key,value in race_map.items()})
tograph = tograph.drop(0)
tograph.at[1, "model"] = "basic"
ax = tograph.set_index("model").plot.bar()
ax.get_legend().remove()
ax.set_ylabel("ppv")
# In[]:
# differential npv
tograph = acc[["npv_"+str(i) for i in res.race.unique()] + ["model"]]
tograph = tograph.rename(columns={"npv_"+str(key):value for key,value in race_map.items()})
tograph = tograph.drop(0)
tograph.at[1, "model"] = "basic"
ax = tograph.set_index("model").plot.bar()
ax.get_legend().remove()
ax.set_ylabel("npv")
# In[]:
# differential accuracy
tograph = acc[["accuracy_"+str(i) for i in res.race.unique()] + ["model"]]
tograph = tograph.rename(columns={"accuracy_"+str(key):value for key,value in race_map.items()})
tograph = tograph.drop(0)
tograph.at[1, "model"] = "basic"
ax = tograph.set_index("model").plot.bar()
ax.legend(bbox_to_anchor=(1.1, 1.05))
ax.set_ylabel("accuracy")
# In[]:
# accuracy for nonwhite, white, and overall
tograph = acc[["nonwhite_accuracy", "accuracy_1", "accuracy", "model"]]
tograph = tograph.rename(columns={"nonwhite_accuracy":"nonwhite", "accuracy_1":"white", "accuracy":"overall"})
tograph = tograph.drop(0)
tograph.at[1, "model"] = "basic"
ax = tograph.set_index("model").plot.bar()
ax.legend(bbox_to_anchor=(1.1, 1.05))
ax.set_ylabel("accuracy")
# In[]:
# ppv for nonwhite, white, and overall
tograph = acc[["nonwhite_ppv", "ppv_1", "ppv", "model"]]
tograph = tograph.rename(columns={"nonwhite_ppv":"nonwhite", "ppv_1":"white", "ppv":"overall"})
tograph = tograph.drop(0)
tograph.at[1, "model"] = "basic"
ax = tograph.set_index("model").plot.bar()
ax.get_legend().remove()
ax.set_ylabel("ppv")
# In[]:
# npv for nonwhite, white, and overall
tograph = acc[["nonwhite_npv", "npv_1", "npv", "model"]]
tograph = tograph.rename(columns={"nonwhite_npv":"nonwhite", "npv_1":"white", "npv":"overall"})
tograph = tograph.drop(0)
tograph.at[1, "model"] = "basic"
ax = tograph.set_index("model").plot.bar()
ax.get_legend().remove()
ax.set_ylabel("npv")
# In[]:
from scipy.stats import levene
# brown forsythe to see if variance in diff accuracy across models is significantly diff
for model in models[2:]:
accuracies = []
basic = []
for race in res["race"].unique():
accuracies.append(acc[acc["model"] == model]["accuracy_"+str(race)].tolist()[0])
basic.append(acc[acc["model"] == "multirace"]["accuracy_"+str(race)].tolist()[0])
print(model, levene(accuracies, basic, center="median"))
# same for nnb versus hirevue
nnb_acc = []
hirevue_acc = []
for race in res["race"].unique():
nnb_acc.append(acc[acc["model"] == "nnb"]["accuracy_"+str(race)].tolist()[0])
hirevue_acc.append(acc[acc["model"] == "hirevue"]["accuracy_"+str(race)].tolist()[0])
print(levene(nnb_acc, hirevue_acc, center="median"))
# In[]:
# same for ppv
for model in models[2:]:
ppv = []
basic = []
for race in res["race"].unique():
ppv.append(acc[acc["model"] == model]["ppv_"+str(race)].tolist()[0])
basic.append(acc[acc["model"] == "multirace"]["ppv_"+str(race)].tolist()[0])
print(model, levene(accuracies, basic, center="median"))
# In[]:
# and npv
for model in models[2:]:
npv = []
basic = []
for race in res["race"].unique():
npv.append(acc[acc["model"] == model]["npv_"+str(race)].tolist()[0])
basic.append(acc[acc["model"] == "multirace"]["npv_"+str(race)].tolist()[0])
print(model, levene(accuracies, basic, center="median"))
# In[]:
from statistics import variance
# table of variances in npv, accuracy, and ppv
npv_var = []
ppv_var = []
accuracy_var = []
for model in models[1:]:
temp1 = variance(acc[acc["model"] == model]["ppv_"+str(race)].tolist()[0] for race in res["race"].unique())
ppv_var.append(temp1)
temp1 = variance(acc[acc["model"] == model]["npv_"+str(race)].tolist()[0] for race in res["race"].unique())
npv_var.append(temp1)
temp1 = variance(acc[acc["model"] == model]["accuracy_"+str(race)].tolist()[0] for race in res["race"].unique())
accuracy_var.append(temp1)
variance = pd.DataFrame([npv_var, ppv_var, accuracy_var], columns=models[1:])
variance["measure"] = ["npv", "ppv", "accuracy"]
variance = variance[["measure"]+variance.columns.tolist()[:-1]]
print(variance.head())
variance.to_html('variance.html')