-
Notifications
You must be signed in to change notification settings - Fork 1
/
qii.py
178 lines (129 loc) · 5.94 KB
/
qii.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
""" QII mesurement script
author: mostly Shayak
"""
from __future__ import print_function
import time
import pandas as pd
import numpy
import pickle
import numpy.linalg
import copy
from ml_util import split_and_train_classifier, split_data, get_arguments, \
Dataset, measure_analytics, \
plot_series_with_baseline, plot_series
import qii_lib
def __main__():
args = get_arguments()
qii_lib.record_counterfactuals = args.record_counterfactuals
# Read dataset
dataset = Dataset(args.dataset, sensitive=args.sensitive, target=args.target)
# Get column names
# f_columns = dataset.num_data.columns
# sup_ind = dataset.sup_ind
######### Begin Training Classifier ##########
split_dataset = split_data(args, dataset)
classifiers = args.classifier
for classifier in classifiers:
dat = split_and_train_classifier(classifier, args, split_dataset)
print('End Training Classifier: %s' % classifier)
######### End Training Classifier ##########
measure_analytics(dataset, dat.cls, dat.x_test, dat.y_test, dat.sens_test)
t_start = time.time()
measures = {'discrim': eval_discrim,
'average-unary-individual': eval_average_unary_individual,
'unary-individual': eval_unary_individual,
'banzhaf': eval_banzhaf,
'shapley': eval_shapley}
tmp_args = copy.deepcopy(args)
tmp_args.output_suffix = args.output_suffix + '_' + classifier
if args.measure in measures:
measures[args.measure](dataset, tmp_args, dat)
else:
raise ValueError("Unknown measure %s" % args.measure)
t_end = time.time()
print(t_end - t_start)
if args.batch_mode:
args_filename = 'processed_data/args_%s' % args.output_suffix
with open(args_filename, 'wb') as pickle_file:
pickle.dump(args, pickle_file, 0)
def eval_discrim(dataset, args, dat):
""" Discrimination metric """
baseline = qii_lib.discrim(numpy.array(dat.x_test), dat.cls, numpy.array(dat.sens_test))
discrim_inf = qii_lib.discrim_influence(dataset, dat.cls, dat.x_test, dat.sens_test)
discrim_inf_series = pd.Series(discrim_inf, index=discrim_inf.keys())
if args.show_plot:
plot_series_with_baseline(
discrim_inf_series, args,
'Feature', 'QII on Group Disparity',
baseline)
def eval_average_unary_individual(dataset, args, dat):
""" Unary QII averaged over all individuals. """
average_local_inf, _ = qii_lib.average_local_influence(
dataset, dat.cls, dat.x_test)
average_local_inf_series = pd.Series(average_local_inf,
index=average_local_inf.keys())
if args.show_plot or args.output_pdf:
plot_series(average_local_inf_series, args,
'Feature', 'QII on Outcomes')
def eval_unary_individual(dataset, args, dat):
""" Unary QII. """
x_individual = dat.scaler.transform(dataset.num_data.ix[args.individual].reshape(1, -1))
average_local_inf, _ = qii_lib.unary_individual_influence(
dataset, dat.cls, x_individual, dat.x_test)
average_local_inf_series = pd.Series(
average_local_inf, index=average_local_inf.keys())
if args.show_plot or args.output_pdf:
plot_series(average_local_inf_series, args,
'Feature', 'QII on Outcomes')
def eval_banzhaf(dataset, args, dat):
""" Banzhaf metric. """
x_individual = dat.scaler.transform(dataset.num_data.ix[args.individual])
banzhaf = qii_lib.banzhaf_influence(dataset, dat.cls, x_individual, dat.x_test)
banzhaf_series = pd.Series(banzhaf, index=banzhaf.keys())
if args.show_plot or args.output_pdf:
plot_series(banzhaf_series, args, 'Feature', 'QII on Outcomes (Banzhaf)')
def eval_shapley(dataset, args, dat):
""" Shapley metric. """
if (args.batch_mode):
eval_shapley_batch(dataset, args, dat)
return
row_individual = dataset.num_data.ix[args.individual].reshape(1, -1)
x_individual = dat.scaler.transform(row_individual)
shapley, _ = qii_lib.shapley_influence(dataset, dat.cls, x_individual, dat.x_test)
print(shapley)
shapley_series = pd.Series(shapley, index=shapley.keys())
if args.show_plot or args.output_pdf:
plot_series(shapley_series, args, 'Feature', 'QII on Outcomes (Shapley)')
def eval_shapley_batch(dataset, args, dat):
super_indices = list(dataset.sup_ind.keys())
rowsize = dataset.num_data.shape[0]
learning_samples = args.batch_mode_samples
shapley_saved = numpy.zeros((learning_samples, len(super_indices)))
x_samples = numpy.zeros(((learning_samples, dataset.num_data.shape[1])))
y_samples = numpy.zeros((learning_samples, 1))
time_last = time.time()
for i in range(0, learning_samples):
if i % 20 == 0:
time_new = time.time()
print('Index:', i)
print('Time:', time_new - time_last)
time_last = time_new
idx = i * (rowsize // learning_samples)
row_individual = dataset.num_data.ix[idx].reshape(1, -1)
x_individual = dat.scaler.transform(row_individual)
x_samples[i] = x_individual
shapley, _ = qii_lib.shapley_influence(dataset, dat.cls, x_individual, dat.x_test)
shapley_series = pd.Series(shapley, index=shapley.keys())
for j in range(0, len(super_indices)):
shapley_saved[i][j] = shapley_series[super_indices[j]]
y_samples[i] = dat.cls.predict(x_individual)
suffix = args.output_suffix
x_filename = getfilename("processed_data/", "x_samples", suffix)
qii_filename = getfilename("processed_data/", "qii_samples", suffix)
y_filename = getfilename("processed_data/", "y_samples", suffix)
numpy.save(x_filename, x_samples)
numpy.save(qii_filename, shapley_saved)
numpy.save(y_filename, y_samples)
def getfilename(prefix, name, suffix):
return prefix + name + suffix
__main__()