-
Notifications
You must be signed in to change notification settings - Fork 3
/
UNSW_MULTI_UPDATED.py
408 lines (400 loc) · 12.7 KB
/
UNSW_MULTI_UPDATED.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
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import pickle
from os import path
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder
from sklearn import metrics
from sklearn import preprocessing
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import VotingClassifier
import time
bin_data_path = "./datasets/multi_data.csv"
df = pd.read_csv(bin_data_path)
print("Dimensions of the Training set:", df.shape)
df.shape
df.head()
X = df.drop(columns=["label"], axis=1)
Y = df["label"]
X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size=0.30, random_state=50
)
print("Training Data Shape:", X_train.shape)
print("Training Labels Shape:", y_train.shape)
print("Testing Data Shape:", X_test.shape)
print("Testing Label Shape:", y_test.shape)
knn = KNeighborsClassifier(n_neighbors=3)
svm = SVC(kernel="poly", C=1.0, random_state=0)
rf = RandomForestClassifier(n_estimators=10, random_state=1)
dt = DecisionTreeClassifier(random_state=0)
mlp = MLPClassifier(random_state=0, max_iter=300)
clf_voting = VotingClassifier(
estimators=[("rf", rf), ("knn", knn), ("svm", svm)], voting="hard"
)
knn = KNeighborsClassifier(
algorithm="auto",
leaf_size=30,
metric="minkowski",
metric_params=None,
n_jobs=None,
n_neighbors=5,
p=2,
weights="uniform",
)
print("=========================")
print("kNN Classifier")
print("=========================")
t1_ens = time.time()
knn.fit(X_train, y_train.astype(int))
t2_ens = time.time()
print("Time to train knn on MultiClass training dat:", t2_ens - t1_ens)
y_pred = knn.predict(X_test)
print("Accuracy - ", accuracy_score(y_test, y_pred) * 100)
cls_report = classification_report(y_true=y_test, y_pred=y_pred)
print(cls_report)
pkl_filename = "./qaiser_models/knn_multi.pkl"
if not path.isfile(pkl_filename):
with open(pkl_filename, "wb") as file:
pickle.dump(knn, file)
print("Saved model to disk")
else:
print("Model already saved")
import matplotlib.pyplot as plt
from sklearn.metrics import plot_confusion_matrix
print("Testing on Unssen Data")
fig, ax = plt.subplots(figsize=(10, 10))
labels = [
"Analysis",
"Backdoor",
"DoS",
"Exploits",
"Fuzzers",
"Generic",
"Normal",
"Recon",
"Worms",
]
plot_confusion_matrix(
knn, X_test, y_test, cmap="Greens", display_labels=labels, normalize="pred", ax=ax
)
plt.savefig("./diagrams/kNN Confusion Matrix.png")
plt.show()
print("=========================")
print("Fitting SVM Classifier")
print("=========================")
t1_svm = time.time()
svm.fit(X_train, y_train.astype(int))
t2_svm = time.time()
print("Time to train SVM on training dat:", t2_svm - t1_svm)
y_pred = svm.predict(X_test)
print("Accuracy for Multiclass SVM is - ", accuracy_score(y_test, y_pred) * 100)
cls_report = classification_report(y_true=y_test, y_pred=y_pred)
print(cls_report)
pkl_filename = "./qaiser_models/SVM_multi.pkl"
if not path.isfile(pkl_filename):
with open(pkl_filename, "wb") as file:
pickle.dump(svm, file)
print("Saved model to disk")
else:
print("Model already saved")
import matplotlib.pyplot as plt
from sklearn.metrics import plot_confusion_matrix
print("Testing on Unssen Data")
fig, ax = plt.subplots(figsize=(10, 10))
labels = [
"Analysis",
"Backdoor",
"DoS",
"Exploits",
"Fuzzers",
"Generic",
"Normal",
"Recon",
"Worms",
]
plot_confusion_matrix(
svm, X_test, y_test, cmap="Greens", display_labels=labels, normalize="pred", ax=ax
)
plt.savefig("./diagrams/SVM multiclass Confusion Matrix.png")
plt.show()
import matplotlib.pyplot as plt
from sklearn.metrics import plot_confusion_matrix
print("=========================")
print("Fitting Random Forest Classifier")
print("=========================")
t1_rf = time.time()
rf.fit(X_train, y_train.astype(int))
t2_rf = time.time()
print("Time to train RF on binary training dat:", t2_rf - t1_rf)
print("======================================================")
y_pred = rf.predict(X_test)
print("Accuracy for multi RF is - ", accuracy_score(y_test, y_pred) * 100)
cls_report = classification_report(y_true=y_test, y_pred=y_pred)
print("========Printing Classification Reports==========")
print(cls_report)
pkl_filename = "./qaiser_models/RF_multi.pkl"
if not path.isfile(pkl_filename):
with open(pkl_filename, "wb") as file:
pickle.dump(rf, file)
print("Saved model to disk")
else:
print("Model already saved")
print("Testing on Unssen Data")
fig, ax = plt.subplots(figsize=(10, 10))
labels = [
"Analysis",
"Backdoor",
"DoS",
"Exploits",
"Fuzzers",
"Generic",
"Normal",
"Recon",
"Worms",
]
plot_confusion_matrix(
rf, X_test, y_test, cmap="Greens", display_labels=labels, normalize="pred", ax=ax
)
plt.savefig("./diagrams/RF Confusion Matrix.png")
plt.show()
print("===========================================")
print("Fitting DT Classifier")
print("===========================================")
t1_dt = time.time()
dt.fit(X_train, y_train.astype(int))
t2_dt = time.time()
print("Time to train RF on multiclass training dat:", t2_dt - t1_dt)
print("======================================================")
y_pred = dt.predict(X_test)
print("Accuracy for multi DT is - ", accuracy_score(y_test, y_pred) * 100)
cls_report = classification_report(y_true=y_test, y_pred=y_pred)
print("========Printing Classification Reports==========")
print(cls_report)
pkl_filename = "./qaiser_models/DT_multi.pkl"
if not path.isfile(pkl_filename):
with open(pkl_filename, "wb") as file:
pickle.dump(dt, file)
print("Saved model to disk")
else:
print("Model already saved")
print("Testing on Unssen Data")
fig, ax = plt.subplots(figsize=(10, 10))
labels = [
"Analysis",
"Backdoor",
"DoS",
"Exploits",
"Fuzzers",
"Generic",
"Normal",
"Recon",
"Worms",
]
plot_confusion_matrix(
dt, X_test, y_test, cmap="Greens", display_labels=labels, normalize="pred", ax=ax
)
plt.savefig("./diagrams/DT Confusion Matrix.png")
plt.show()
print("===========================================")
print("Fitting MLP Classifier")
print("===========================================")
t1_mlp = time.time()
mlp.fit(X_train, y_train.astype(int))
t2_mlp = time.time()
print("Time to train MLP on multiclass training dat:", t2_dt - t1_dt)
print("======================================================")
y_pred = mlp.predict(X_test)
print("Accuracy for multiclass MLP is - ", accuracy_score(y_test, y_pred) * 100)
cls_report = classification_report(y_true=y_test, y_pred=y_pred)
print("========Printing Classification Reports==========")
print(cls_report)
pkl_filename = "./qaiser_models/MLP_multi.pkl"
if not path.isfile(pkl_filename):
with open(pkl_filename, "wb") as file:
pickle.dump(mlp, file)
print("Saved model to disk")
else:
print("Model already saved")
print("Testing on Unssen Data")
fig, ax = plt.subplots(figsize=(10, 10))
labels = [
"Analysis",
"Backdoor",
"DoS",
"Exploits",
"Fuzzers",
"Generic",
"Normal",
"Recon",
"Worms",
]
plot_confusion_matrix(
mlp, X_test, y_test, cmap="Greens", display_labels=labels, normalize="pred", ax=ax
)
plt.savefig("./diagrams/MLP Confusion Matrix.png")
plt.show()
print("===========================================")
print("Fitting Our Ensemble Method Classifier")
print("===========================================")
t1_clf_voting = time.time()
clf_voting.fit(X_train, y_train.astype(int))
t2_clf_voting = time.time()
print("Time to train clf_voting on binary training dat:", t2_clf_voting - t1_clf_voting)
print("======================================================")
y_pred = clf_voting.predict(X_test)
print("Accuracy for binary clf_voting is - ", accuracy_score(y_test, y_pred) * 100)
cls_report = classification_report(y_true=y_test, y_pred=y_pred)
print("========Printing Classification Reports==========")
print(cls_report)
pkl_filename = "./qaiser_models/clf_voting_multi.pkl"
if not path.isfile(pkl_filename):
with open(pkl_filename, "wb") as file:
pickle.dump(clf_voting, file)
print("Saved model to disk")
else:
print("Model already saved")
print("Testing on Unssen Data")
fig, ax = plt.subplots(figsize=(10, 10))
labels = [
"Analysis",
"Backdoor",
"DoS",
"Exploits",
"Fuzzers",
"Generic",
"Normal",
"Recon",
"Worms",
]
plot_confusion_matrix(
clf_voting,
X_test,
y_test,
cmap="Greens",
display_labels=labels,
normalize="pred",
ax=ax,
)
plt.savefig("./diagrams/clf_voting Confusion Matrix-Testing.png")
plt.show()
print("===========================================")
print("Fitting Our Ensemble Method Classifier")
print("===========================================")
from sklearn.ensemble import GradientBoostingClassifier
xg = GradientBoostingClassifier(
n_estimators=100, learning_rate=1.0, max_depth=3, random_state=0
)
clf_voting = VotingClassifier(
estimators=[("rf", rf), ("dt", dt), ("xg", xg)], voting="hard"
)
t1_clf_voting = time.time()
clf_voting.fit(X_train, y_train.astype(int))
t2_clf_voting = time.time()
print("Time to train clf_voting on multi training dat:", t2_clf_voting - t1_clf_voting)
print("======================================================")
y_pred = clf_voting.predict(X_test)
print("Accuracy for multiclass clf_voting is - ", accuracy_score(y_test, y_pred) * 100)
cls_report = classification_report(y_true=y_test, y_pred=y_pred)
print("========Printing Classification Reports==========")
print(cls_report)
pkl_filename = "./qaiser_models/clf_ensemble_multi.pkl"
if not path.isfile(pkl_filename):
with open(pkl_filename, "wb") as file:
pickle.dump(clf_voting, file)
print("Saved model to disk")
else:
print("Model already saved")
print("Testing on Unssen Data")
fig, ax = plt.subplots(figsize=(10, 10))
labels = [
"Analysis",
"Backdoor",
"DoS",
"Exploits",
"Fuzzers",
"Generic",
"Normal",
"Recon",
"Worms",
]
plot_confusion_matrix(
clf_voting,
X_test,
y_test,
cmap="Greens",
display_labels=labels,
normalize="pred",
ax=ax,
)
plt.savefig("./diagrams/Ensemble Confusion Matrix-Testing.png")
plt.show()
print("===========================================")
print("Fitting Our Ensemble Method Classifier")
print("===========================================")
print("Time to train clf_voting on multi training dat:", t2_clf_voting - t1_clf_voting)
print("======================================================")
y_pred = clf_voting.predict(X_train)
print(
"Accuracy for multiclass clf_voting on Training Data is - ",
accuracy_score(y_train.astype(int), y_pred) * 100,
)
print("Testing on Unssen Data")
fig, ax = plt.subplots(figsize=(10, 10))
labels = [
"Analysis",
"Backdoor",
"DoS",
"Exploits",
"Fuzzers",
"Generic",
"Normal",
"Recon",
"Worms",
]
plot_confusion_matrix(
clf_voting,
X_train,
y_train,
cmap="Greens",
display_labels=labels,
normalize="pred",
ax=ax,
)
plt.savefig("./diagrams/Ensemble Training Data Confusion Matrix-Testing.png")
plt.show()
xg = GradientBoostingClassifier(
n_estimators=100, learning_rate=1.0, max_depth=3, random_state=0
)
clf_voting = VotingClassifier(
estimators=[("rf", rf), ("dt", dt), ("xg", xg)], voting="soft"
)
t1_clf_voting = time.time()
clf_voting.fit(X_train, y_train.astype(int))
t2_clf_voting = time.time()
print("Time to train clf_voting on multi training dat:", t2_clf_voting - t1_clf_voting)
import scikitplot.plotters as skplt
import matplotlib.pyplot as plt
print("ROC Curve for Testing Data")
preds = clf_voting.predict_proba(X_test)
fig, ax = plt.subplots(figsize=(10, 10))
skplt.plot_roc_curve(y_test, preds, ax=ax)
plt.savefig("Ensemble ROC for Testing.png")
plt.show()
import scikitplot.plotters as skplt
import matplotlib.pyplot as plt
print("ROC Curve for Training Data")
preds = clf_voting.predict_proba(X_train)
fig, ax = plt.subplots(figsize=(10, 10))
skplt.plot_roc_curve(y_train, preds, ax=ax)
plt.savefig("Ensemble ROC for Training.png")
plt.show()