-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtree_regression.py
376 lines (314 loc) · 12.5 KB
/
tree_regression.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import inspect
import time
import warnings
import numpy as np
import pandas as pd
from catboost import CatBoostRegressor
from lightgbm import LGBMRegressor
from scipy import sparse
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import KFold
from xgboost import XGBRegressor
from utils import get_data
from utils import timer
warnings.filterwarnings(action='ignore')
class TreeRegression(object):
def __init__(self, mode, n_fold=10, seed=4590, save=False):
self.mode = mode
self.n_fold = n_fold
self.seed = seed
self.save = save
self._check_mode(self.mode)
@staticmethod
def _check_mode(mode):
assert mode in ['lgb', 'xgb', 'rf', 'ctb', 'ada', 'gbdt']
def _get_gbm(self, params):
if self.mode == 'lgb':
gbm = LGBMRegressor(**params)
elif self.mode == 'xgb':
gbm = XGBRegressor(**params)
elif self.mode == 'ctb':
gbm = CatBoostRegressor(**params)
elif self.mode == 'ada':
gbm = AdaBoostRegressor(**params)
elif self.mode == 'gbdt':
gbm = GradientBoostingRegressor(**params)
elif self.mode == 'rf':
gbm = RandomForestRegressor(**params)
else:
raise ValueError()
return gbm
@staticmethod
def _get_dataset():
dataset = get_data()
train_data = dataset[dataset['score'] > 0.0]
test_data = dataset[dataset['score'] < 0.0]
train_data.reset_index(inplace=True, drop=True)
test_data.reset_index(inplace=True, drop=True)
return train_data, test_data
@staticmethod
def _get_iteration_kwargs(gbm):
predict_args = inspect.getfullargspec(gbm.predict).args
if hasattr(gbm, 'best_iteration_'):
best_iteration = getattr(gbm, 'best_iteration_')
if 'num_iteration' in predict_args:
iteration_kwargs = {'num_iteration': best_iteration}
elif 'ntree_end' in predict_args:
iteration_kwargs = {'ntree_end': best_iteration}
else:
raise ValueError()
elif hasattr(gbm, 'best_ntree_limit'):
best_iteration = getattr(gbm, 'best_ntree_limit')
if 'ntree_limit' in predict_args:
iteration_kwargs = {'ntree_limit': best_iteration}
else:
raise ValueError()
else:
raise ValueError()
return iteration_kwargs
def _ensemble_tree(self, params):
train_data, test_data = self._get_dataset()
columns = train_data.columns
remove_columns = ['id', 'score']
features_columns = [column for column in columns if column not in remove_columns]
train_labels = train_data['score']
train_x = train_data[features_columns]
test_x = test_data[features_columns]
# to csr 加快模型速度
train_x = sparse.csr_matrix(train_x.values)
test_x = sparse.csr_matrix(test_x.values)
kfolder = KFold(n_splits=self.n_fold, shuffle=True, random_state=self.seed)
kfold = kfolder.split(train_x, train_labels)
preds_list = list()
oof = np.zeros(train_data.shape[0])
for train_index, vali_index in kfold:
k_x_train = train_x[train_index]
k_y_train = train_labels.loc[train_index]
k_x_vali = train_x[vali_index]
k_y_vali = train_labels.loc[vali_index]
gbm = self._get_gbm(params)
gbm = gbm.fit(k_x_train, k_y_train, eval_set=[(k_x_train, k_y_train), (k_x_vali, k_y_vali)],
early_stopping_rounds=200, verbose=False)
iteration_kwargs = self._get_iteration_kwargs(gbm)
k_pred = gbm.predict(k_x_vali, **iteration_kwargs)
oof[vali_index] = k_pred
preds = gbm.predict(test_x, **iteration_kwargs)
preds_list.append(preds)
fold_mae_error = mean_absolute_error(train_labels, oof)
print(f'{self.mode} fold mae error is {fold_mae_error}')
fold_score = 1 / (1 + fold_mae_error)
print(f'fold score is {fold_score}')
preds_columns = ['preds_{id}'.format(id=i) for i in range(self.n_fold)]
preds_df = pd.DataFrame(data=preds_list)
preds_df = preds_df.T
preds_df.columns = preds_columns
preds_list = list(preds_df.mean(axis=1))
prediction = preds_list
if self.save:
sub_df = pd.DataFrame({'id': test_data['id'],
'score': prediction})
sub_df['score'] = sub_df['score'].apply(lambda item: int(round(item)))
sub_df.to_csv('submittion.csv', index=False)
return oof, prediction
def _sklearn_tree(self, params):
train_data, test_data = self._get_dataset()
columns = train_data.columns
remove_columns = ['id', 'score']
features_columns = [column for column in columns if column not in remove_columns]
train_labels = train_data['score']
train_x = train_data[features_columns]
test_x = test_data[features_columns]
# to csr 加快模型速度
train_x = sparse.csr_matrix(train_x.values)
test_x = sparse.csr_matrix(test_x.values)
kfolder = KFold(n_splits=self.n_fold, shuffle=True, random_state=self.seed)
kfold = kfolder.split(train_x, train_labels)
preds_list = list()
oof = np.zeros(train_data.shape[0])
for train_index, vali_index in kfold:
k_x_train = train_x[train_index]
k_y_train = train_labels.loc[train_index]
k_x_vali = train_x[vali_index]
gbm = self._get_gbm(params)
gbm.fit(k_x_train, k_y_train)
k_pred = gbm.predict(k_x_vali)
oof[vali_index] = k_pred
preds = gbm.predict(test_x)
preds_list.append(preds)
fold_mae_error = mean_absolute_error(train_labels, oof)
print(f'{self.mode} fold mae error is {fold_mae_error}')
fold_score = 1 / (1 + fold_mae_error)
print(f'fold score is {fold_score}')
preds_columns = ['preds_{id}'.format(id=i) for i in range(self.n_fold)]
preds_df = pd.DataFrame(data=preds_list)
preds_df = preds_df.T
preds_df.columns = preds_columns
preds_list = list(preds_df.mean(axis=1))
prediction = preds_list
if self.save:
sub_df = pd.DataFrame({'id': test_data['id'],
'score': prediction})
sub_df['score'] = sub_df['score'].apply(lambda item: int(round(item)))
sub_df.to_csv('submittion.csv', index=False)
return oof, prediction
def _ctb_boost_tree(self, params):
# catboost 不支持csr,单独考虑
train_data, test_data = self._get_dataset()
columns = train_data.columns
remove_columns = ['id', 'score']
features_columns = [column for column in columns if column not in remove_columns]
train_labels = train_data['score']
train_x = train_data[features_columns]
test_x = test_data[features_columns]
kfolder = KFold(n_splits=self.n_fold, shuffle=True, random_state=self.seed)
kfold = kfolder.split(train_x, train_labels)
preds_list = list()
oof = np.zeros(train_data.shape[0])
for train_index, vali_index in kfold:
k_x_train = train_x.loc[train_index]
k_y_train = train_labels.loc[train_index]
k_x_vali = train_x.loc[vali_index]
k_y_vali = train_labels.loc[vali_index]
gbm = self._get_gbm(params)
gbm = gbm.fit(k_x_train, k_y_train, eval_set=[(k_x_train, k_y_train), (k_x_vali, k_y_vali)],
early_stopping_rounds=200, verbose=False)
iteration_kwargs = self._get_iteration_kwargs(gbm)
k_pred = gbm.predict(k_x_vali, **iteration_kwargs)
oof[vali_index] = k_pred
preds = gbm.predict(test_x, **iteration_kwargs)
preds_list.append(preds)
fold_mae_error = mean_absolute_error(train_labels, oof)
print(f'{self.mode} fold mae error is {fold_mae_error}')
fold_score = 1 / (1 + fold_mae_error)
print(f'fold score is {fold_score}')
preds_columns = ['preds_{id}'.format(id=i) for i in range(self.n_fold)]
preds_df = pd.DataFrame(data=preds_list)
preds_df = preds_df.T
preds_df.columns = preds_columns
preds_list = list(preds_df.mean(axis=1))
prediction = preds_list
if self.save:
sub_df = pd.DataFrame({'id': test_data['id'],
'score': prediction})
sub_df['score'] = sub_df['score'].apply(lambda item: int(round(item)))
sub_df.to_csv('submittion.csv', index=False)
return oof, prediction
@timer(func_name='TreeModels.tree.model')
def tree_model(self, params):
if self.mode in ['lgb', 'xgb']:
oof, prediction = self._ensemble_tree(params)
elif self.mode in ['ada', 'rf', 'gbdt']:
oof, prediction = self._sklearn_tree(params)
elif self.mode == 'ctb':
oof, prediction = self._ctb_boost_tree(params)
else:
raise ValueError()
return oof, prediction
def regression_main(mode, **kwargs):
assert mode in ['lgb', 'xgb', 'rf', 'ctb', 'ada', 'gbdt']
lgb_params = {
'boosting_type': 'gbdt',
'objective': 'mae',
'n_estimators': 10000,
'metric': 'mae',
'learning_rate': 0.01,
'min_child_samples': 46,
'min_child_weight': 0.01,
'subsample_freq': 1,
'num_leaves': 40,
'max_depth': 7,
'subsample': 0.42,
'colsample_bytree': 0.48,
'reg_alpha': 0.15,
'reg_lambda': 5,
'verbose': -1,
'seed': 4590
}
# lgb_params = {
# 'boosting_type': 'gbdt',
# 'objective': 'mae',
# 'n_estimators': 10000,
# 'metric': 'mae',
# 'learning_rate': 0.01,
# 'min_child_samples': 43,
# 'min_child_weight': 0,
# 'subsample_freq': 1,
# 'num_leaves': 65,
# 'max_depth': 6,
# 'subsample': 0.6,
# 'colsample_bytree': 0.8,
# 'reg_alpha': 2,
# 'reg_lambda': 0.1,
# 'verbose': -1,
# 'seed': 4590
# }
xgb_params = {
'booster': 'gbtree',
'learning_rate': 0.01,
'max_depth': 5,
'subsample': 0.7,
'colsample_bytree': 0.8,
'objective': 'reg:linear',
'n_estimators': 10000,
'min_child_weight': 3,
'gamma': 0,
'silent': True,
'n_jobs': 4,
'random_state': 4590,
'reg_alpha': 2,
'reg_lambda': 0.1,
'alpha': 1,
'verbose': 1
}
ctb_params = {
'n_estimators': 10000,
'learning_rate': 0.01,
'random_seed': 4590,
'reg_lambda': 5,
'subsample': 0.7,
'bootstrap_type': 'Bernoulli',
'boosting_type': 'Plain',
'one_hot_max_size': 10,
'rsm': 0.5,
'leaf_estimation_iterations': 5,
'use_best_model': True,
'max_depth': 6,
'verbose': -1,
'thread_count': 4
}
gbdt_params = {
'loss': 'lad',
'learning_rate': 0.1,
'n_estimators': 1000,
'random_state': 2019
}
rf_params = {
'n_estimators': 1000,
'n_jobs': 5,
'random_state': 2019
}
if mode == 'lgb':
lgb_oof, lgb_prediction = TreeRegression(mode='lgb', **kwargs).tree_model(lgb_params)
return lgb_oof, lgb_prediction
elif mode == 'xgb':
xgb_oof, xgb_prediction = TreeRegression(mode='xgb', **kwargs).tree_model(xgb_params)
return xgb_oof, xgb_prediction
elif mode == 'ctb':
ctb_oof, ctb_prediction = TreeRegression(mode='ctb', **kwargs).tree_model(ctb_params)
return ctb_oof, ctb_prediction
elif mode == 'gbdt':
gbdt_oof, gbdt_prediction = TreeRegression(mode='gbdt', **kwargs).tree_model(gbdt_params)
return gbdt_oof, gbdt_prediction
elif mode == 'rf':
rf_oof, rf_prediction = TreeRegression(mode='rf', **kwargs).tree_model(rf_params)
return rf_oof, rf_prediction
if __name__ == '__main__':
t0 = time.time()
regression_main(mode='lgb', save=True)
usage_time = time.time() - t0
print(f'usage time: {usage_time}')