-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtitanic_classifier.py
276 lines (196 loc) · 10.4 KB
/
titanic_classifier.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
# pylint: disable=C0321,C0103,E1221,C0301,E1305,E1121,C0302,C0330
# -*- coding: utf-8 -*-
"""
You can put hardcode here, specific to titatinic dataet
All in one file config
python titanic_classifier.py train > zlog/log_titanic_train.txt 2>&1
python titanic_classifier.py predict > zlog/log_titanic_predict.txt 2>&1
"""
import warnings, copy, os, sys
warnings.filterwarnings('ignore')
###################################################################################
from source import util_feature
####################################################################################
###### Path ########################################################################
print( os.getcwd())
root = os.path.abspath(os.getcwd()).replace("\\", "/") + "/"
print(root)
dir_data = os.path.abspath( root + "/data/" ) + "/"
dir_data = dir_data.replace("\\", "/")
print(dir_data)
def global_pars_update(model_dict, data_name, config_name):
m = {}
model_name = model_dict['model_pars']['model_class']
m['path_config_model'] = root + f"/{config_file}"
m['config_name'] = config_name
m['path_data_train'] = f'data/input/{data_name}/train/'
m['path_data_test'] = f'data/input/{data_name}/test/'
m['path_model'] = f'data/output/{data_name}/{config_name}/'
m['path_output_pred'] = f'data/output/{data_name}/pred_{config_name}/'
m['n_sample'] = model_dict['data_pars'].get('n_sample', 5000)
model_dict[ 'global_pars'] = m
return model_dict
def os_get_function_name():
import sys
return sys._getframe(1).f_code.co_name
####################################################################################
config_file = "titanic_classifier.py" ### name of file which contains data configuration
config_default = 'titanic_lightgbm' ### name of function which contains data configuration
####################################################################################
##### Params########################################################################
# data_name = "titanic" ### in data/input/
cols_input_type_1 = {
"coly" : "Survived"
,"colid" : "PassengerId"
,"colcat" : ["Sex", "Embarked" ]
,"colnum" : ["Pclass", "Age","SibSp", "Parch","Fare"]
,"coltext" : []
,"coldate" : []
,"colcross" : [ "Name", "Sex", "Ticket","Embarked","Pclass", "Age","SibSp", "Parch","Fare" ]
}
cols_input_type_2 = {
"coly" : "Survived"
,"colid" : "PassengerId"
,"colcat" : ["Sex", "Embarked" ]
,"colnum" : ["Pclass", "Age","SibSp", "Parch","Fare"]
,"coltext" : ["Name", "Ticket"]
,"coldate" : []
,"colcross" : [ "Name", "Sex", "Ticket","Embarked","Pclass", "Age","SibSp", "Parch","Fare" ]
}
####################################################################################
def titanic_lightgbm(path_model_out="") :
"""
Contains all needed informations for Light GBM Classifier model,
used for titanic classification task
"""
data_name = "titanic" ### in data/input/
model_class = 'LGBMClassifier' ### ACTUAL Class name for model_sklearn.py
n_sample = 1000
def post_process_fun(y):
### After prediction is done
return int(y)
def pre_process_fun(y):
### Before the prediction is done
return int(y)
model_dict = {'model_pars': {
'model_path' : path_model_out
### LightGBM API model #######################################
,'model_class': model_class
,'model_pars' : {'objective': 'binary',
'n_estimators':3000,
'learning_rate':0.001,
'boosting_type':'gbdt', ### Model hyperparameters
'early_stopping_rounds': 5
}
### After prediction ##########################################
, 'post_process_fun' : post_process_fun
### Before training ##########################################
, 'pre_process_pars' : {'y_norm_fun' : pre_process_fun ,
### Pipeline for data processing ##############################
'pipe_list': [
{'uri': 'source/preprocessors.py::pd_coly', 'pars': {}, 'cols_family': 'coly', 'cols_out': 'coly', 'type': 'coly' },
{'uri': 'source/preprocessors.py::pd_colnum_bin', 'pars': {}, 'cols_family': 'colnum', 'cols_out': 'colnum_bin', 'type': '' },
{'uri': 'source/preprocessors.py::pd_colnum_binto_onehot', 'pars': {}, 'cols_family': 'colnum_bin', 'cols_out': 'colnum_onehot', 'type': '' },
{'uri': 'source/preprocessors.py::pd_colcat_bin', 'pars': {}, 'cols_family': 'colcat', 'cols_out': 'colcat_bin', 'type': '' },
{'uri': 'source/preprocessors.py::pd_colcat_to_onehot', 'pars': {}, 'cols_family': 'colcat_bin', 'cols_out': 'colcat_onehot', 'type': '' },
{'uri': 'source/preprocessors.py::pd_colcross', 'pars': {}, 'cols_family': 'colcross', 'cols_out': 'colcross_pair_onehot', 'type': 'cross'}
],
}
},
'compute_pars': { 'metric_list': ['accuracy_score','average_precision_score']
},
'data_pars': { 'n_sample' : n_sample,
'cols_input_type' : cols_input_type_1,
### family of columns for MODEL ########################################################
# "colnum", "colnum_bin", "colnum_onehot", "colnum_binmap", #### Colnum columns
# "colcat", "colcat_bin", "colcat_onehot", "colcat_bin_map", #### colcat columns
# 'colcross_single_onehot_select', "colcross_pair_onehot", 'colcross_pair', #### colcross columns
# 'coldate',
# 'coltext',
'cols_model_group': [ 'colnum_bin',
'colcat_bin',
# 'coltext',
# 'coldate',
# 'colcross_pair'
]
### Filter data rows ##################################################################
,'filter_pars': { 'ymax' : 2 ,'ymin' : -1 }
}
}
##### Filling Global parameters ############################################################
model_dict = global_pars_update(model_dict, data_name, config_name=os_get_function_name() )
return model_dict
#####################################################################################
########## Profile data #############################################################
def data_profile(path_data_train="", path_model="", n_sample= 5000):
from source.run_feature_profile import run_profile
run_profile(path_data = path_data_train,
path_output = path_model + "/profile/",
n_sample = n_sample,
)
###################################################################################
########## Preprocess #############################################################
def preprocess(config=None, nsample=None):
model_class = config if config is not None else config_default
mdict = globals()[model_class]()
m = mdict['global_pars']
print(mdict)
from source import run_preprocess, run_preprocess_old
run_preprocess.run_preprocess(config_name= model_class,
path_data = m['path_data_train'],
path_output = m['path_model'],
path_config_model = m['path_config_model'],
n_sample = nsample if nsample is not None else m['n_sample'],
mode = 'run_preprocess')
##################################################################################
########## Train #################################################################
def train(config=None, nsample=None):
model_class = config if config is not None else config_default
mdict = globals()[model_class]()
m = mdict['global_pars']
print(mdict)
from source import run_train
run_train.run_train(config_name= model_class,
path_data = m['path_data_train'],
path_output = m['path_model'],
path_config_model = m['path_config_model'],
n_sample = nsample if nsample is not None else m['n_sample']
)
###################################################################################
######### Check data ##############################################################
def check():
pass
####################################################################################
####### Inference ##################################################################
def predict(config=None, nsample=None):
model_class = config if config is not None else config_default
mdict = globals()[model_class]()
m = mdict['global_pars']
from source import run_inference,run_inference
run_inference.run_predict(model_class,
path_model = m['path_model'],
path_data = m['path_data_test'],
path_output = m['path_output_pred'],
pars={'cols_group': mdict['data_pars']['cols_input_type'],
'pipe_list': mdict['model_pars']['pre_process_pars']['pipe_list']},
n_sample = nsample if nsample is not None else m['n_sample']
)
def run_all():
data_profile()
preprocess()
train()
check()
predict()
###########################################################################################################
###########################################################################################################
"""
python titanic_classifier.py data_profile
python titanic_classifier.py preprocess --nsample 100
python titanic_classifier.py train --nsample 200
python titanic_classifier.py check
python titanic_classifier.py predict
python titanic_classifier.py run_all
"""
if __name__ == "__main__":
import fire
fire.Fire()