-
Notifications
You must be signed in to change notification settings - Fork 12
/
ensemble.py
419 lines (347 loc) · 14.9 KB
/
ensemble.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
"""
Airbnb New User Bookings Comptetition
https://www.kaggle.com/c/airbnb-recruiting-new-user-bookings
Author: Sandro Vega Pons ([email protected])
Ensemble techniques.
"""
import os
import numpy as np
import pickle
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import log_loss
from sklearn.base import BaseEstimator
from scipy.optimize import minimize
from xgboost.sklearn import XGBClassifier
from sklearn.preprocessing import LabelBinarizer
from letor_metrics import ndcg_score
from sklearn.cross_validation import StratifiedKFold
from sklearn.calibration import CalibratedClassifierCV
def get_X_X_Test(valid_folder='save/valid', test_folder='save/test'):
"""
Auxiliary function to compute the new X and X_test from the data
stored in the 'save/valid' and 'save/test' folders respectively.
"""
#Computing X from data in valid folder
lv_path = os.listdir(valid_folder)
lv_path.sort()
print len(lv_path)
list_valid = []
for p in lv_path:
arr = pickle.load(open(os.path.join(valid_folder, p), 'r'))
list_valid.append(arr)
X = np.hstack(list_valid)
#Computing X_test from data in test folder
lt_path = os.listdir(test_folder)
lt_path.sort()
list_test = []
for p in lt_path:
arr = pickle.load(open(os.path.join(test_folder, p), 'r'))
list_test.append(arr)
X_test = np.hstack(list_test)
#
n_preds = len(list_valid)
n_class = list_valid[0].shape[1]
return X, X_test, n_preds, n_class
##############################################
##Optimization based ensemblers
#############################################
def opt_1_obj_func(w, X, y, n_class):
"""
Function to be minimized in the EN_OPT_1 ensembler.
Parameters:
----------
w: ndarray size=(n_preds * n_class)
Candidate solution to the optimization problem (vector of weights).
X: ndarray size=(n_samples, n_preds * n_class)
Solutions to be combined horizontally concatenated.
y: ndarray size=(n_samples,)
Class labels
n_class: int
Number of classes in the problem, i.e. = 12
"""
#Constraining the weights for each class to sum 1.
#This constrain can be defined in the scipy.minimize function, but doing it here
#gives more flexibility to the scipy.minimize function (e.g. more solvers
#are allowed).
w_range = np.arange(len(w))%n_class
for i in range(n_class):
w[w_range==i] = w[w_range==i] / np.sum(w[w_range==i])
sol = np.zeros((X.shape[0], n_class))
for i in range(len(w)):
sol[:, i % n_class] += X[:, i] * w[i]
#The quantity to minimize is the log_loss.
sc_ll = log_loss(y, sol)
return sc_ll
class EN_OPT_1(BaseEstimator):
"""
Ensembler. There is a weight for each class for each prediction.
Given a set of candidate solutions x1, x2, ..., xn, where each xi has
m=12 predictions (one for each class), i.e. xi = xi1, xi2,...,xim. The
algorithms finds the optimal set of weights w1, w2, ..., w(n*m); such that
minimizes log_loss(y_true, x_ensemble), where x_ensemble = x11*w1 + x12*w2 +
...+ x21*w(m+1) +... xnm*w(n*m).
"""
def __init__(self, n_preds, n_class):
super(EN_OPT_1, self).__init__()
self.n_preds = n_preds
self.n_class = n_class
def fit(self, X, y):
"""
Learn the optimal weights by solving an optimization problem.
"""
x0 = np.ones(self.n_class * self.n_preds) / float(self.n_preds)
bounds = [(0,1)]*len(x0)
res = minimize(opt_1_obj_func, x0, args=(X, y, self.n_class),
method='L-BFGS-B',
bounds=bounds,
)
self.w = res.x
return self
def predict_proba(self, X):
"""
Use the weights learned in training to predict class probabilities.
"""
y_pred = np.zeros((X.shape[0], self.n_class))
for i in range(len(self.w)):
y_pred[:, i % self.n_class] += X[:, i] * self.w[i]
return y_pred
def apply_en_opt_1(y_valid, valid_folder='save/valid', test_folder='save/test'):
"""
Applies the EN_OPT_1 ensembler to the solutions stored in 'save/valid' and
'save/test'.
"""
#Loading data
X, X_test, n_preds, n_class = get_X_X_Test(valid_folder, test_folder)
y = y_valid
validation = False
if validation:
#This is just to test the results of the ensemble on validation data.
sss = StratifiedKFold(y, n_folds=5, random_state=0)
for id_train, id_valid in sss:
X_train, X_valid = X[id_train], X[id_valid]
y_train, y_valid = y[id_train], y[id_valid]
lb = LabelBinarizer()
lb.fit(y_train)
yb_valid = lb.transform(y_valid)
print 'Individual results'
for i in range(n_preds):
sing_pred = X_valid[:,i*n_class:(i+1)*n_class]
score_sing_pred= np.mean([ndcg_score(tr, pr, k=5) for tr, pr in zip(yb_valid.tolist(), sing_pred.tolist())])
print score_sing_pred, log_loss(y_valid, sing_pred)
print ' '
ec1 = EN_OPT_1(n_preds, n_class)
ec1.fit(X_train, y_train)
y_ec_pred = ec1.predict_proba(X_valid)
print np.round(ec1.w.reshape(n_preds, -1), 2)
sc_ec = np.mean([ndcg_score(tr, pr, k=5) for tr, pr in zip(yb_valid.tolist(), y_ec_pred.tolist())])
print 'Result of EC1'
print sc_ec, log_loss(y_valid, y_ec_pred)
print '------ \n'
else:
#Applying the EN_OPT_1 ensembler
ens = EN_OPT_1(n_preds, n_class)
ens.fit(X, y)
y_pred = ens.predict_proba(X_test)
return y_pred
def opt_2_obj_func(w, X, y, n_class):
"""
Function to be minimized in the EN_OPT_2 ensembler.
In this case there is only one weight for each classification restlt to be
combined.
Parameters:
----------
w: ndarray size=(n_preds)
Candidate solution to the optimization problem (vector of weights).
X: ndarray size=(n_samples, n_preds * n_class)
Solutions to be combined horizontally concatenated.
y: ndarray size=(n_samples,)
Class labels
n_class: int
Number of classes in the problem, i.e. = 12
"""
w = np.abs(w)
sol = np.zeros((X.shape[0], n_class))
for i in range(len(w)):
sol += X[:, i*n_class:(i+1)*n_class] * w[i]
#Minimizing the logloss
sc_ll = log_loss(y, sol)
return sc_ll
class EN_OPT_2(BaseEstimator):
"""
Ensembler. There is only one weight for each independent solution.
Given a set of candidate solutions x1, x2, ..., xn; it computes
the optimal set of weights w1, w2, ..., wn; such that minimizes
log_loss(y_true, x_ensemble), where x_ensemble = x1*w1 + x2*w2 +..., xn*wn.
"""
def __init__(self, n_preds, n_class):
super(EN_OPT_2, self).__init__()
self.n_preds = n_preds
self.n_class = n_class
def fit(self, X, y):
"""
Learn the optimal weights by solving an optimization problem.
"""
x0 = np.ones(self.n_preds) / float(self.n_preds)
bounds = [(0,1)]*len(x0)
cons = ({'type':'eq','fun':lambda w: 1-sum(w)})
res = minimize(opt_2_obj_func, x0, args=(X, y, self.n_class),
method='SLSQP',
bounds=bounds,
constraints=cons
)
self.w = res.x
return self
def predict_proba(self, X):
"""
Use the weights learned in training to predict class probabilities.
"""
y_pred = np.zeros((X.shape[0], self.n_class))
for i in range(len(self.w)):
y_pred += X[:, i*self.n_class:(i+1)*self.n_class] * self.w[i]
return y_pred
def apply_en_opt_2(y_valid, valid_folder='save/valid', test_folder='save/test'):
"""
Applies the EN_OPT_2 ensembler to the solutions stored in 'save/valid' and
'save/test'.
"""
#Loading data
X, X_test, n_preds, n_class = get_X_X_Test(valid_folder, test_folder)
y = y_valid
validation = False
if validation:
#This is just to test the results of the ensemble on validation data.
sss = StratifiedKFold(y, n_folds=5, random_state=0)
for id_train, id_valid in sss:
X_train, X_valid = X[id_train], X[id_valid]
y_train, y_valid = y[id_train], y[id_valid]
lb = LabelBinarizer()
lb.fit(y_train)
yb_valid = lb.transform(y_valid)
print 'Individual results'
for i in range(n_preds):
sing_pred = X_valid[:,i*n_class:(i+1)*n_class]
score_sing_pred= np.mean([ndcg_score(tr, pr, k=5) for tr, pr in zip(yb_valid.tolist(), sing_pred.tolist())])
print score_sing_pred, log_loss(y_valid, sing_pred)
print ' '
ec2 = EN_OPT_2(n_preds, n_class)
ec2.fit(X_train, y_train)
y_ec_pred = ec2.predict_proba(X_valid)
print np.round(ec2.w.reshape(n_preds, -1), 2)
sc_ec = np.mean([ndcg_score(tr, pr, k=5) for tr, pr in zip(yb_valid.tolist(), y_ec_pred.tolist())])
print 'Result of EC1'
print sc_ec, log_loss(y_valid, y_ec_pred)
print '------ \n'
else:
#Applying the EN_OPT_2 ensembler
ens = EN_OPT_2(n_preds, n_class)
ens.fit(X, y)
y_pred = ens.predict_proba(X_test)
return y_pred
def apply_mix_opt(y_valid, valid_folder='save/valid', test_folder='save/test'):
"""
Ensembler that is a combination of EN_OPT_1 and EN_OPT_2 + its calibrated
versions. If
en1 => prediction of EN_OPT_1
cal_en1 => prediction of calibrated EN_OPT_1
en2 => prediction of EN_OPT_2
cal_en2 => prediction of EN_OPT_2
then, the prediction is ((en1*2 + cal_en1)/3 + ((en2*2 + cal_en2)/3)*2)/3
"""
#Loading data
X, X_test, n_preds, n_class = get_X_X_Test(valid_folder, test_folder)
y = y_valid
validation = False
if validation:
#This is just to test the results of the ensemble on validation data.
sss = StratifiedKFold(y, n_folds=5, random_state=0)
for id_train, id_valid in sss:
X_train, X_valid = X[id_train], X[id_valid]
y_train, y_valid = y[id_train], y[id_valid]
lb = LabelBinarizer()
lb.fit(y_train)
yb_valid = lb.transform(y_valid)
print 'Individual results'
for i in range(n_preds):
sing_pred = X_valid[:,i*n_class:(i+1)*n_class]
score_sing_pred= np.mean([ndcg_score(tr, pr, k=5) for tr, pr in zip(yb_valid.tolist(), sing_pred.tolist())])
print score_sing_pred, log_loss(y_valid, sing_pred)
print ' '
ec1 = EN_OPT_1(n_preds, n_class)
ec1.fit(X_train, y_train)
y_ec1_pred = ec1.predict_proba(X_valid)
cc_ec1 = CalibratedClassifierCV(base_estimator=ec1, method='isotonic', cv=3)
cc_ec1.fit(X_train, y_train)
y_cc1_pred = cc_ec1.predict_proba(X_valid)
y_E1_pred = (y_ec1_pred*2 + y_cc1_pred) / 3.
ec2 = EN_OPT_2(n_preds, n_class)
ec2.fit(X_train, y_train)
y_ec2_pred = ec2.predict_proba(X_valid)
cc_ec2 = CalibratedClassifierCV(base_estimator=ec2, method='isotonic', cv=3)
cc_ec2.fit(X_train, y_train)
y_cc2_pred = cc_ec2.predict_proba(X_valid)
y_E2_pred = (y_ec2_pred*2 + y_cc2_pred) / 3.
y_ec_v1_pred = (y_E1_pred + y_E2_pred) / 2.
y_ec_v2_pred = (y_E1_pred*2 + y_E2_pred) / 3.
y_ec_v3_pred = (y_E1_pred + y_E2_pred*2) / 3.
sc_ec_v1 = np.mean([ndcg_score(tr, pr, k=5) for tr, pr in zip(yb_valid.tolist(), y_ec_v1_pred.tolist())])
sc_ec_v2 = np.mean([ndcg_score(tr, pr, k=5) for tr, pr in zip(yb_valid.tolist(), y_ec_v2_pred.tolist())])
sc_ec_v3 = np.mean([ndcg_score(tr, pr, k=5) for tr, pr in zip(yb_valid.tolist(), y_ec_v3_pred.tolist())])
print 'Results'
print sc_ec_v1, log_loss(y_valid, y_ec_v1_pred)
print sc_ec_v2, log_loss(y_valid, y_ec_v2_pred)
print sc_ec_v3, log_loss(y_valid, y_ec_v3_pred)
print '------ \n'
else:
#Applying the ensembler...
ec1 = EN_OPT_1(n_preds, n_class)
ec1.fit(X, y)
y_ec1_pred = ec1.predict_proba(X_test)
print '.'
cc_ec1 = CalibratedClassifierCV(base_estimator=ec1, method='isotonic', cv=3)
cc_ec1.fit(X, y)
y_cc1_pred = cc_ec1.predict_proba(X_test)
y_E1_pred = (y_ec1_pred*2 + y_cc1_pred) / 3.
print '..'
ec2 = EN_OPT_2(n_preds, n_class)
ec2.fit(X, y)
y_ec2_pred = ec2.predict_proba(X_test)
print '...'
cc_ec2 = CalibratedClassifierCV(base_estimator=ec2, method='isotonic', cv=3)
cc_ec2.fit(X, y)
y_cc2_pred = cc_ec2.predict_proba(X_test)
y_E2_pred = (y_ec2_pred*2 + y_cc2_pred) / 3.
print '....'
y_pred = (y_E1_pred + y_E2_pred*2) / 3.
return y_pred
##################################
##Classification based ensemblers
#################################
#They produced much poorer results than the optimization based ensemblers.
def apply_log_reg_ens(y_valid, valid_folder='save/valid', test_folder='save/test'):
"""
Ensembler based on logistic regression.
"""
#Loading data
X, X_test, n_preds, n_class = get_X_X_Test(valid_folder, test_folder)
y = y_valid
#Defining classifier
lr = LogisticRegression(penalty='l2', C=0.01, multi_class='ovr',
max_iter=300, solver='lbfgs',n_jobs=-1)
lr.fit(X, y)
y_pred = lr.predict_proba(X_test)
return y_pred
def apply_xgb_ens(y_valid, valid_folder='Valid', test_folder='Test'):
"""
Ensembler based on xgboost Gradient boosting.
"""
#Loading data
X, X_test, n_preds, n_class = get_X_X_Test(valid_folder, test_folder)
y = y_valid
#Defining classifier
xgb = XGBClassifier(max_depth=4, learning_rate=0.05, n_estimators=200,
objective='multi:softprob', gamma=0.,
max_delta_step=0., subsample=0.9, colsample_bytree=0.9,
seed=0)
xgb.fit(X, y)
y_pred = xgb.predict_proba(X_test)
return y_pred