-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_creator.py
355 lines (259 loc) · 11 KB
/
feature_creator.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
# imports
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import OneHotEncoder
import pandas as pd
import numpy as np
from numpy import save
from numpy import load
from dateutil.parser import parse
import re
oneHotCutOut = 10
# read datasets
train_df = pd.read_csv('train_ml.csv', index_col=0)
test_df = pd.read_csv('test_ml.csv', index_col=0)
train_y = train_df[['updates', 'personal', 'promotions',
'forums', 'purchases', 'travel',
'spam', 'social']]
oneHotCutOut = 10
train_df = pd.read_csv('train_ml.csv', index_col=0)
test_df = pd.read_csv('test_ml.csv', index_col=0)
train_y = train_df[['updates', 'personal', 'promotions',
'forums', 'purchases', 'travel',
'spam', 'social']]
## ccs
data_field_name = 'ccs'
train_x = train_df[[data_field_name]]
train_x = train_x.fillna(value=0)
test_x = test_df[[data_field_name]]
test_x = test_x.fillna(value=0)
features_names= np.array(['ccs'])
S = StandardScaler()
train_x = S.fit_transform(train_x)
test_x = S.transform(test_x)
features_train = np.array( train_x)
features_test = np.array(test_x)
## images
data_field_name = 'images'
train_x = train_df[[data_field_name]]
train_x = train_x.fillna(value=0)
test_x = test_df[[data_field_name]]
test_x = test_x.fillna(value=0)
for i in range(train_x.shape[1]):
features_names = np.append(features_names,['images'])
features_train = np.concatenate((features_train, train_x), axis=1)
features_test = np.concatenate((features_test, test_x), axis=1)
## urls
data_field_name = 'urls'
train_x = train_df[[data_field_name]]
train_x = train_x.fillna(value=0)
test_x = test_df[[data_field_name]]
test_x = test_x.fillna(value=0)
for i in range(train_x.shape[1]):
features_names = np.append(features_names,['urls'])
features_train = np.concatenate((features_train, train_x), axis=1)
features_test = np.concatenate((features_test, test_x), axis=1)
## salutations
data_field_name = 'salutations'
train_x = train_df[[data_field_name]]
train_x = train_x.fillna(value=0)
test_x = test_df[[data_field_name]]
test_x = test_x.fillna(value=0)
for i in range(train_x.shape[1]):
features_names = np.append(features_names,['salutations'])
features_train = np.concatenate((features_train, train_x), axis=1)
features_test = np.concatenate((features_test, test_x), axis=1)
## bcced
data_field_name = 'bcced'
train_x = train_df[[data_field_name]]
train_x = train_x.fillna(value=0)
test_x = test_df[[data_field_name]]
test_x = test_x.fillna(value=0)
for i in range(train_x.shape[1]):
features_names = np.append(features_names,['bcced'])
features_train = np.concatenate((features_train, train_x), axis=1)
features_test = np.concatenate((features_test, test_x), axis=1)
## designation
data_field_name = 'designation'
train_x = train_df[[data_field_name]]
train_x = train_x.fillna(value=0)
test_x = test_df[[data_field_name]]
test_x = test_x.fillna(value=0)
for i in range(train_x.shape[1]):
features_names = np.append(features_names,['designation'])
features_train = np.concatenate((features_train, train_x), axis=1)
features_test = np.concatenate((features_test, test_x), axis=1)
## chars_in_subject
data_field_name = 'chars_in_subject'
train_x = train_df[[data_field_name]]
train_x = train_x.fillna(value=0)
test_x = test_df[[data_field_name]]
test_x = test_x.fillna(value=0)
for i in range(train_x.shape[1]):
features_names = np.append(features_names,['chars_in_subject'])
features_train = np.concatenate((features_train, train_x), axis=1)
features_test = np.concatenate((features_test, test_x), axis=1)
## chars_in_body
data_field_name = 'chars_in_body'
train_x = train_df[[data_field_name]]
train_x = train_x.fillna(value=0)
test_x = test_df[[data_field_name]]
test_x = test_x.fillna(value=0)
for i in range(train_x.shape[1]):
features_names = np.append(features_names,['chars_in_body'])
features_train = np.concatenate((features_train, train_x), axis=1)
features_test = np.concatenate((features_test, test_x), axis=1)
# date
test_x = test_df[['date']]
test_x = test_x.fillna(value='None')
test_x = np.array(test_x)
d = np.array(parse(test_x[0][0]).timetuple())
d = np.concatenate((d,np.array([0])), axis =None)
first = True
x = 0
for row in test_x:
if first==True:
first = False
else:
if (len(row[0].split()[-1]) == 5):
if(row[0].split()[-1][-1] == ')'):
timezone = int(row[0].split()[-2][3:])+int(row[0].split()[-2][:3])*60
else:
timezone = int(row[0].split()[-1][3:])+int(row[0].split()[-1][:3])*60
else:
if (len(row[0].split()[-2]) == 5):
timezone = int(row[0].split()[-2][3:])+int(row[0].split()[-2][:3])*60
else:
timezone = 0
row = np.array(parse(re.sub(' +', ' ', row[0])[:31]).timetuple())
row = np.concatenate([row, np.array([timezone])])
d = np.vstack([d, row])
features_test = np.concatenate((features_test, np.array(d)), axis=1)
train_x = train_df[['date']]
train_x = train_x.fillna(value='None')
train_x = np.array(train_x)
d = np.array(parse(train_x[0][0]).timetuple())
d = np.concatenate((d,np.array([0])), axis =None)
first = True
for row in train_x:
if first==True:
first = False
else:
if (len(row[0].split()[-1]) == 5):
if(row[0].split()[-1][-1] == ')'):
timezone = int(row[0].split()[-2][3:])+int(row[0].split()[-2][:3])*60
else:
timezone = int(row[0].split()[-1][3:])+int(row[0].split()[-1][:3])*60
else:
if (len(row[0].split()[-2]) == 5):
timezone = int(row[0].split()[-2][3:])+int(row[0].split()[-2][:3])*60
else:
timezone = 0
row = np.array(parse(re.sub(' +', ' ', row[0])[:31]).timetuple())
row = np.concatenate([row, np.array([timezone])])
d = np.vstack([d, row])
for i in range(d.shape[1]):
features_names = np.append(features_names,['date'])
features_train = np.concatenate((features_train, np.array(d)), axis=1)
train_x = np.divide(features_train[:,1],features_train[:,7]).T.reshape((39671,1))
test_x = np.divide(features_test[:,1],features_test[:,7]).T.reshape((17002,1))
S = StandardScaler()
train_x = S.fit_transform(train_x)
test_x = S.transform(test_x)
features_train = np.hstack([features_train, train_x])
features_test = np.hstack([features_test,test_x])
features_names = np.append(features_names,['ratioImageBody'])
train_x = np.divide(features_train[:,2],features_train[:,7]).T.reshape((39671,1))
test_x = np.divide(features_test[:,2],features_test[:,7]).T.reshape((17002,1))
S = StandardScaler()
train_x = S.fit_transform(train_x)
test_x = S.transform(test_x)
features_train = np.hstack([features_train, train_x])
features_test = np.hstack([features_test,test_x])
features_names = np.append(features_names,['ratioUrlBody'])
train_x = np.divide(features_train[:,6],features_train[:,7]).T.reshape((39671,1))
test_x = np.divide(features_test[:,6],features_test[:,7]).T.reshape((17002,1))
S = StandardScaler()
train_x = S.fit_transform(train_x)
test_x = S.transform(test_x)
features_train = np.hstack([features_train, train_x])
features_test = np.hstack([features_test,test_x])
features_names = np.append(features_names,['ratioSubjectBody'])
train_x = np.multiply(features_train[:,3],features_train[:,4]).T.reshape((39671,1))
test_x = np.multiply(features_test[:,3],features_test[:,4]).T.reshape((17002,1))
S = StandardScaler()
train_x = S.fit_transform(train_x)
test_x = S.transform(test_x)
features_train = np.hstack([features_train, train_x])
features_test = np.hstack([features_test,test_x])
features_names = np.append(features_names,['ratioSubjectBody'])
##Correcting the data
train_x = np.array(features_train[:,[1,2,6,7]])
test_x = np.array(features_test[:,[1,2,6,7]])
S = StandardScaler()
train_x = S.fit_transform(train_x)
test_x = S.transform(test_x)
features_train[:,[1,2,6,7]] = train_x
features_test[:,[1,2,6,7]] = test_x
##Correcting the data
train_x = np.array(features_train[:,[8,9,10,11,12,13,14,15,17]])
test_x = np.array(features_test[:,[8,9,10,11,12,13,14,15,17]])
S = StandardScaler()
train_x = S.fit_transform(train_x)
test_x = S.transform(test_x)
features_train[:,[8,9,10,11,12,13,14,15,17]] = train_x
features_test[:,[8,9,10,11,12,13,14,15,17]] = test_x
features_train = np.delete(features_train, 13, 1)
features_test = np.delete(features_test, 13, 1)
##mail_type
train_x = train_df[['mail_type']]
train_x = train_x.fillna(value='None')
test_x = test_df[['mail_type']]
test_x = test_x.fillna(value='None')
feat_enc = OneHotEncoder()
feat_enc.fit(np.vstack([train_x, test_x]))
train_x_featurized = feat_enc.transform(train_x).toarray()
test_x_featurized = feat_enc.transform(test_x).toarray()
one_test = np.array(train_x_featurized.sum(axis=0))
test_x_featurized = np.array(test_x_featurized).T[np.where(one_test>5)].T
train_x_featurized = np.array(train_x_featurized).T[np.where(one_test>5)].T
for i in range(test_x_featurized.shape[1]):
features_names = np.append(features_names,['mail_type'])
features_train = np.concatenate((features_train, np.array(train_x_featurized)), axis=1)
features_test = np.concatenate((features_test, np.array(test_x_featurized)), axis=1)
## ORG
data_field_name = 'org'
train_x = train_df[[data_field_name]]
train_x = train_x.fillna(value='None')
test_x = test_df[[data_field_name]]
test_x = test_x.fillna(value='None')
feat_enc = OneHotEncoder()
feat_enc.fit(np.vstack([train_x, test_x]))
train_x_featurized = feat_enc.transform(train_x).toarray()
test_x_featurized = feat_enc.transform(test_x).toarray()
one_test = np.array(train_x_featurized.sum(axis=0))
test_x_featurized = np.array(test_x_featurized).T[np.where(one_test>10)].T
train_x_featurized = np.array(train_x_featurized).T[np.where(one_test>10)].T
for i in range(test_x_featurized.shape[1]):
features_names = np.append(features_names,['Org'])
features_train = np.concatenate((features_train, np.array(train_x_featurized)), axis=1)
features_test = np.concatenate((features_test, np.array(test_x_featurized)), axis=1)
## TLD
data_field_name = 'tld'
train_x = train_df[[data_field_name]]
train_x = train_x.fillna(value='None')
test_x = test_df[[data_field_name]]
test_x = test_x.fillna(value='None')
feat_enc = OneHotEncoder()
feat_enc.fit(np.vstack([train_x, test_x]))
train_x_featurized = feat_enc.transform(train_x).toarray()
test_x_featurized = feat_enc.transform(test_x).toarray()
one_test = np.array(train_x_featurized.sum(axis=0))
train_x_featurized = np.array(train_x_featurized).T[np.where(one_test>5)].T
test_x_featurized = np.array(test_x_featurized).T[np.where(one_test>5)].T
features_train = np.concatenate((features_train, np.array(train_x_featurized)), axis=1)
features_test = np.concatenate((features_test, np.array(test_x_featurized)), axis=1)
print("Finished Data preprocessing and feature engineering!")
print("Features shape"+str(features_train.shape))
save('features/features_train.npy', features_train)
save('features/features_test.npy', features_test)
save('features/train_y.npy',train_y)