-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathoptimize.py
323 lines (259 loc) · 9.74 KB
/
optimize.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import warnings
from collections import namedtuple
import lightgbm as lgb
import numpy as np
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from logconfig import config_logging
warnings.filterwarnings('ignore')
config_logging()
logger = logging.getLogger('optimize')
Property = namedtuple('Property', ['min', 'max', 'type'])
class Optimize(object):
_num_leaves = None
_learning_rate = None
_n_estimators = None
_min_child_weight = None
_min_child_samples = None
_reg_alpha = None
_reg_lambda = None
_colsample_bytree = None
_subsample = None
def __init__(self, x_train, y_train, params, grid_params, iter_num=1):
self.x_train = x_train
self.y_train = y_train
self.params = params
self.grid_params = grid_params
self.iter_num = iter_num
# init property
self.num_leaves = None
self.learning_rate = None
self.n_estimators = None
self.min_child_weight = None
self.min_child_samples = None
self.reg_alpha = None
self.reg_lambda = None
self.colsample_bytree = None
self.subsample = None
# zip property as a dict
self.property_dict = dict(
num_leaves=self.num_leaves,
learning_rate=self.learning_rate,
n_estimators=self.n_estimators,
min_child_weight=self.min_child_weight,
min_child_samples=self.min_child_samples,
reg_alpha=self.reg_alpha,
reg_lambda=self.reg_lambda,
colsample_bytree=self.colsample_bytree,
subsample=self.subsample
)
@property
def num_leaves(self):
return self._num_leaves
@num_leaves.setter
def num_leaves(self, value=None):
default = [10, 1000, 'int']
if value is None:
self._num_leaves = Property._make(default)
elif isinstance(value, list):
self._num_leaves = Property._make(value)
elif isinstance(value, dict):
self._num_leaves = Property(**value)
else:
raise ValueError()
@property
def learning_rate(self):
return self._learning_rate
@learning_rate.setter
def learning_rate(self, value=None):
default = [0.01, 0.5, 'float']
if value is None:
self._learning_rate = Property._make(default)
elif isinstance(value, list):
self._learning_rate = Property._make(value)
elif isinstance(value, dict):
self._learning_rate = Property(**value)
else:
raise ValueError()
@property
def n_estimators(self):
return self._n_estimators
@n_estimators.setter
def n_estimators(self, value=None):
default = [500, 20000, 'int']
if value is None:
self._n_estimators = Property._make(default)
elif isinstance(value, list):
self._n_estimators = Property._make(value)
elif isinstance(value, dict):
self._n_estimators = Property(**value)
else:
raise ValueError()
@property
def min_child_weight(self):
return self._min_child_weight
@min_child_weight.setter
def min_child_weight(self, value=None):
default = [0.1, 10, 'float']
if value is None:
self._min_child_weight = Property._make(default)
elif isinstance(value, list):
self._min_child_weight = Property._make(value)
elif isinstance(value, dict):
self._min_child_weight = Property(**value)
else:
raise ValueError()
@property
def min_child_samples(self):
return self._min_child_samples
@min_child_samples.setter
def min_child_samples(self, value=None):
default = [50, 1000, 'int']
if value is None:
self._min_child_samples = Property._make(default)
elif isinstance(value, list):
self._min_child_samples = Property._make(value)
elif isinstance(value, dict):
self._min_child_samples = Property(**value)
else:
raise ValueError()
@property
def reg_alpha(self):
return self._reg_alpha
@reg_alpha.setter
def reg_alpha(self, value=None):
default = [0, 10, 'float']
if value is None:
self._reg_alpha = Property._make(default)
elif isinstance(value, list):
self._reg_alpha = Property._make(value)
elif isinstance(value, dict):
self._reg_alpha = Property(**value)
else:
raise ValueError()
@property
def reg_lambda(self):
return self._reg_lambda
@reg_lambda.setter
def reg_lambda(self, value=None):
default = [0, 10, 'float']
if value is None:
self._reg_lambda = Property._make(default)
elif isinstance(value, list):
self._reg_lambda = Property._make(value)
elif isinstance(value, dict):
self._reg_lambda = Property(**value)
else:
raise ValueError()
@property
def colsample_bytree(self):
return self._colsample_bytree
@colsample_bytree.setter
def colsample_bytree(self, value=None):
default = [0.5, 1, 'float']
if value is None:
self._colsample_bytree = Property._make(default)
elif isinstance(value, list):
self._colsample_bytree = Property._make(value)
elif isinstance(value, dict):
self._colsample_bytree = Property(**value)
else:
raise ValueError()
@property
def subsample(self):
return self._subsample
@subsample.setter
def subsample(self, value=None):
default = [0.5, 1, 'float']
if value is None:
self._subsample = Property._make(default)
elif isinstance(value, list):
self._subsample = Property._make(value)
elif isinstance(value, dict):
self._subsample = Property(**value)
else:
raise ValueError()
@staticmethod
def _get_values_list(low, high, dtype, size):
linspace = np.linspace(low, high, size, dtype=dtype)
if dtype == 'float':
linspace = list(map(lambda item: round(item, 4), linspace))
return linspace
def _get_grid_params(self, values, key, best_value, size):
max_value = max(values)
min_value = min(values)
property_item = self.property_dict[key]
if best_value == max_value:
if best_value == property_item.max:
return [best_value]
low = best_value
high = property_item.max
linspace = self._get_values_list(low, high, property_item.type, size)
elif best_value == min_value:
if best_value == property_item.min:
return [best_value]
low = min_value
high = best_value
linspace = self._get_values_list(low, high, property_item.type, size)
else:
best_index = values.index(best_value)
low = values[best_index - 1]
high = values[best_index + 1]
linspace = self._get_values_list(low, high, property_item.type, size)
linspace = list(set(linspace))
return linspace
def _update_params(self, best_params):
for key, value in best_params.items():
self.params[key] = value
def _update_grid_params(self, best_params, size=4):
for key, value in best_params.items():
values = self.grid_params[key]
values_list = self._get_grid_params(values, key, value, size)
self.grid_params[key] = values_list
def _optimize(self, params, grid_params):
clf = lgb.LGBMClassifier(**params)
grid_clf = GridSearchCV(clf, grid_params, cv=5, scoring='neg_log_loss', n_jobs=1, verbose=100)
grid_clf.fit(self.x_train, self.y_train)
return grid_clf
def optimize(self):
best_params = None
while self.iter_num > 0:
grid_clf = self._optimize(self.params, self.grid_params)
best_params = grid_clf.best_params_
best_score = grid_clf.best_score_
logger.info('iter_num: {} best_params: {}'.format(self.iter_num, best_params))
logger.info('iter_num: {} best_score: {}'.format(self.iter_num, best_score))
self._update_params(best_params)
self._update_grid_params(best_params)
self.iter_num -= 1
return best_params
class SimpleOptimize(object):
def __init__(self, x_train, y_train, params, opt_params):
self.x_train = x_train
self.y_train = y_train
self.params = params
self.opt_params = opt_params
def _update_params(self, best_params):
for key, value in best_params.items():
self.params[key] = value
def optimize(self, grid=True, random=False):
gbm = lgb.LGBMClassifier(**self.params)
if grid:
opt_gbm = GridSearchCV(gbm, self.opt_params, cv=5, scoring='neg_log_loss', refit="binary_logloss",
n_jobs=1, verbose=100)
elif random:
opt_gbm = RandomizedSearchCV(gbm, self.opt_params, cv=5, scoring='neg_log_loss', refit="binary_logloss",
n_jobs=1, verbose=100)
else:
raise ValueError()
opt_gbm.fit(self.x_train, self.y_train)
best_params = opt_gbm.best_params_
best_score = opt_gbm.best_score_
logger.info('best_params: {}'.format(best_params))
logger.info('best_score: {}'.format(best_score))
self._update_params(best_params)
logger.info('update best params: {}'.format(self.params))
return self.params