-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgroup_imp.py
272 lines (195 loc) · 9.76 KB
/
group_imp.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
from sklearn.ensemble import AdaBoostClassifier, VotingClassifier
from xgboost import XGBClassifier
from sklearn.tree import DecisionTreeClassifier
from features.pos_tags_features import POSTagFeatures
from features.sentence_struct_features import SentenceStructureFeatures
from features.sentence_sentm_features import SentenceSentimentFeatures
from features.original_features import OriginalFeatures
from features.sentence_word_emb import GloVeFeatures
from utils.data_preprocess import generate_final_training_dataset, get_train_test_scores
from logger import Logger
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score
from sklearn.metrics import roc_auc_score, mean_squared_error
import numpy as np
import json
logger = Logger(show = True, html_output = True, config_file = "config.txt")
comm_cols = ['Click_Bait', 'ID']
pos_cols = POSTagFeatures(logger).end_computing_features()
sent_cols = SentenceSentimentFeatures(logger).end_computing_features()
struct_cols = SentenceStructureFeatures(logger).end_computing_features()
sw_cph_cols = struct_cols[2:4]
struct_cols = struct_cols[:2] + struct_cols[4:]
emb_cols = GloVeFeatures(logger).end_computing_features()
orig_cols = OriginalFeatures(logger).end_computing_features()
def get_only_original(df):
return df[orig_cols + comm_cols]
def get_only_emb(df):
return df[emb_cols + comm_cols]
def get_only_struct(df):
return df[struct_cols + comm_cols]
def get_only_sent(df):
return df[sent_cols + comm_cols]
def get_only_sw_cph(df):
return df[sw_cph_cols + comm_cols]
def get_only_pos(df):
return df[pos_cols + comm_cols]
def get_without_orig(df):
return df.drop(orig_cols, axis=1)
def get_without_emb(df):
return df.drop(emb_cols, axis=1)
def get_without_sent(df):
return df.drop(sent_cols, axis=1)
def get_without_struct(df):
return df.drop(struct_cols, axis=1)
def get_without_sw_cph(df):
return df.drop(sw_cph_cols, axis=1)
def get_without_pos(df):
return df.drop(pos_cols, axis=1)
def evaluate_model(model, X_test, y_true, y_score):
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)
y_prob = np.array([elem[1] for elem in y_prob])
logger.log("Scores snippet {}".format(y_prob[:10]))
logger.log("Accuracy {}".format(accuracy_score(y_true, y_pred)))
logger.log("Precision {}".format(precision_score(y_true, y_pred)))
logger.log("Recall {}".format(recall_score(y_true, y_pred)))
logger.log("F1 {}".format(f1_score(y_true, y_pred)))
logger.log("MSE {}".format(mean_squared_error(y_score, y_prob)))
logger.log("AUC {}".format(roc_auc_score(y_true, y_prob, max_fpr = 0.3)))
def load_architecture():
ada_params_filename = logger.config_dict['BEST_ADA_L']
logger.log("Loading params for ADA from {} ...".format(ada_params_filename))
with open(logger.get_model_file(ada_params_filename, "large")) as fp:
ada_best_params = json.load(fp)
ada_model = AdaBoostClassifier(DecisionTreeClassifier())
ada_model.set_params(**ada_best_params)
xgb_params_filename = logger.config_dict['BEST_XGB_L']
logger.log("Loading params for XGB from {} ...".format(xgb_params_filename))
with open(logger.get_model_file(xgb_params_filename, "large")) as fp:
xgb_best_params = json.load(fp)
xgb_model = XGBClassifier()
xgb_model.set_params(**xgb_best_params)
ensemble_weights = [0.5, 0.5]
comb_model = VotingClassifier(estimators = [('ADA', ada_model), ('XGB', xgb_model)],
voting='soft', weights = ensemble_weights, n_jobs = -1)
logger.log("Finish loading best architecture {}".format(comb_model))
return comb_model
CBAIT_SAMP_W = 3.0
if __name__ == '__main__':
all_df = generate_final_training_dataset("large", logger)
crt_model = load_architecture()
crt_df = all_df
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training on all features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_only_original(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training on only original features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_only_sent(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training on only sentiment features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_only_struct(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training on only structure features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_only_emb(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training on only embedding features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_only_sw_cph(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training on only stopW + cBaitPhr features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_only_pos(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training on only pos features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_without_orig(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training without original features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_without_struct(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training without structure features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_without_sent(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training without sentiment features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_without_emb(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training without embedding features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_without_pos(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training without pos features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)
logger.log("")
crt_model = load_architecture()
crt_df = get_without_sw_cph(all_df)
X_train, y_train, X_test, y_test, y_score = get_train_test_scores(crt_df,
test_size = 0.2)
sample_weight = [CBAIT_SAMP_W if int(sample) == 1 else 1 for sample in y_train]
logger.log("Training without stopW + cBaitPhr features ... {}".format(crt_df.columns.values.tolist()))
crt_model.fit(X_train, y_train, sample_weight = sample_weight)
evaluate_model(crt_model, X_test, y_test, y_score)