-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_models.py
395 lines (322 loc) · 14.8 KB
/
gen_models.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
import pandas as pd
import numpy as np
import pickle
import matplotlib.pyplot as plt
import seaborn
import string
from IPython.display import display
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import learning_curve
from sklearn.decomposition import TruncatedSVD
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.svm import SVC
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import AdaBoostClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.neighbors import NearestNeighbors
from sklearn.neighbors import NearestCentroid
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.tree import DecisionTreeClassifier
import sklearn.gaussian_process.kernels as kernels
from sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import KFold
from sklearn.pipeline import Pipeline
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from scipy.stats import expon
from gen_features import *
def train_model(clf, param_grid, X, Y):
'''Trains and evaluates the model clf from input
The function selects the best model of clf by optimizing for the validation data,
then evaluates its performance using the out of sample test data.
input - clf: the model to train
param_grid: a dict of hyperparameters to use for optimization
X: features
Y: labels
output - the best estimator (trained model)
the confusion matrix from classifying the test data
'''
#First, partition into train and test data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
n_iter = 5
#If number of possible iterations are less than prefered number of iterations,
#set it to the number of possible iterations
#number of possible iterations are not less than prefered number of iterations if any argument is expon()
#because expon() is continous (writing 100 instead, could be any large number)
n_iter = min(n_iter,np.prod([
100 if type(xs) == type(expon())
else len(xs)
for xs in param_grid.values()
]))
#perform a grid search for the best parameters on the training data.
#Cross validation is made to select the parameters, so the training data is actually split into
#a new train data set and a validation data set, K number of times
cvv = ShuffleSplit(n_splits=5, test_size=0.2, random_state=0) #DEBUG: n_iter=10
cv = cvv.split(X_train)
#cv = KFold(n=len(X), n_folds=10)
random_grid_search = RandomizedSearchCV(
clf,
param_distributions=param_grid,
cv=cv,
scoring='f1',
n_iter=n_iter, #DEBUG 1
random_state=5,
refit=True,
verbose=10,
n_jobs=-1 # modify
)
'''Randomized search used instead. We have limited computing power
grid_search = GridSearchCV(
clf,
param_grid=param_grid,
cv=cv,
scoring='f1', #accuracy/f1/f1_weighted all give same result?
verbose=10,
n_jobs=-1
)
grid_search.fit(X_train, Y_train)
'''
random_grid_search.fit(X_train, Y_train)
#Evaluate the best model on the test data
Y_test_predicted = random_grid_search.best_estimator_.predict(X_test)
Y_test_predicted_prob = random_grid_search.best_estimator_.predict_proba(X_test)[:, 1]
confusion = confusion_matrix(Y_test, Y_test_predicted)
TP = confusion[1, 1]
TN = confusion[0, 0]
FP = confusion[0, 1]
FN = confusion[1, 0]
#Calculate recall (sensitivity) from confusion matrix
sensitivity = TP / float(TP + FN)
#Calculate specificity from confusion matrix
specificity = TN / float(TN + FP)
#Calculate accuracy
accuracy = (confusion[0][0] + confusion[1][1]) / (confusion.sum().sum())
#Calculate axes of ROC curve
fpr, tpr, thresholds = roc_curve(Y_test, Y_test_predicted_prob)
#Area under the ROC curve
auc = roc_auc_score(Y_test, Y_test_predicted_prob)
return {
'conf_matrix':confusion,
'accuracy':accuracy,
'sensitivity':sensitivity,
'specificity':specificity,
'auc':auc,
'params':random_grid_search.best_params_,
'model':random_grid_search.best_estimator_,
'roc':{'fpr':fpr,'tpr':tpr,'thresholds':thresholds}
}
def create_classifier_inputs_using_vectorizers(vectorizer, subscript):
'''make pipelines of the specified vectorizer with the classifiers to train
input - vectorizer: the vectorizer to add to the pipelines
subscript: subscript name for the dictionary key
output - A dict of inputs to use for train_model(); a pipeline and a dict of params to optimize
'''
classifier_inputs = {}
classifier_inputs[subscript + ' MLPClassifier'] = {
'pipeline':Pipeline([('vect', vectorizer),('clf',MLPClassifier(
activation='relu',
solver='adam',
early_stopping=False,
verbose=True
))]),
'dict_params': {
'vect__min_df':[1,2,5,10,20,40],
'clf__hidden_layer_sizes':[(500,250,125,62)],
'clf__alpha':[0.0005,0.001,0.01,0.1,1],
'clf__learning_rate':['constant','invscaling'],
'clf__learning_rate_init':[0.001,0.01,0.1,1],
'clf__momentum':[0,0.9],
}
}
classifier_inputs[subscript + ' MultinomialNB'] = {
'pipeline':Pipeline([('vect', vectorizer),('clf',MultinomialNB())]),
'dict_params': {
'vect__min_df':[1,2,5,10,20,40]
}
}
classifier_inputs[subscript + ' RandomForest'] = {
'pipeline':Pipeline([('vect', vectorizer),('clf',RandomForestClassifier(
max_depth=None,min_samples_split=2, random_state=0))]),
'dict_params': {
'vect__min_df':[1,2,5,10,20,40],
'clf__n_estimators':[10,20,40,60]
}
}
classifier_inputs[subscript + ' Logistic'] = {
'pipeline':Pipeline([('vect', vectorizer), ('clf',LogisticRegression())]),
'dict_params': {
'vect__min_df':[1,2,5,10,20,40],
'clf__C':[0.001, 0.01, 0.1, 1, 10, 100, 1000]
}
}
classifier_inputs[subscript + ' SVM'] = {
'pipeline':Pipeline([('vect', vectorizer), ('clf',SVC(probability=True))]),
'dict_params': {
'vect__min_df':[1,2,5,10,20,40],
'clf__C':[0.001, 0.01, 0.1, 1, 10, 100, 1000],
'clf__gamma':[0.001, 0.0001,'auto'],
'clf__kernel':['rbf']
}
}
return classifier_inputs
def create_classifier_inputs(subscript):
classifier_inputs = {}
'''classifier_inputs[subscript + ' GPC'] = {
'pipeline':GaussianProcessClassifier(),
'dict_params': {
'kernel':[
1.0*kernels.RBF(1.0),
1.0*kernels.Matern(),
1.0*kernels.RationalQuadratic(),
1.0*kernels.DotProduct()
]
}
}'''
classifier_inputs[subscript + ' AdaBoostClassifier'] = {
'pipeline':AdaBoostClassifier(n_estimators=100),
'dict_params': {
'n_estimators':[10,20,50, 100],
'learning_rate':[0.1, 0.5, 1.0, 2.0]
}
}
classifier_inputs[subscript + ' SGD'] = {
'pipeline':SGDClassifier(loss="log", penalty="l2"),
'dict_params': {
'learning_rate': ['optimal']
}
}
classifier_inputs[subscript + ' RandomForest'] = {
'pipeline':RandomForestClassifier(
max_depth=None,min_samples_split=2, random_state=0),
'dict_params': {
'n_estimators':[10,20,40,60]
}
}
classifier_inputs[subscript + ' DecisionTree'] = {
'pipeline': DecisionTreeClassifier(max_depth=5),
'dict_params': {
'min_samples_split': [2]
}
}
classifier_inputs[subscript + ' MLPClassifier'] = {
'pipeline':MLPClassifier(
activation='relu',
solver='adam',
early_stopping=False,
verbose=True
),
'dict_params': {
'hidden_layer_sizes':[(300, 200, 150, 150), (30, 30, 30), (150, 30, 30, 150),
(400, 250, 100, 100) , (150, 200, 300)],
'alpha':[0.0005,0.001,0.01,0.1,1],
'learning_rate':['constant','invscaling'],
'learning_rate_init':[0.0005,0.001,0.01,0.1,1],
'momentum':[0,0.9],
}
}
classifier_inputs[subscript + ' Logistic'] = {
'pipeline':LogisticRegression(),
'dict_params': {
'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000]
}
}
classifier_inputs[subscript + ' MultinomialNB'] = {
'pipeline':MultinomialNB(),
'dict_params': {
'alpha': [1.0]
}
}
classifier_inputs[subscript + ' SVM'] = {
'pipeline':SVC(probability=True),
'dict_params': {
'C':[0.001, 0.01, 0.1, 1, 10, 100, 1000],
'gamma':[0.001, 0.0001,'auto'],
'kernel':['rbf']
}
}
return classifier_inputs
if __name__ == "__main__":
classifier_results = pd.DataFrame(columns=['accuracy','sensitivity','specificity','auc','conf_matrix','params','model','roc'])#,index=classifier_inputs.keys())
# Don't try to run this, it will take several days to complete
classifier_inputs = {}
classifier_inputs.update(create_classifier_inputs_using_vectorizers(count_vectorizer_1grams,'count 1grams'))
classifier_inputs.update(create_classifier_inputs_using_vectorizers(count_vectorizer_2grams,'count 2grams'))
classifier_inputs.update(create_classifier_inputs_using_vectorizers(count_vectorizer_3grams,'count 3grams'))
classifier_inputs.update(create_classifier_inputs_using_vectorizers(tfidf_vectorizer_1grams,'tfidf 1grams'))
classifier_inputs.update(create_classifier_inputs_using_vectorizers(tfidf_vectorizer_2grams,'tfidf 2grams'))
classifier_inputs.update(create_classifier_inputs_using_vectorizers(tfidf_vectorizer_3grams,'tfidf 3grams'))
X = payloads['payload']
Y = payloads['is_malicious']
for classifier_name, inputs in classifier_inputs.items():
display(inputs['dict_params'])
if classifier_name in classifier_results.index.values.tolist():
print('Skipping ' + classifier_name + ', already trained')
else:
result_dict = train_model(inputs['pipeline'],inputs['dict_params'],X,Y)
classifier_results.loc[classifier_name] = result_dict
display(classifier_results)
display(pd.DataFrame(payloads['payload'].copy()))
#Save classifiers in a pickle file to be able to re-use them without re-training
pickle.dump( classifier_results, open( "data/trained_classifiers.p", "wb" ) )
classifier_inputs_custom = {}
#Get classifiers and parameters to optimize
classifier_inputs_custom.update(create_classifier_inputs('custom'))
#Extract payloads and labels
Y = payloads['is_malicious']
X = create_features(pd.DataFrame(payloads['payload'].copy()))
#Select the best features
X_new = SelectKBest(score_func=chi2, k=4).fit_transform(X,Y)
Call train_model for every classifier and save results to classifier_results
for classifier_name, inputs in classifier_inputs_custom.items():
if classifier_name in classifier_results.index.values.tolist():
print('Skipping ' + classifier_name + ', already trained')
else:
result_dict = train_model(inputs['pipeline'],inputs['dict_params'],X,Y)
classifier_results.loc[classifier_name] = result_dict
display(classifier_results)
pickle.dump( classifier_results, open( "data/trained_classifiers_custom_all_features.p", "wb" ) )
# Classifier results
#Display the results for the classifiers that were trained using our custom feature space
custom_features_classifiers = pickle.load( open("data/trained_classifiers_custom_all_features.p", "rb"))
display(custom_features_classifiers)
#Display the results for the classifiers that were using bag of words feature spaces
classifier_results = pickle.load( open( "data/trained_classifiers.p", "rb" ) )
display(classifier_results)
#Combine the two tables into one table
# classifier_results = classifier_results.append(custom_features_classifiers)
classifier_results = custom_features_classifiers.sort_values(['sensitivity','accuracy'], ascending=[False,False])
display(classifier_results)
def f1_score(conf_matrix):
precision = conf_matrix[0][0] / (conf_matrix[0][0] + conf_matrix[0][1] )
recall = conf_matrix[0][0] / (conf_matrix[0][0] + conf_matrix[1][0] )
return (2 * precision * recall) / (precision + recall)
#load classifier table if not yet loaded
# classifier_results = pickle.load( open( "data/trained_classifiers.p", "rb" ) )
#Calculate F1-scores
classifier_results['F1-score'] = [ f1_score(conf_matrix) for conf_matrix in classifier_results['conf_matrix']]
#Re-arrange columns
classifier_results = classifier_results[['F1-score','accuracy','sensitivity','specificity','auc','conf_matrix','params','model','roc']]
#re-sort on F1-score
classifier_results = classifier_results.sort_values(['F1-score','accuracy'], ascending=[False,False])
display(classifier_results)
classifier_results[['F1-score','accuracy','sensitivity','specificity','auc']] = classifier_results[['F1-score','accuracy','sensitivity','specificity','auc']].apply(pd.to_numeric)
classifier_results = classifier_results.round({'F1-score':4,'accuracy':4,'sensitivity':4,'specificity':4,'auc':4})
#classifier_results[['F1-score','accuracy','sensitivity','specificity','auc','conf_matrix','params']].to_csv('data/classifiers_result_table.csv')
display(classifier_results.dtypes)
#save complete list of classifiers to 'trained_classifiers'
pickle.dump( classifier_results, open( "data/trained_classifiers_add_F1_score.p", "wb" ) )
#In this case, we are going to implement tfidf 2grams RandomForest in our dummy server
classifier = (custom_features_classifiers['model'].iloc[0])
print(classifier)
#Save classifiers in a pickle file to be able to re-use them without re-training
pickle.dump( classifier, open( "data/tfidf_2grams_randomforest.p", "wb" ) )