-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathsentiment_classifier.py
342 lines (230 loc) · 11.9 KB
/
sentiment_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
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
# 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 sentiment_classifier.py preprocess
python sentiment_classifier.py train > zlog/log-airbnb.txt 2>&1
python sentiment_classifier.py check > zlog/log-airbnb.txt 2>&1
python sentiment_classifier.py predict > zlog/log-airbnb.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['model_class'] = 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}/{model_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 = "sentiment_classifier.py"
config_default = 'sentiment_lightgbm'
#####################################################################################
####################################################################################
##### Params########################################################################
cols_input_type_1 = {
"coly" : ""
,"colid" : ""
,"colcat" : [ ]
,"colnum" : [ ]
,"coltext" : [ ]
,"coldate" : [ ]
,"colcross" : [ ]
}
####################################################################################
def sentiment_lightgbm(path_model_out="") :
"""
"""
data_name = "sentiment" ###in data/
model_name = 'LGBMClassifier'
n_sample = 1000
def post_process_fun(y):
return int(y)
def pre_process_fun(y):
return int(y)
#############################################################################
model_dict = {'model_pars': {'model_class': model_name
,'model_path': path_model_out
,'model_pars': {'objective': 'huber',
} # lightgbm one
,'post_process_fun': post_process_fun
,'pre_process_pars': {'y_norm_fun' : copy.deepcopy(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': ['root_mean_squared_error', 'mean_absolute_error', #### sklearm names
'explained_variance_score', 'r2_score', 'median_absolute_error']
},
'data_pars': {
'cols_input_type' : cols_input_type_1
#"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'
#,'colcat_bin'
#,'coltext'
]
,'filter_pars': { 'ymax' : 2 ,'ymin' : -1 } ### Filter data
}}
##### 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):
config_name = config if config is not None else config_default
mdict = globals()[config_name]()
m = mdict['global_pars']
print(mdict)
from source import run_preprocess, run_preprocess_old
run_preprocess.run_preprocess(config_name= config_name,
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):
config_name = config if config is not None else config_default
mdict = globals()[config_name]()
m = mdict['global_pars']
print(mdict)
from source import run_train
run_train.run_train(config_name= config_name,
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):
config_name = config if config is not None else config_default
mdict = globals()[config_name]()
m = mdict['global_pars']
print(mdict['data_pars']['cols_input_type'])
print(m)
from source import run_inference,run_inference
run_inference.run_predict(config_name,
path_model = m['path_model'],
path_data = m['path_data_test'],
path_output = m['path_output_pred'],
cols_group = mdict['data_pars']['cols_input_type'],
n_sample = nsample if nsample is not None else m['n_sample']
)
def run_all():
data_profile()
preprocess()
train()
check()
predict()
###########################################################################################################
###########################################################################################################
"""
python sentiment_classifier.py preprocess
python sentiment_classifier.py train
python sentiment_classifier.py check
python sentiment_classifier.py predict
python sentiment_classifier.py run_all
"""
if __name__ == "__main__":
import fire
fire.Fire()
def sentiment_elasticnetcv(path_model_out=""):
global model_name
model_name = 'ElasticNetCV'
def post_process_fun(y):
return y_norm(y, inverse=True, mode='boxcox')
def pre_process_fun(y):
return y_norm(y, inverse=False, mode='boxcox')
model_dict = {'model_pars': {'model_class': 'ElasticNetCV'
, 'model_path': path_model_out
, 'model_pars': {} # default ones
, 'post_process_fun': post_process_fun
, 'pre_process_pars': {'y_norm_fun' : pre_process_fun,
### Pipeline for data processing.
'pipe_list' : [ 'filter', 'label', 'dfnum_bin', 'dfnum_hot', 'dfcat_bin', 'dfcat_hot', 'dfcross_hot',
'dfdate', 'dftext'
]
}
},
'compute_pars': { 'metric_list': ['root_mean_squared_error', 'mean_absolute_error',
'explained_variance_score', 'r2_score', 'median_absolute_error']
},
'data_pars': {
'cols_input_type' : cols_input_type_1,
'cols_model_group': [ 'colnum_onehot', 'colcat_onehot', 'colcross_onehot' ]
,'cols_model': [] # cols['colcat_model'],
,'coly': [] # cols['coly']
,'filter_pars': { 'ymax' : 100000.0 ,'ymin' : 0.0 } ### Filter data
}}
return model_dict
def sentiment_bayesian_pyro(path_model_out="") :
global model_name
model_name = 'model_bayesian_pyro'
def post_process_fun(y):
return y_norm(y, inverse=True, mode='boxcox')
def pre_process_fun(y):
return y_norm(y, inverse=False, mode='boxcox')
model_dict = {'model_pars': {'model_class': 'model_bayesian_pyro'
, 'model_path': path_model_out
, 'model_pars': {'input_width': 112, } # default
, 'post_process_fun': post_process_fun
, 'pre_process_pars': {'y_norm_fun' : copy.deepcopy(pre_process_fun) ,
### Pipeline for data processing.
'pipe_list' : [ 'filter', 'label', 'dfnum_bin', 'dfnum_hot', 'dfcat_bin', 'dfcat_hot', 'dfcross_hot', ]
}
},
'compute_pars': {'compute_pars': {'n_iter': 1200, 'learning_rate': 0.01}
, 'metric_list': ['root_mean_squared_error', 'mean_absolute_error',
'explained_variance_score', 'r2_score', 'median_absolute_error']
, 'max_size': 1000000
, 'num_samples': 300
},
'data_pars': {
'cols_input_type' : cols_input_type_1
,'cols_model_group': [ 'colnum_onehot', 'colcat_onehot' ]
,'filter_pars': { 'ymax' : 100000.0 ,'ymin' : 0.0 } ### Filter data
}}
return model_dict