-
Notifications
You must be signed in to change notification settings - Fork 1
/
clf_xgboost_2_levels_cal.py
296 lines (247 loc) · 10.8 KB
/
clf_xgboost_2_levels_cal.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
"""
"""
import sys
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.base import BaseEstimator
from sklearn.calibration import CalibratedClassifierCV
from sklearn.cross_validation import StratifiedKFold
#sys.path.append('/home/sandrovegapons/anaconda/src/xgboost/wrapper')
sys.path.append('E:\Competitions\OttoGroup\py_ml_utils\lib')
from xgboost import DMatrix
from clf_xgboost import Clf_xgboost, my_train_xgboost
import pdb
class CalClf(BaseEstimator):
def __init__(self, param0, num_round0, w0, param1, num_round1, w1, param2,
num_round2, w2):
super(CalClf, self).__init__()
self.param0 = param0
self.num_round0 = num_round0
self.w0 = w0
self.param1 = param1
self.num_round1 = num_round1
self.w1 = w1
self.param2 = param2
self.num_round2 = num_round2
self.w2 = w2
def fit(X, y):
le = LabelEncoder()
id_123 = np.logical_or(np.logical_or(y_train==1, y_train==2),
y_train==3)
y0_train = np.zeros(len(y_train), dtype=np.int32)
y0_train[id_123] = 1
X0_train = np.copy(X_train)
y0_train = le.fit_transform(y0_train).astype(np.int32)
X1_train = X_train[id_123]
y1_train = y_train[id_123]
y1_train = le.fit_transform(y1_train).astype(np.int32)
X2_train = X_train[np.logical_not(id_123)]
y2_train = y_train[np.logical_not(id_123)]
y2_train = le.fit_transform(y2_train).astype(np.int32)
class Clf_xgboost_2_levels(Clf_xgboost):
"""
Simple xgboost based classifier.
"""
def __init__(self, num_classes=9):
"""
"""
self.prefix = 'xgb_2lv'
#Classifier 0
self.param0 = {}
self.param0['objective'] = 'multi:softprob'
# scale weight of positive examples
self.param0['eta'] = 0.015
self.param0['gamma'] = 0.5
self.param0['min_child_weight'] = 3.5
self.param0['max_delta_step'] = 0
self.param0['subsample'] = 0.3
self.param0['colsample_bytree'] = 0.3
self.param0['max_depth'] = 19
self.param0['silent'] = 1
self.param0['nthread'] = 7
self.param0['eval_metric'] = 'mlogloss'
self.param0['num_class'] = 2
self.num_round0 = 1200
self.w0 = [1., 1.03]
self.rt0_eta=1.00055
self.rt0_ssp=1.0007
self.rt0_clb=1.0007
self.rt0_dpt=0.998
#Classifier 1
self.param1 = {}
self.param1['objective'] = 'multi:softprob'
self.param1['eta'] = 0.01
self.param1['gamma'] = 0.7
self.param1['min_child_weight'] = 4
self.param1['subsample'] = 0.5
self.param1['max_depth'] = 17
self.param1['max_delta_step'] = 10
self.param1['colsample_bytree'] = 0.5
self.param1['silent'] = 1
self.param1['nthread'] = 7
self.param1['eval_metric'] = 'mlogloss'
self.param1['num_class'] = 3
self.num_round1 = 1800
self.w1 = [1., 1.17, 1.23]
self.rt1_eta=1.00009
self.rt1_ssp=1.0003
self.rt1_clb=1.0003
self.rt1_dpt=0.9996
#Classifier 2
self.param2 = {}
self.param2['objective'] = 'multi:softprob'
self.param2['eta'] = 0.015
self.param2['gamma'] = 0.7
self.param2['min_child_weight'] = 3
self.param2['subsample'] = 0.5
self.param2['max_depth'] = 13
self.param2['max_delta_step'] = 6
self.param2['colsample_bytree'] = 0.5
self.param2['silent'] = 1
self.param2['nthread'] = 7
self.param2['eval_metric'] = 'mlogloss'
self.param2['num_class'] = 6
self.num_round2 = 1100
self.w2 = [ 1.2, 1.2, 1., 1.2, 1.05, 1.1]
self.rt2_eta=1.00055
self.rt2_ssp=1.00055
self.rt2_clb=1.00055
self.rt2_dpt=0.9998
def train_validate(self, X_train, y_train, X_valid, y_valid):
"""
"""
#training
le = LabelEncoder()
id_123 = np.logical_or(np.logical_or(y_train==1, y_train==2),
y_train==3)
y0_train = np.zeros(len(y_train), dtype=np.int32)
y0_train[id_123] = 1
X0_train = np.copy(X_train)
y0_train = le.fit_transform(y0_train).astype(np.int32)
X1_train = X_train[id_123]
y1_train = y_train[id_123]
y1_train = le.fit_transform(y1_train).astype(np.int32)
X2_train = X_train[np.logical_not(id_123)]
y2_train = y_train[np.logical_not(id_123)]
y2_train = le.fit_transform(y2_train).astype(np.int32)
#Validation
id_123_valid = np.logical_or(np.logical_or(y_valid==1, y_valid==2),
y_valid==3)
y0_valid = np.zeros(len(y_valid), dtype=np.int32)
y0_valid[id_123_valid] = 1
X0_valid = np.copy(X_valid)
y0_valid = le.fit_transform(y0_valid).astype(np.int32)
X1_valid = X_valid[id_123_valid]
y1_valid = y_valid[id_123_valid]
y1_valid = le.fit_transform(y1_valid).astype(np.int32)
X2_valid = X_valid[np.logical_not(id_123_valid)]
y2_valid = y_valid[np.logical_not(id_123_valid)]
y2_valid = le.fit_transform(y2_valid).astype(np.int32)
xg_valid = DMatrix(X_valid)
#Classifier 0
w0_train = np.zeros(len(y0_train))
for i in range(len(w0_train)):
w0_train[i] = self.w0[int(y0_train[i])]
xg0_train = DMatrix(X0_train, label=y0_train, weight=w0_train)
xg0_valid = DMatrix(X0_valid, label=y0_valid)
watchlist0 = [(xg0_train,'train'), (xg0_valid, 'validation')]
bst0 = my_train_xgboost(self.param0, xg0_train, self.num_round0,
watchlist0, rt_eta=self.rt0_eta,
rt_ssp=self.rt0_ssp, rt_clb=self.rt0_clb,
rt_dpt=self.rt0_dpt)
y0_pred = bst0.predict(xg_valid).reshape(y_valid.shape[0], 2)
# pdb.set_trace()
#Classifier 1
w1_train = np.zeros(len(y1_train))
for i in range(len(w1_train)):
w1_train[i] = self.w1[int(y1_train[i])]
xg1_train = DMatrix(X1_train, label=y1_train, weight=w1_train)
xg1_valid = DMatrix(X1_valid, label=y1_valid)
watchlist1 = [(xg1_train,'train'), (xg1_valid, 'validation')]
bst1 = my_train_xgboost(self.param1, xg1_train, self.num_round1,
watchlist1, rt_eta=self.rt1_eta,
rt_ssp=self.rt1_ssp, rt_clb=self.rt1_clb,
rt_dpt=self.rt1_dpt)
y1_pred = bst1.predict(xg_valid).reshape(y_valid.shape[0], 3)
#Classifier 2
w2_train = np.zeros(len(y2_train))
for i in range(len(w2_train)):
w2_train[i] = self.w2[int(y2_train[i])]
xg2_train = DMatrix(X2_train, label=y2_train, weight=w2_train)
xg2_valid = DMatrix(X2_valid, label=y2_valid)
watchlist2 = [(xg2_train,'train'), (xg2_valid, 'validation')]
bst2 = my_train_xgboost(self.param2, xg2_train, self.num_round2,
watchlist2, rt_eta=self.rt2_eta,
rt_ssp=self.rt2_ssp, rt_clb=self.rt2_clb,
rt_dpt=self.rt2_dpt)
y2_pred = bst2.predict(xg_valid).reshape(y_valid.shape[0], 6)
y_pred = np.zeros((y0_pred.shape[0], 9))
y_pred[:,0] = y0_pred[:,0]*y2_pred[:,0]
y_pred[:,1] = y0_pred[:,1]*y1_pred[:,0]
y_pred[:,2] = y0_pred[:,1]*y1_pred[:,1]
y_pred[:,3] = y0_pred[:,1]*y1_pred[:,2]
y_pred[:,4] = y0_pred[:,0]*y2_pred[:,1]
y_pred[:,5] = y0_pred[:,0]*y2_pred[:,2]
y_pred[:,6] = y0_pred[:,0]*y2_pred[:,3]
y_pred[:,7] = y0_pred[:,0]*y2_pred[:,4]
y_pred[:,8] = y0_pred[:,0]*y2_pred[:,5]
return y_pred
def train_test(self, X, y, X_test):
"""
"""
#training
le = LabelEncoder()
id_123 = np.logical_or(np.logical_or(y==1, y==2),
y==3)
y0 = np.zeros(len(y), dtype=np.int32)
y0[id_123] = 1
X0 = np.copy(X)
y0 = le.fit_transform(y0).astype(np.int32)
X1 = X[id_123]
y1 = y[id_123]
y1 = le.fit_transform(y1).astype(np.int32)
X2 = X[np.logical_not(id_123)]
y2 = y[np.logical_not(id_123)]
y2 = le.fit_transform(y2).astype(np.int32)
xg_test = DMatrix(X_test)
#Classifier 0
w0_train = np.zeros(len(y0))
for i in range(len(w0_train)):
w0_train[i] = self.w0[int(y0[i])]
xg0_train = DMatrix(X0, label=y0, weight=w0_train)
bst0 = my_train_xgboost(self.param0, xg0_train, self.num_round0,
rt_eta=self.rt0_eta,
rt_ssp=self.rt0_ssp, rt_clb=self.rt0_clb,
rt_dpt=self.rt0_dpt)
y0_pred = bst0.predict(xg_test).reshape(X_test.shape[0], 2)
#Classifier 1
w1_train = np.zeros(len(y1))
for i in range(len(w1_train)):
w1_train[i] = self.w1[int(y1[i])]
xg1_train = DMatrix(X1, label=y1, weight=w1_train)
bst1 = my_train_xgboost(self.param1, xg1_train, self.num_round1,
rt_eta=self.rt1_eta,
rt_ssp=self.rt1_ssp, rt_clb=self.rt1_clb,
rt_dpt=self.rt1_dpt)
y1_pred = bst1.predict(xg_test).reshape(X_test.shape[0], 3)
#Classifier 2
w2_train = np.zeros(len(y2))
for i in range(len(w2_train)):
w2_train[i] = self.w2[int(y2[i])]
xg2_train = DMatrix(X2, label=y2, weight=w2_train)
bst2 = my_train_xgboost(self.param2, xg2_train, self.num_round2,
rt_eta=self.rt2_eta,
rt_ssp=self.rt2_ssp, rt_clb=self.rt2_clb,
rt_dpt=self.rt2_dpt)
y2_pred = bst2.predict(xg_test).reshape(X_test.shape[0], 6)
y_pred = np.zeros((y0_pred.shape[0], 9))
y_pred[:,0] = y0_pred[:,0]*y2_pred[:,0]
y_pred[:,1] = y0_pred[:,1]*y1_pred[:,0]
y_pred[:,2] = y0_pred[:,1]*y1_pred[:,1]
y_pred[:,3] = y0_pred[:,1]*y1_pred[:,2]
y_pred[:,4] = y0_pred[:,0]*y2_pred[:,1]
y_pred[:,5] = y0_pred[:,0]*y2_pred[:,2]
y_pred[:,6] = y0_pred[:,0]*y2_pred[:,3]
y_pred[:,7] = y0_pred[:,0]*y2_pred[:,4]
y_pred[:,8] = y0_pred[:,0]*y2_pred[:,5]
return y_pred