-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_implementation_and_result_analysis_on_preprocessed_data_.py
463 lines (308 loc) · 13.8 KB
/
models_implementation_and_result_analysis_on_preprocessed_data_.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# -*- coding: utf-8 -*-
"""Models Implementation and Result Analysis on Preprocessed Data .ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1f-RIegN9veMO8czs32C7dUt9dt6YniEJ
# This Notebook starts from Preprocessed Dataframework. "df_spark.csv" is the dataframe
"""
from google.colab import drive
drive.mount('/content/drive')
import pandas as pd
df_spark = pd.read_csv('/content/drive/My Drive/Code/df_spark.csv')
df_spark.head()
df_spark = df_spark.drop(columns="Unnamed: 0")
df_spark.head()
"""# In the following code X contains features and y contains label"""
y = df_spark.iloc[:,0].values
X = df_spark.iloc[:,1:].values
"""# The whole dataset is split into 80:20 ratio. X_train contains 80% of the features, X_test contains 20% of the features and y_train contains 80% corresponding label of X_train and y_test contains 20% corresponding label of X_test"""
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.20, random_state = 1)
print(X_train.shape)
print(X_train[0])
print(X_test.shape)
print(X_test)
print(y_train.shape)
print(y_train)
print(y_test.shape)
print(y_test)
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
from sklearn.model_selection import learning_curve
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics
"""# 5-Fold Cross validation Estimation for KNN"""
pipe_knn = Pipeline([('scl', StandardScaler()),('clf', KNeighborsClassifier(n_neighbors= 1))])
train_sizes, train_scores, test_scores = learning_curve(estimator=pipe_knn,X=X, y=y, train_sizes=np.linspace(0.2,1.0,5), cv=5, n_jobs=-1)
train_mean = np.mean(train_scores, axis=1)
train_std = np.std(train_scores, axis=1)
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)
for i in train_sizes:
print(i)
for i in train_mean:
print(i)
for i in test_mean:
print(i)
"""# Evaluation Metrics Calculations for KNN"""
pipe_knn = pipe_knn.fit(X_train, y_train)
y_pred_train = pipe_knn.predict(X_train)
y_pred_test = pipe_knn.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy_score(y_train, y_pred_train)
accuracy_score(y_test, y_pred_test)
from sklearn.metrics import classification_report
target_names = ['Normal', 'DoSattack', 'scan', 'malitiousControl', 'malitiousOperation', 'spying', 'dataProbing', 'wrongSetUp']
print(classification_report(y_train, y_pred_train, target_names=target_names))
print(classification_report(y_test, y_pred_test, target_names=target_names))
from sklearn.metrics import confusion_matrix
import itertools
cnf_matrix = confusion_matrix(y_test, y_pred_test)
for i in cnf_matrix:
for j in i:
print(j, end=' ')
print()
"""# 5-Fold Cross validation Estimation for Gaussian Naive Bayes"""
from sklearn.naive_bayes import GaussianNB
pipe_gnb = Pipeline([('scl', StandardScaler()),('clf', GaussianNB(priors=None, var_smoothing=1e-09))])
#pipe_lr = Pipeline([('scl', StandardScaler()),('clf', LogisticRegression(penalty='l2', random_state=0))])
train_sizes, train_scores, test_scores = learning_curve(estimator=pipe_gnb,X=X, y=y, train_sizes=np.linspace(0.2,1.0,5), cv=5, n_jobs=-1)
train_mean = np.mean(train_scores, axis=1)
train_std = np.std(train_scores, axis=1)
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)
for i in train_sizes:
print(i)
for i in train_mean:
print(i)
for i in test_mean:
print(i)
"""# Evaluation Metrics Calculations for Gaussian Naive Bayes"""
pipe_gnb = pipe_gnb.fit(X_train, y_train)
y_pred_train = pipe_gnb.predict(X_train)
y_pred_test = pipe_gnb.predict(X_test)
accuracy_score(y_train, y_pred_train)
accuracy_score(y_test, y_pred_test)
print(classification_report(y_train, y_pred_train, target_names=target_names))
print(classification_report(y_test, y_pred_test, target_names=target_names))
cnf_matrix = confusion_matrix(y_test, y_pred_test)
for i in cnf_matrix:
for j in i:
print(j,end=' ')
print()
"""# 5-Fold Cross validation Estimation for Logistic Regression"""
pipe_lr = Pipeline([('scl', StandardScaler()),('clf', LogisticRegression(penalty='l2', random_state=0))])
train_sizes, train_scores, test_scores = learning_curve(estimator=pipe_lr,X=X, y=y, train_sizes=np.linspace(0.2,1.0,5), cv=5, n_jobs=-1)
train_mean = np.mean(train_scores, axis=1)
train_std = np.std(train_scores, axis=1)
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)
for i in train_sizes:
print(i)
for i in train_mean:
print(i)
for i in test_mean:
print(i)
"""# 5-Fold Cross validation Estimation for SVM"""
from sklearn.svm import LinearSVC
pipe_svc = Pipeline([('scl', StandardScaler()),('clf', LinearSVC())])
train_sizes_svc, train_scores_svc, test_scores_svc = learning_curve(estimator=pipe_svc,X=X, y=y, train_sizes=np.linspace(0.2,1.0,5), cv=5, n_jobs=-1)
train_mean_svc = np.mean(train_scores_svc, axis=1)
train_std_svc = np.std(train_scores_svc, axis=1)
test_mean_svc = np.mean(test_scores_svc, axis=1)
test_std_svc = np.std(test_scores_svc, axis=1)
for i in train_mean_svc:
print(i)
for i in test_mean_svc:
print(i)
"""# 5-Fold Cross validation Estimation for Decision Tree"""
from sklearn import tree
pipe_tree = Pipeline([('scl', StandardScaler()),('clf', tree.DecisionTreeClassifier())])
train_sizes_tree, train_scores_tree, test_scores_tree = learning_curve(estimator=pipe_tree,X=X, y=y, train_sizes=np.linspace(0.2,1.0,5), cv=5, n_jobs=-1)
train_mean_tree = np.mean(train_scores_tree, axis=1)
train_std_tree = np.std(train_scores_tree, axis=1)
test_mean_tree = np.mean(test_scores_tree, axis=1)
test_std_tree = np.std(test_scores_tree, axis=1)
for i in train_mean_tree:
print(i)
for i in test_mean_tree:
print(i)
"""# 5-Fold Cross validation Estimation for Random Forest"""
from sklearn.ensemble import RandomForestClassifier
pipe_rnd = Pipeline([('scl', StandardScaler()),('clf', RandomForestClassifier(n_estimators=10))])
train_sizes_rnd, train_scores_rnd, test_scores_rnd = learning_curve(estimator=pipe_rnd,X=X, y=y, train_sizes=np.linspace(0.2,1.0,5), cv=5, n_jobs=-1)
train_mean_rnd = np.mean(train_scores_rnd, axis=1)
train_std_rnd = np.std(train_scores_rnd, axis=1)
test_mean_rnd = np.mean(test_scores_rnd, axis=1)
test_std_rnd = np.std(test_scores_rnd, axis=1)
for i in train_mean_rnd:
print(i)
for i in test_mean_rnd:
print(i)
"""# 5-Fold Cross validation Estimation for ANN"""
from sklearn.neural_network import MLPClassifier
mlp = MLPClassifier(hidden_layer_sizes=(50,), max_iter=10, alpha=1e-4, solver='sgd', verbose=10, tol=1e-4, random_state=1, learning_rate_init=.1)
pipe_mlp = Pipeline([('scl', StandardScaler()),('clf', mlp)])
train_sizes_mlp, train_scores_mlp, test_scores_mlp = learning_curve(estimator=pipe_mlp,X=X, y=y, train_sizes=np.linspace(0.2,1.0,5), cv=5, n_jobs=-1)
train_mean_mlp = np.mean(train_scores_mlp, axis=1)
train_std_mlp = np.std(train_scores_mlp, axis=1)
test_mean_mlp = np.mean(test_scores_mlp, axis=1)
test_std_mlp = np.std(test_scores_mlp, axis=1)
for i in train_mean_mlp:
print(i)
for i in test_mean_mlp:
print(i)
"""# Mean values of Training and Testing accuracies and Standard Deviation of Training and Testing accuracies are given below"""
np.mean(train_mean) , np.mean(train_mean_svc), np.mean(train_mean_tree), np.mean(train_mean_rnd), np.mean(train_mean_mlp)
np.mean(train_std) , np.mean(train_std_svc), np.mean(train_std_tree), np.mean(train_std_rnd), np.mean(train_std_mlp)
np.mean(test_mean) , np.mean(test_mean_svc), np.mean(test_mean_tree), np.mean(test_mean_rnd), np.mean(test_mean_mlp)
np.mean(test_std) , np.mean(test_std_svc), np.mean(test_std_tree), np.mean(test_std_rnd), np.mean(test_std_mlp)
"""# Evaluation Metrics Calculations for Logisitic Regression"""
pipe_lr = pipe_lr.fit(X_train, y_train)
y_pred_train = pipe_lr.predict(X_train)
y_pred_test = pipe_lr.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy_score(y_train, y_pred_train)
accuracy_score(y_test, y_pred_test)
from sklearn.metrics import classification_report
target_names = ['Normal', 'DoSattack', 'scan', 'malitiousControl', 'malitiousOperation', 'spying', 'dataProbing', 'wrongSetUp']
print(classification_report(y_train, y_pred_train, target_names=target_names))
print(classification_report(y_test, y_pred_test, target_names=target_names))
from sklearn.metrics import confusion_matrix
import itertools
cnf_matrix = confusion_matrix(y_test, y_pred_test)
for i in cnf_matrix:
for j in i:
print(j, end=' ')
print()
"""# Evaluation Metrics Calculations for SVM"""
pipe_svc = pipe_svc.fit(X_train, y_train)
y_pred_train = pipe_svc.predict(X_train)
y_pred_test = pipe_svc.predict(X_test)
accuracy_score(y_train, y_pred_train), accuracy_score(y_test, y_pred_test)
print(classification_report(y_train, y_pred_train, target_names=target_names))
print(classification_report(y_test, y_pred_test, target_names=target_names))
cnf_matrix = confusion_matrix(y_test, y_pred_test)
for i in cnf_matrix:
for j in i:
print(j, end=' ')
print()
"""# Evaluation Metrics Calculations for Decision Tree"""
pipe_tree = pipe_tree.fit(X_train, y_train)
y_pred_train = pipe_tree.predict(X_train)
y_pred_test = pipe_tree.predict(X_test)
accuracy_score(y_train, y_pred_train), accuracy_score(y_test, y_pred_test)
print(classification_report(y_train, y_pred_train, target_names=target_names))
print(classification_report(y_test, y_pred_test, target_names=target_names))
cnf_matrix = confusion_matrix(y_test, y_pred_test)
for i in cnf_matrix:
for j in i:
print(j, end=' ')
print()
"""# Evaluation Metrics Calculations for Random Forest"""
pipe_rnd = pipe_rnd.fit(X_train, y_train)
y_pred_train = pipe_rnd.predict(X_train)
y_pred_test = pipe_rnd.predict(X_test)
y_pred_train = pipe_rnd.predict(X_train)
y_pred_test = pipe_rnd.predict(X_test)
accuracy_score(y_train, y_pred_train), accuracy_score(y_test, y_pred_test)
print(classification_report(y_train, y_pred_train, target_names=target_names))
print(classification_report(y_test, y_pred_test, target_names=target_names))
cnf_matrix = confusion_matrix(y_test, y_pred_test)
for i in cnf_matrix:
for j in i:
print(j, end=' ')
print()
"""# Evaluation Metrics Calculations for ANN"""
pipe_mlp = pipe_mlp.fit(X_train, y_train)
y_pred_train = pipe_mlp.predict(X_train)
y_pred_test = pipe_mlp.predict(X_test)
accuracy_score(y_train, y_pred_train), accuracy_score(y_test, y_pred_test)
print(classification_report(y_train, y_pred_train, target_names=target_names))
import pickle
pickle.dump(pipe_mlp,open('MLP.sav', 'wb'))
print(classification_report(y_test, y_pred_test, target_names=target_names))
from sklearn.metrics import confusion_matrix
import itertools
cnf_matrix = confusion_matrix(y_test, y_pred_test)
cnf_matrix
for i in cnf_matrix:
for j in i:
print(j, end=' ')
print()
"""# CNN"""
import keras.models
import tensorflow
import numpy as np
from keras.layers.convolutional import Conv1D , Conv2D
from keras.layers.pooling import MaxPooling1D
from keras.models import Sequential
from keras.optimizers import Adam
from keras.layers import Dense, Activation, Flatten
# define model
n_steps = 11
n_features = 1
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(n_steps, n_features)))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.summary()
# n_features = 1
# X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], n_features))
print(X_train.shape)
print(y_train.shape)
# y_train=np.ones((286352,10,64))
model.fit(X_train, y_train, epochs=10, verbose=0)
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
y_test=np.ones((71589,10,64))
scores = model.evaluate(X_test, y_test)
print(scores)
"""# TUNING HYPERPARAMETERS IN ANN
"""
from sklearn.neural_network import MLPClassifier
mlp = MLPClassifier(hidden_layer_sizes=(35,), max_iter=10, alpha=1e-4, solver='sgd', verbose=10, tol=1e-4, random_state=1, learning_rate_init=.1)
pipe_mlp = Pipeline([('scl', StandardScaler()),('clf', mlp)])
train_sizes_mlp, train_scores_mlp, test_scores_mlp = learning_curve(estimator=pipe_mlp,X=X, y=y, train_sizes=np.linspace(0.2,1.0,5), cv=5, n_jobs=-1)
train_mean_mlp = np.mean(train_scores_mlp, axis=1)
train_std_mlp = np.std(train_scores_mlp, axis=1)
test_mean_mlp = np.mean(test_scores_mlp, axis=1)
test_std_mlp = np.std(test_scores_mlp, axis=1)
for i in train_mean_mlp:
print(i)
for i in test_mean_mlp:
print(i)
pipe_mlp = pipe_mlp.fit(X_train, y_train)
y_pred_train = pipe_mlp.predict(X_train)
y_pred_test = pipe_mlp.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy_score(y_train, y_pred_train), accuracy_score(y_test, y_pred_test)
# (0.9942273844778454, 0.994077302378857)-->base paper
# (0.992, 0.991)--->100 layers, adam
# (0.9942204000670504, 0.994091271005322)--->75 layers, sgd
# (0.9942238922724479, 0.994091271005322)---->100,sgd
# (0.9942169078616528, 0.994091271005322)---->35,sgd
# (0.9929597139185339, 0.9926385338529662)---->35,adam
from sklearn.metrics import classification_report
target_names = ['Normal', 'DoSattack', 'scan', 'malitiousControl', 'malitiousOperation', 'spying', 'dataProbing', 'wrongSetUp']
print(classification_report(y_train, y_pred_train, target_names=target_names))
import pickle
pickle.dump(pipe_mlp,open('MLP.sav', 'wb'))
print(classification_report(y_test, y_pred_test, target_names=target_names))
from sklearn.metrics import confusion_matrix
import itertools
cnf_matrix = confusion_matrix(y_test, y_pred_test)
cnf_matrix
for i in cnf_matrix:
for j in i:
print(j, end=' ')
print()