-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigmoid_kernel_trick.py
325 lines (253 loc) · 12.3 KB
/
sigmoid_kernel_trick.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
# Logistic Regression - Classes 0, 1
from __future__ import division
import numpy as np
import pandas as pd
import math
from scipy.special import xlogy
from scipy.optimize import fmin_tnc, fmin, fmin_powell, fsolve, minimize, minimize_scalar, fmin_cobyla, fmin_slsqp
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_squared_error
from sklearn.metrics import precision_recall_fscore_support
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn.preprocessing import OneHotEncoder
from sklearn.gaussian_process.kernels import RBF, DotProduct
from MLE_Calculator import MLE_Calculator
class LogisticModel:
"""
Logistic model:
"""
def __init__(self, X, Y, fair_column):
self.X = X
self.Y = Y
self.theta = np.zeros(train_dataset.shape[1]) #np.random.random_sample(train_dataset.shape[1])
self.loss = 10000000000
self.fair_column = fair_column
def sigmoid(self, x):
# Activation function used to map any real value between 0 and 1
sigm = 1 / (1 + np.exp(-x))
return sigm
def logit(self, x):
# Activation function used to map any real value between 0 and 1
logit_val = np.log(x/(1-x))
return self.sigmoid(logit_val)
def net_input(self, theta, x):
# Computes the weighted sum of inputs
prod = np.dot(x, theta)
return prod
def probability(self, theta, x):
# Returns the probability after passing through sigmoid
pred = self.sigmoid(self.net_input(theta, x))
return pred
# Skip log(0) for stability
def logs(self, proba):
return xlogy(np.sign(proba), proba)
# Cost Function of basic Logistic Regression
def log_cost_function(self, theta, x, y):
# Computes the cost function for all the training samples
m = x.shape[0]
proba = self.probability(theta, x)
total_cost = -(1 / m) * np.sum(y * self.logs(proba) + (1 - y) * self.logs(1-proba))
return total_cost
def gradient(self, theta, x, y):
# Computes the gradient of the cost function at the point theta
m = x.shape[0]
return (1 / m) * np.dot(x.T, self.sigmoid(self.net_input(theta, x)) - y)
def hard_class_predict(self, soft_pred, labels=(1,2)):
y_pred = [labels[0] if i <= 0.5 else labels[1] for i in soft_pred]
return y_pred
def constraint_1(self, theta, x, y):
# Implementation of constraint -1
y_pred = self.hard_class_predict(self.probability(theta, x))
const_val = MLE_Calculator(y + 1, y_pred, self.fair_column).constraint_1_fair1()
return const_val
def constraint_2(self, theta, x, y):
# Implementation of constraint -1
y_pred = self.hard_class_predict(self.probability(theta, x))
const_val = MLE_Calculator(y + 1, y_pred, self.fair_column).constraint_2_fair2()
return const_val
def constraint_3(self, theta, x, y):
# Implementation of constraint -1
y_pred = self.hard_class_predict(self.probability(theta, x))
const_val = MLE_Calculator(y + 1, y_pred, self.fair_column).constraint_3_fair3()
return const_val
def constraint_4(self, theta, x, y):
# Implementation of constraint -1
y_pred = self.hard_class_predict(self.probability(theta, x))
const_val = MLE_Calculator(y + 1, y_pred, self.fair_column).constraint_4_fair4()
return const_val
def constraint_5(self, theta, x, y):
# Implementation of constraint -1
y_pred = self.hard_class_predict(self.probability(theta, x))
const_val = MLE_Calculator(y + 1, y_pred, self.fair_column).constraint_5_fair5()
return const_val
def constraint_6(self, theta, x, y):
# Implementation of constraint -1
y_pred = self.hard_class_predict(self.probability(theta, x))
const_val = MLE_Calculator(y + 1, y_pred, self.fair_column).constraint_6_fair6()
return const_val
def get_required_indexes(self,soft_pred, sensitive_feat, y):
X_11 = []
y_11 = []
X_12 = []
y_12 = []
X_21 = []
y_21 = []
X_22 = []
y_22 = []
for i, item in enumerate(sensitive_feat):
if item[0] == 1 and item[1] == 1:
X_11.extend([soft_pred[i]])
y_11.extend([y[i]])
elif item[0] == 1 and item[1] == 2:
X_12.extend([soft_pred[i]])
y_12.extend([y[i]])
elif item[0] == 2 and item[1] == 1:
X_21.extend([soft_pred[i]])
y_21.extend([y[i]])
elif item[0] == 2 and item[1] == 2:
X_22.extend([soft_pred[i]])
y_22.extend([y[i]])
return X_11 , X_12, X_21, X_22, y_11, y_12, y_21, y_22
def combine_logistic_loss(self, current_loss, y):
# total_cost = -(1 / m) * np.sum(y * self.logs(proba) + (1 - y) * self.logs(1-proba))
minus_y = [1 - a for a in y]
minus_prob = [1 - a for a in current_loss]
mul_post = np.multiply(y, self.logs(current_loss))
mul_negt = np.multiply(minus_y, self.logs(minus_prob))
total_cost = -(1 / len(current_loss)) * np.sum(mul_post + mul_negt)
return total_cost
def compute_proba_loss(self, soft_pred, y, fair_column):
sensitive_feat = np.concatenate((y.reshape(len(y),1), fair_column.reshape(len(fair_column),1)), axis=1)
X_11, X_12, X_21, X_22, y_11, y_12, y_21, y_22 = self.get_required_indexes(soft_pred, sensitive_feat, y-1)
if(1):
true_11 = np.zeros((1, len(X_11)))[0]
true_12 = np.zeros((1, len(X_12)))[0]
true_21 = np.ones((1, len(X_21)))[0]
true_22 = np.ones((1, len(X_22)))[0]
# Compute loss
loss_211 = np.square(true_11 - X_11).mean()
loss_212 = np.square(true_12 - X_12).mean()
loss_121 = np.square(true_21 - X_21).mean()
loss_122 = np.square(true_22 - X_22).mean()
else:
# Compute loss
loss_211 = self.combine_logistic_loss(X_11, y_11)
loss_212 = self.combine_logistic_loss(X_12, y_12)
loss_121 = self.combine_logistic_loss(X_21, y_21)
loss_122 = self.combine_logistic_loss(X_22, y_22)
return loss_211, loss_212, loss_121, loss_122
def cost_function(self, theta, x, y):
# Computes the cost function for all the training samples
self.theta = theta
# For Our Loss fn..,
y_pred = self.hard_class_predict(self.probability(theta, x))
#Find Proba Loss for Class 1 and 2
loss_211, loss_212, loss_121, loss_122 = self.compute_proba_loss(self.probability(theta, x), y+1, self.fair_column)
loss, target_loss = MLE_Calculator(y + 1, y_pred, self.fair_column, loss_211, loss_212, loss_121, loss_122).calculate_loss()
self.loss = loss
print ("Target Loss - " + str(target_loss) + " and Current Loss - " + str(loss))
return loss
#
# def gradient_function(self, theta, x, y):
# # Computes the Gradient for all the training samples
# self.theta = new_theta = theta
# y_pred = self.hard_class_predict(self.probability(theta, x))
# grad = MLE_Calculator(y + 1, y_pred, self.fair_column).calculate_grad()
#
# new_theta -= grad * 0.01
# return new_theta
def fit(self):
x, y, theta = self.X, self.Y, self.theta
# Constraint Def:
# constraint_1_dict = {"type": "eq", "fun": self.constraint_1, "args": (x,y)}
# constraint_2_dict = {"type": "eq", "fun": self.constraint_2, "args": (x,y)}
# opt_weights = minimize(fun=se lf.log_cost_function, x0=theta, args=(x,y),
# method="SLSQP",
# constraints=[constraint_2_dict,constraint_1_dict])
# opt_weights = fmin_tnc(func=self.log_cost_function, x0=theta,
# fprime=self.gradient, args=(x, y.flatten()))[0]
opt_weights = fmin_slsqp(func=self.cost_function, x0=theta, args=(x,y))#, eqcons=[self.constraint_1, self.constraint_2,])
return opt_weights
def predict(self, x, parameters):
theta = parameters[:, np.newaxis]
return self.probability(theta, x)
# Add Intercept to Log Model via Dataset
fit_intercept = True
select_feature= True
encode_feature= True
normalize = True
kernal_trick = False
def add_intercept(X):
intercept = np.ones((X.shape[0], 1))
return np.concatenate((X,intercept), axis=1)
#Train Prep - Label & Features split
train_dataset = pd.read_csv("dataset/train.csv", header=None).values
train_label = (train_dataset[:,-1]-1)
train_dataset = train_dataset[:,:-1]
#Fairness Required Column - X_13 - BUFFER
fair_column = train_dataset[:,12].reshape(train_dataset.shape[0],1)
# train_dataset = np.delete(train_dataset, 12, 1)
# test_dataset = np.delete(test_dataset, 12, 1)
# ************************************ Feature Engineer ************************************
if normalize:
# Normalize numerical cols
cont_columns = StandardScaler().fit_transform(train_dataset[:,:6])
else:
cont_columns = train_dataset[:,:6]
if encode_feature:
# # One Hot Encode categorical cols
cat_columns = OneHotEncoder(handle_unknown='ignore').fit_transform(train_dataset[:,6:]).toarray()
else:
cat_columns = train_dataset[:,6:]
train_dataset = np.concatenate((cont_columns, cat_columns), axis=1)
if fit_intercept:
# Add Bias Term/Intercept to Sigmoid function
train_dataset = add_intercept(train_dataset)
# ADD FairColumn Temprarily - For Dataset Split
train_dataset = np.concatenate((train_dataset, fair_column), axis=1)
# Train-Test Split
train_dataset, test_dataset, train_label, test_label = train_test_split(train_dataset, train_label, test_size=0.3)
#Final Fair Column - For Train (Dirty Work - Remove Temp Fair Column)
fair_column_train = train_dataset[:,-1]
fair_column_test = test_dataset[:,-1]
train_dataset = train_dataset[:,:-1]
test_dataset = test_dataset[:,:-1]
#################### ---------- KERNEL Trick After Split ---------- ####################
if kernal_trick:
# Train Dataset Kernalized
train_dataset = RBF().__call__(train_dataset)
# Test Dataset Kernalized
test_dataset = RBF().__call__(test_dataset)
if select_feature:
# ************************************ Feature Selection ************************************
clf = ExtraTreesClassifier(n_estimators=10)
clf = clf.fit(train_dataset, train_label)
train_dataset = SelectFromModel(clf, prefit=True).transform(train_dataset)
test_dataset = SelectFromModel(clf, prefit=True).transform(test_dataset)
print ("Post Feature Selection, Train Dataset size - " + str(train_dataset.shape))
print ("Post Feature Selection, Test Dataset size - " + str(test_dataset.shape))
# -1 in all datasets to remove the column of ->
log_model = LogisticModel(X=train_dataset, Y=train_label, fair_column=fair_column_train)
optimal_params = log_model.fit()
print ("------------------Confusion Matrix------------------")
final_labels = log_model.hard_class_predict(log_model.predict(x=test_dataset, parameters=optimal_params), labels=(0,1))
score = precision_recall_fscore_support(test_label, final_labels, average='macro')
print (confusion_matrix(test_label, final_labels))
print ("Precision - " + str(score[0]))
print ("Recall - " + str(score[1]))
print ("F1 Score - " + str(score[2]))
print ("------------******* Fairness Constraint ********---------------")
hard_labels = log_model.hard_class_predict(log_model.predict(x=test_dataset, parameters=optimal_params))
print ("Constraint 1 : " + str(MLE_Calculator(test_label + 1, hard_labels , fair_column_test).constraint_1_fair1()))
print ("Constraint 2 : " + str(MLE_Calculator(test_label + 1, hard_labels , fair_column_test).constraint_2_fair2()))
print ("------------------ Performance Measures ------------------")
loss, target_loss = MLE_Calculator(test_label + 1, hard_labels, fair_column_test).calculate_loss()
print ("Accuracy of Model - " + str(accuracy_score(test_label, final_labels)))
print ("Assignment Loss Value - " + str(target_loss))
print ("Weights Shape : " + str(optimal_params.shape))