-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel1_catb.py
298 lines (251 loc) · 9.12 KB
/
model1_catb.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 26 17:59:02 2017
@author: ldong
"""
import numpy as np
import cPickle as pk
import pandas as pd
from catboost import CatBoostRegressor
import matplotlib.pyplot as plt
import datetime as dt
from sklearn.metrics import mean_absolute_error
from data_alloc import *
plt.rcParams['figure.figsize'] = 7, 15
class sklMAE(object):
def get_final_error(self, error, weight):
return error / (weight + 1e-38)
def is_max_optimal(self):
return True
def evaluate(self, approxes, target, weight):
# approxes is list of indexed containers (containers with only __len__ and __getitem__ defined), one container
# per approx dimension. Each container contains floats.
# weight is one dimensional indexed container.
# target is float.
# weight parameter can be None.
# Returns pair (error, weights sum)
assert len(approxes) == 1
assert len(target) == len(approxes[0])
approx = approxes[0]
error_sum = 0.0
weight_sum = 0.0
for i in xrange(len(approx)):
w = 1.0 if weight is None else weight[i]
weight_sum += w
error_sum += w * np.abs(target[i] - approx[i] )
return error_sum, weight_sum
def data_categ(x):
categ = list(x.filter(regex='categ'))
# categ.extend(['date','age'])
categ.extend(list(x.filter(regex='num')))
categ.extend(list(x.filter(regex='flag')))
cat_feature = np.squeeze(np.array(np.where(np.in1d(list(x),categ))))
# for i in xrange(len(categ)):
# x.loc[:,categ[i]] = x.loc[:,categ[i]].astype('category')
return cat_feature
def model_run(tr_x, tr_y, tr_x17, tr_y17, quarter, model, pred17=False):
tr_x, tr_y, valid_x, valid_y, pid = data_quarter(tr_x, tr_y, tr_x17, tr_y17, quarter)
cat_f = data_categ(tr_x)
tr_y = np.squeeze(tr_y.as_matrix())
valid_y = np.squeeze(valid_y.as_matrix())
model.fit(tr_x, tr_y,cat_features=cat_f, eval_set=[valid_x,valid_y])
# lgb.plot_importance(bst)
pred = model.predict(valid_x)
print('valid mae score: {}'.format(mean_absolute_error(valid_y, pred)))
if pred17:
te_x17 = all_x # all_x is all_x_17 in this case!!!
te_x17 = te_x17.rename(columns={'parcelid':'date'})
te_x1 = te_x17
te_x1.loc[:,'date'] = 7
pred = model.predict(te_x1, thread_count=8)
return pred
pid = pid.to_frame().assign(f_catb=pred)
return pid
def model_pred(tr_x, tr_y, te_x, tr_x17, tr_y17, te_x17, quarter, model):
tr_x, tr_y, _, _, pid = data_quarter(tr_x, tr_y, tr_x17, tr_y17, quarter, nextQ=False)
if quarter == 3:
te_x = te_x.rename(columns={'parcelid':'date'})
te_x1 = te_x
te_x1.loc[:,'date'] = 10
te_x2 = te_x
te_x2.loc[:,'date'] = 11
te_x3 = te_x
te_x3.loc[:,'date'] = 12
cat_f = data_categ(tr_x)
tr_y = np.squeeze(tr_y.as_matrix())
model.fit(tr_x, tr_y,cat_features=cat_f)
pred1 = model.predict(te_x1, thread_count=8)
pred2 = model.predict(te_x2, thread_count=8)
pred3 = model.predict(te_x3, thread_count=8)
elif quarter == 7:
te_x17 = te_x17.rename(columns={'parcelid':'date'})
te_x1 = te_x17
te_x1.loc[:,'date'] = 22
te_x2 = te_x17
te_x2.loc[:,'date'] = 23
te_x3 = te_x17
te_x3.loc[:,'date'] = 24
cat_f = data_categ(tr_x)
tr_y = np.squeeze(tr_y.as_matrix())
model.fit(tr_x, tr_y,cat_features=cat_f)
pred1 = model.predict(te_x1, thread_count=8)
pred2 = model.predict(te_x2, thread_count=8)
pred3 = model.predict(te_x3, thread_count=8)
pred_train = model.predict(tr_x)
print('train mae score: {}'.format(mean_absolute_error(tr_y, pred_train)))
pid1 = pid.to_frame().assign(f_catb=pred1)
pid2 = pid.to_frame().assign(f_catb=pred2)
pid3 = pid.to_frame().assign(f_catb=pred3)
return pid1, pid2, pid3
# year 2016
#%%
model = CatBoostRegressor(
iterations=288, learning_rate=0.01, rsm=1,
depth=6, l2_leaf_reg=6, bagging_temperature=1,
loss_function='MAE',
eval_metric='MAE',
random_seed=0,
thread_count=8,
use_best_model=True,
od_type='Iter',
od_wait=100,
verbose=True)
featQ2 = model_run(train_x, train_y, train_x_17, train_y_17, 1, model)
metaQ2 = featQ2.join(train_y.logerror)
#%%
model = CatBoostRegressor(
iterations=889, learning_rate=0.01, rsm=1,
depth=6, l2_leaf_reg=6, bagging_temperature=1,
loss_function='MAE',
eval_metric='MAE',
random_seed=0,
thread_count=8,
use_best_model=True,
od_type='Iter',
od_wait=100,
verbose=True)
featQ3 = model_run(train_x, train_y, train_x_17, train_y_17, 2, model)
metaQ3 = featQ3.join(train_y.logerror)
with open('featQ23_catb.pkl','wb') as f:
pk.dump([metaQ2,metaQ3], f, protocol=pk.HIGHEST_PROTOCOL)
#%% use model trained without 2017 Q3 to predict
model = CatBoostRegressor(
iterations=2000, learning_rate=0.01, rsm=0.8,
depth=6, l2_leaf_reg=6, bagging_temperature=1,
loss_function='MAE',
eval_metric='MAE',
random_seed=0,
thread_count=8,
use_best_model=True,
od_type='Iter',
od_wait=100,
verbose=True)
pred_19 = model_run(train_x, train_y, train_x_17, train_y_17, 2, model, True)
with open('catb_pred17_7.pkl','wb') as f:
pk.dump(pred_19, f, protocol=pk.HIGHEST_PROTOCOL)
# %%
# year 2016
model = CatBoostRegressor(
iterations=1500, learning_rate=0.01, rsm=1,
depth=6, l2_leaf_reg=6, bagging_temperature=1,
loss_function='MAE',
eval_metric='MAE',
random_seed=0,
thread_count=8)
feat_10, feat_11, feat_12 = model_pred(train_x, train_y, all_x, train_x_17, train_y_17, all_x_17, 3, model)
with open('featAll16_catb.pkl','wb') as f:
pk.dump([feat_10, feat_11, feat_12], f, protocol=pk.HIGHEST_PROTOCOL)
#%%
write_sub([feat_10.f_catb.as_matrix(),feat_11.f_catb.as_matrix(),feat_12.f_catb.as_matrix(),
feat_10.f_catb.as_matrix(),feat_11.f_catb.as_matrix(),feat_12.f_catb.as_matrix()])
#%%
# mae:0.05828486 learning_rate=0.01, rsm=0.8, depth=6, l2_leaf_reg=6, bagging_temperature=1, shrink to 1767
model = CatBoostRegressor(
iterations=1768, learning_rate=0.01, rsm=0.8,
depth=6, l2_leaf_reg=6, bagging_temperature=1,
loss_function='MAE',
eval_metric='MAE',
random_seed=0,
thread_count=8,
use_best_model=True,
od_type='Iter',
od_wait=100,
verbose=True)
featQ4 = model_run(train_x, train_y, train_x_17, train_y_17, 3, model)
metaQ4 = featQ4.join(train_y.logerror)
# year 2017
#%%
model = CatBoostRegressor(
iterations=37, learning_rate=0.01, rsm=0.5,
depth=10, l2_leaf_reg=3, bagging_temperature=1,
loss_function='MAE',
eval_metric='MAE',
random_seed=0,
thread_count=8,
use_best_model=True,
od_type='Iter',
od_wait=100,
verbose=True)
featQ5 = model_run(train_x, train_y, train_x_17, train_y_17, 4, model) # train on 2016Q4, valid on 2017Q1
metaQ5 = featQ5.join(train_y_17.logerror)
#%%
model = CatBoostRegressor(
iterations=2000, learning_rate=0.01, rsm=0.8,
depth=6, l2_leaf_reg=6, bagging_temperature=1,
loss_function='MAE',
eval_metric='MAE',
random_seed=0,
thread_count=8,
use_best_model=True,
od_type='Iter',
od_wait=100,
verbose=True)
featQ6 = model_run(train_x, train_y, train_x_17, train_y_17, 5, model)
metaQ6 = featQ6.join(train_y_17.logerror)
#real_mae(featQ6)
#%%
model = CatBoostRegressor(
iterations=2000, learning_rate=0.01, rsm=0.8,
depth=6, l2_leaf_reg=6, bagging_temperature=1,
loss_function='MAE',
eval_metric='MAE',
random_seed=0,
thread_count=8,
use_best_model=True,
od_type='Iter',
od_wait=100,
verbose=True)
featQ7 = model_run(train_x, train_y, train_x_17, train_y_17, 6, model)
metaQ7 = featQ7.join(train_y_17.logerror)
with open('featQ4567_catb.pkl','wb') as f:
pk.dump([metaQ4,metaQ5,metaQ6,metaQ7], f, protocol=pk.HIGHEST_PROTOCOL)
#%% use model trained without 2017 Q3 to predict
model = CatBoostRegressor(
iterations=2000, learning_rate=0.01, rsm=0.8,
depth=6, l2_leaf_reg=6, bagging_temperature=1,
loss_function='MAE',
eval_metric='MAE',
random_seed=0,
thread_count=8,
verbose=True)
pred_19 = model_run(train_x, train_y, train_x_17, train_y_17, 6, model, True)
with open('catb_pred17_7.pkl','wb') as f:
pk.dump(pred_19, f, protocol=pk.HIGHEST_PROTOCOL)
# year 2017
#%%
model = CatBoostRegressor(
iterations=1000, learning_rate=0.01, rsm=0.8,
depth=6, l2_leaf_reg=6, bagging_temperature=1,
loss_function='MAE',
eval_metric='MAE',
random_seed=0,
thread_count=16,
verbose=True)
feat_22, feat_23, feat_24 = model_pred(train_x, train_y, all_x, train_x_17, train_y_17, all_x_17, 7, model)
#%%
with open('featAll17_catb.pkl','wb') as f:
pk.dump([feat_22, feat_23, feat_24], f, protocol=pk.HIGHEST_PROTOCOL)
#%%
write_sub([feat_10.f_catb.as_matrix(),feat_11.f_catb.as_matrix(),feat_12.f_catb.as_matrix(),
feat_22.f_catb.as_matrix(),feat_23.f_catb.as_matrix(),feat_24.f_catb.as_matrix()])