This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
/
pipeline_blocks.py
427 lines (373 loc) · 22.9 KB
/
pipeline_blocks.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
420
421
422
423
424
425
426
427
from functools import partial
from sklearn.metrics import roc_auc_score
from steppy.adapter import Adapter, E
from steppy.base import Step, make_transformer
from toolkit.misc import LightGBM
import feature_extraction as fe
from hyperparameter_tuning import RandomSearchOptimizer, NeptuneMonitor, PersistResults
from models import get_sklearn_classifier, XGBoost
from utils import ToNumpyLabel
def classifier_light_gbm(features, config, train_mode, **kwargs):
if train_mode:
features_train, features_valid = features
if config.random_search.light_gbm.n_runs:
transformer = RandomSearchOptimizer(TransformerClass=LightGBM,
params=config.light_gbm,
train_input_keys=[],
valid_input_keys=['X_valid', 'y_valid'],
score_func=roc_auc_score,
maximize=True,
n_runs=config.random_search.light_gbm.n_runs,
callbacks=[
NeptuneMonitor(
**config.random_search.light_gbm.callbacks.neptune_monitor),
PersistResults(
**config.random_search.light_gbm.callbacks.persist_results)]
)
else:
transformer = LightGBM(**config.light_gbm)
light_gbm = Step(name='light_gbm',
transformer=transformer,
input_data=['input'],
input_steps=[features_train, features_valid],
adapter=Adapter({'X': E(features_train.name, 'features'),
'y': E('input', 'y'),
'feature_names': E(features_train.name, 'feature_names'),
'categorical_features': E(features_train.name, 'categorical_features'),
'X_valid': E(features_valid.name, 'features'),
'y_valid': E('input', 'y_valid'),
}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
else:
light_gbm = Step(name='light_gbm',
transformer=LightGBM(**config.light_gbm),
input_steps=[features],
adapter=Adapter({'X': E(features.name, 'features')}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return light_gbm
def classifier_xgb(features, config, train_mode, **kwargs):
if train_mode:
features_train, features_valid = features
if config.random_search.xgboost.n_runs:
transformer = RandomSearchOptimizer(TransformerClass=XGBoost,
params=config.xgboost,
train_input_keys=[],
valid_input_keys=['X_valid', 'y_valid'],
score_func=roc_auc_score,
maximize=True,
n_runs=config.random_search.xgboost.n_runs,
callbacks=[
NeptuneMonitor(
**config.random_search.xgboost.callbacks.neptune_monitor),
PersistResults(
**config.random_search.xgboost.callbacks.persist_results)]
)
else:
transformer = XGBoost(**config.xgboost)
xgboost = Step(name='xgboost',
transformer=transformer,
input_data=['input'],
input_steps=[features_train, features_valid],
adapter=Adapter({'X': E(features_train.name, 'features'),
'y': E('input', 'y'),
'feature_names': E(features_train.name, 'feature_names'),
'X_valid': E(features_valid.name, 'features'),
'y_valid': E('input', 'y_valid'),
}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
else:
xgboost = Step(name='xgboost',
transformer=XGBoost(**config.xgboost),
input_steps=[features],
adapter=Adapter({'X': E(features.name, 'features')}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return xgboost
def classifier_sklearn(sklearn_features, ClassifierClass, full_config, clf_name, train_mode, normalize, **kwargs):
config, model_params, rs_config = full_config
if train_mode:
if config.random_search.random_forest.n_runs:
transformer = RandomSearchOptimizer(
partial(get_sklearn_classifier,
ClassifierClass=ClassifierClass,
normalize=normalize),
model_params,
train_input_keys=[],
valid_input_keys=['X_valid', 'y_valid'],
score_func=roc_auc_score,
maximize=True,
n_runs=rs_config.n_runs,
callbacks=[NeptuneMonitor(**rs_config.callbacks.neptune_monitor),
PersistResults(**rs_config.callbacks.persist_results)]
)
else:
transformer = get_sklearn_classifier(ClassifierClass, normalize, **model_params)
sklearn_clf = Step(name=clf_name,
transformer=transformer,
input_data=['input'],
input_steps=[sklearn_features],
adapter=Adapter({'X': E(sklearn_features.name, 'X'),
'y': E('input', 'y'),
'X_valid': E(sklearn_features.name, 'X_valid'),
'y_valid': E('input', 'y_valid'),
}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
else:
sklearn_clf = Step(name=clf_name,
transformer=get_sklearn_classifier(ClassifierClass, normalize, **model_params),
input_steps=[sklearn_features],
adapter=Adapter({'X': E(sklearn_features.name, 'X')}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return sklearn_clf
def feature_extraction(config, train_mode, **kwargs):
if train_mode:
feature_by_type_split, feature_by_type_split_valid = _feature_by_type_splits(config, train_mode)
bureau, bureau_valid = _bureau(config, train_mode, **kwargs)
categorical_encoder, categorical_encoder_valid = _categorical_encoders(
(feature_by_type_split, feature_by_type_split_valid),
config,
train_mode,
**kwargs)
groupby_aggregation, groupby_aggregation_valid = _groupby_aggregations(
(feature_by_type_split, feature_by_type_split_valid),
config,
train_mode,
**kwargs)
feature_combiner, feature_combiner_valid = _join_features(numerical_features=[feature_by_type_split,
groupby_aggregation,
bureau],
numerical_features_valid=[feature_by_type_split_valid,
groupby_aggregation_valid,
bureau_valid],
categorical_features=[categorical_encoder],
categorical_features_valid=[
categorical_encoder_valid],
config=config,
train_mode=train_mode,
**kwargs)
return feature_combiner, feature_combiner_valid
else:
feature_by_type_split = _feature_by_type_splits(config, train_mode)
bureau = _bureau(config, train_mode, **kwargs)
categorical_encoder = _categorical_encoders(feature_by_type_split, config, train_mode, **kwargs)
groupby_aggregation = _groupby_aggregations(feature_by_type_split, config, train_mode, **kwargs)
feature_combiner = _join_features(numerical_features=[feature_by_type_split, groupby_aggregation, bureau],
numerical_features_valid=[],
categorical_features=[categorical_encoder],
categorical_features_valid=[],
config=config,
train_mode=train_mode,
**kwargs)
return feature_combiner
def preprocessing_fillna(features, config, train_mode, **kwargs):
if train_mode:
features_train, features_valid = features
fillna = Step(name='fillna',
transformer=_fillna(**config.preprocessing),
input_data=['input'],
input_steps=[features_train, features_valid],
adapter=Adapter({'X': E(features_train.name, 'features'),
'X_valid': E(features_valid.name, 'features'),
}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs
)
else:
fillna = Step(name='fillna',
transformer=_fillna(**config.preprocessing),
input_data=['input'],
input_steps=[features],
adapter=Adapter({'X': E(features.name, 'features')}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs
)
return fillna
def _feature_by_type_splits(config, train_mode):
if train_mode:
feature_by_type_split = Step(name='feature_by_type_split',
transformer=fe.DataFrameByTypeSplitter(**config.dataframe_by_type_splitter),
input_data=['input'],
adapter=Adapter({'X': E('input', 'X')}),
experiment_directory=config.pipeline.experiment_directory)
feature_by_type_split_valid = Step(name='feature_by_type_split_valid',
transformer=feature_by_type_split,
input_data=['input'],
adapter=Adapter({'X': E('input', 'X_valid')}),
experiment_directory=config.pipeline.experiment_directory)
return feature_by_type_split, feature_by_type_split_valid
else:
feature_by_type_split = Step(name='feature_by_type_split',
transformer=fe.DataFrameByTypeSplitter(**config.dataframe_by_type_splitter),
input_data=['input'],
adapter=Adapter({'X': E('input', 'X')}),
experiment_directory=config.pipeline.experiment_directory)
return feature_by_type_split
def _join_features(numerical_features,
numerical_features_valid,
categorical_features,
categorical_features_valid,
config, train_mode,
**kwargs):
if train_mode:
feature_joiner = Step(name='feature_joiner',
transformer=fe.FeatureJoiner(),
input_steps=numerical_features + categorical_features,
adapter=Adapter({
'numerical_feature_list': [
E(feature.name, 'numerical_features') for feature in numerical_features],
'categorical_feature_list': [
E(feature.name, 'categorical_features') for feature in categorical_features],
}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
feature_joiner_valid = Step(name='feature_joiner_valid',
transformer=feature_joiner,
input_steps=numerical_features_valid + categorical_features_valid,
adapter=Adapter({
'numerical_feature_list': [
E(feature.name,
'numerical_features') for feature in numerical_features_valid],
'categorical_feature_list': [
E(feature.name,
'categorical_features') for feature in categorical_features_valid],
}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return feature_joiner, feature_joiner_valid
else:
feature_joiner = Step(name='feature_joiner',
transformer=fe.FeatureJoiner(),
input_steps=numerical_features + categorical_features,
adapter=Adapter(
{'numerical_feature_list':
[E(feature.name, 'numerical_features') for feature in numerical_features],
'categorical_feature_list':
[E(feature.name, 'categorical_features') for feature in categorical_features]}
),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return feature_joiner
def _categorical_encoders(dispatchers, config, train_mode, **kwargs):
if train_mode:
feature_by_type_split, feature_by_type_split_valid = dispatchers
numpy_label, numpy_label_valid = _to_numpy_label(config, **kwargs)
categorical_encoder = Step(name='categorical_encoder',
transformer=fe.CategoricalEncoder(),
input_data=['input'],
input_steps=[feature_by_type_split, numpy_label],
adapter=Adapter({'X': E(feature_by_type_split.name, 'categorical_features'),
'y': E(numpy_label.name, 'y')}
),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
categorical_encoder_valid = Step(name='categorical_encoder_valid',
transformer=categorical_encoder,
input_data=['input'],
input_steps=[feature_by_type_split_valid, numpy_label_valid],
adapter=Adapter(
{'X': E(feature_by_type_split_valid.name, 'categorical_features'),
'y': E(numpy_label_valid.name, 'y')}
),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return categorical_encoder, categorical_encoder_valid
else:
feature_by_type_split = dispatchers
categorical_encoder = Step(name='categorical_encoder',
transformer=fe.CategoricalEncoder(),
input_data=['input'],
input_steps=[feature_by_type_split],
adapter=Adapter({'X': E(feature_by_type_split.name, 'categorical_features')}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return categorical_encoder
def _groupby_aggregations(dispatchers, config, train_mode, **kwargs):
if train_mode:
feature_by_type_split, feature_by_type_split_valid = dispatchers
groupby_aggregations = Step(name='groupby_aggregations',
transformer=fe.GroupbyAggregations(**config.groupby_aggregation),
input_data=['input'],
input_steps=[feature_by_type_split],
adapter=Adapter({'categorical_features': E(feature_by_type_split.name,
'categorical_features'),
'numerical_features': E(feature_by_type_split.name,
'numerical_features')
}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
groupby_aggregations_valid = Step(name='groupby_aggregations_valid',
transformer=groupby_aggregations,
input_data=['input'],
input_steps=[feature_by_type_split_valid],
adapter=Adapter({'categorical_features': E(feature_by_type_split_valid.name,
'categorical_features'),
'numerical_features': E(feature_by_type_split_valid.name,
'numerical_features')
}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return groupby_aggregations, groupby_aggregations_valid
else:
feature_by_type_split = dispatchers
groupby_aggregations = Step(name='groupby_aggregations',
transformer=fe.GroupbyAggregations(**config.groupby_aggregation),
input_data=['input'],
input_steps=[feature_by_type_split],
adapter=Adapter({'categorical_features': E(feature_by_type_split.name,
'categorical_features'),
'numerical_features': E(feature_by_type_split.name,
'numerical_features')
}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return groupby_aggregations
def _bureau(config, train_mode, **kwargs):
if train_mode:
bureau = Step(name='bureau',
transformer=fe.GroupbyAggregationFromFile(**config.bureau),
input_data=['input'],
adapter=Adapter({'X': E('input', 'X')}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
bureau_valid = Step(name='bureau_valid',
transformer=bureau,
input_data=['input'],
adapter=Adapter({'X': E('input', 'X_valid')}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return bureau, bureau_valid
else:
bureau = Step(name='bureau',
transformer=fe.GroupbyAggregationFromFile(**config.bureau),
input_data=['input'],
adapter=Adapter({'X': E('input', 'X')}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return bureau
def _fillna(fillna_value):
def _inner_fillna(X, X_valid=None):
if X_valid is None:
return {'X': X.fillna(fillna_value)}
else:
return {'X': X.fillna(fillna_value),
'X_valid': X_valid.fillna(fillna_value)}
return make_transformer(_inner_fillna)
def _to_numpy_label(config, **kwargs):
to_numpy_label = Step(name='to_numpy_label',
transformer=ToNumpyLabel(),
input_data=['input'],
adapter=Adapter({'y': [E('input', 'y')]}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
to_numpy_label_valid = Step(name='to_numpy_label_valid',
transformer=to_numpy_label,
input_data=['input'],
adapter=Adapter({'y': [E('input', 'y_valid')]}),
experiment_directory=config.pipeline.experiment_directory,
**kwargs)
return to_numpy_label, to_numpy_label_valid