-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNID_NSL_KDD_updated.py
768 lines (757 loc) · 30.1 KB
/
NID_NSL_KDD_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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
import pandas as pd
import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn import metrics
import sys
import sklearn
import io
import random
train_url = "https://raw.githubusercontent.com/merteroglu/NSL-KDD-Network-Instrusion-Detection/master/NSL_KDD_Train.csv"
test_url = "https://raw.githubusercontent.com/merteroglu/NSL-KDD-Network-Instrusion-Detection/master/NSL_KDD_Test.csv"
col_names = [
"duration",
"protocol_type",
"service",
"flag",
"src_bytes",
"dst_bytes",
"land",
"wrong_fragment",
"urgent",
"hot",
"num_failed_logins",
"logged_in",
"num_compromised",
"root_shell",
"su_attempted",
"num_root",
"num_file_creations",
"num_shells",
"num_access_files",
"num_outbound_cmds",
"is_host_login",
"is_guest_login",
"count",
"srv_count",
"serror_rate",
"srv_serror_rate",
"rerror_rate",
"srv_rerror_rate",
"same_srv_rate",
"diff_srv_rate",
"srv_diff_host_rate",
"dst_host_count",
"dst_host_srv_count",
"dst_host_same_srv_rate",
"dst_host_diff_srv_rate",
"dst_host_same_src_port_rate",
"dst_host_srv_diff_host_rate",
"dst_host_serror_rate",
"dst_host_srv_serror_rate",
"dst_host_rerror_rate",
"dst_host_srv_rerror_rate",
"label",
]
df = pd.read_csv(train_url, header=None, names=col_names)
df_test = pd.read_csv(test_url, header=None, names=col_names)
print("Dimensions of the Training set:", df.shape)
print("Dimensions of the Test set:", df_test.shape)
df.head(5)
print("Label distribution Training set:")
print(df["label"].value_counts())
print()
print("Label distribution Test set:")
print(df_test["label"].value_counts())
print("Training set:")
for col_name in df.columns:
if df[col_name].dtypes == "object":
unique_cat = len(df[col_name].unique())
print(
"Feature '{col_name}' has {unique_cat} categories".format(
col_name=col_name, unique_cat=unique_cat
)
)
print()
print("Distribution of categories in service:")
print(df["service"].value_counts().sort_values(ascending=False).head())
print("Test set:")
for col_name in df_test.columns:
if df_test[col_name].dtypes == "object":
unique_cat = len(df_test[col_name].unique())
print(
"Feature '{col_name}' has {unique_cat} categories".format(
col_name=col_name, unique_cat=unique_cat
)
)
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
categorical_columns = ["protocol_type", "service", "flag"]
df_categorical_values = df[categorical_columns]
testdf_categorical_values = df_test[categorical_columns]
df_categorical_values.head()
unique_protocol = sorted(df.protocol_type.unique())
string1 = "Protocol_type_"
unique_protocol2 = [string1 + x for x in unique_protocol]
print(unique_protocol2)
unique_service = sorted(df.service.unique())
string2 = "service_"
unique_service2 = [string2 + x for x in unique_service]
print(unique_service2)
unique_flag = sorted(df.flag.unique())
string3 = "flag_"
unique_flag2 = [string3 + x for x in unique_flag]
print(unique_flag2)
dumcols = unique_protocol2 + unique_service2 + unique_flag2
unique_service_test = sorted(df_test.service.unique())
unique_service2_test = [string2 + x for x in unique_service_test]
testdumcols = unique_protocol2 + unique_service2_test + unique_flag2
df_categorical_values_enc = df_categorical_values.apply(LabelEncoder().fit_transform)
print(df_categorical_values.head())
print("--------------------")
print(df_categorical_values_enc.head())
testdf_categorical_values_enc = testdf_categorical_values.apply(
LabelEncoder().fit_transform
)
enc = OneHotEncoder(categories="auto")
df_categorical_values_encenc = enc.fit_transform(df_categorical_values_enc)
df_cat_data = pd.DataFrame(df_categorical_values_encenc.toarray(), columns=dumcols)
testdf_categorical_values_encenc = enc.fit_transform(testdf_categorical_values_enc)
testdf_cat_data = pd.DataFrame(
testdf_categorical_values_encenc.toarray(), columns=testdumcols
)
df_cat_data.head()
trainservice = df["service"].tolist()
testservice = df_test["service"].tolist()
difference = list(set(trainservice) - set(testservice))
string = "service_"
difference = [string + x for x in difference]
difference
for col in difference:
testdf_cat_data[col] = 0
print(df_cat_data.shape)
print(testdf_cat_data.shape)
newdf = df.join(df_cat_data)
newdf.drop("flag", axis=1, inplace=True)
newdf.drop("protocol_type", axis=1, inplace=True)
newdf.drop("service", axis=1, inplace=True)
newdf_test = df_test.join(testdf_cat_data)
newdf_test.drop("flag", axis=1, inplace=True)
newdf_test.drop("protocol_type", axis=1, inplace=True)
newdf_test.drop("service", axis=1, inplace=True)
print(newdf.shape)
print(newdf_test.shape)
labeldf = newdf["label"]
labeldf_test = newdf_test["label"]
newlabeldf = labeldf.replace(
{
"normal": 0,
"neptune": 1,
"back": 1,
"land": 1,
"pod": 1,
"smurf": 1,
"teardrop": 1,
"mailbomb": 1,
"apache2": 1,
"processtable": 1,
"udpstorm": 1,
"worm": 1,
"ipsweep": 2,
"nmap": 2,
"portsweep": 2,
"satan": 2,
"mscan": 2,
"saint": 2,
"ftp_write": 3,
"guess_passwd": 3,
"imap": 3,
"multihop": 3,
"phf": 3,
"spy": 3,
"warezclient": 3,
"warezmaster": 3,
"sendmail": 3,
"named": 3,
"snmpgetattack": 3,
"snmpguess": 3,
"xlock": 3,
"xsnoop": 3,
"httptunnel": 3,
"buffer_overflow": 4,
"loadmodule": 4,
"perl": 4,
"rootkit": 4,
"ps": 4,
"sqlattack": 4,
"xterm": 4,
}
)
newlabeldf_test = labeldf_test.replace(
{
"normal": 0,
"neptune": 1,
"back": 1,
"land": 1,
"pod": 1,
"smurf": 1,
"teardrop": 1,
"mailbomb": 1,
"apache2": 1,
"processtable": 1,
"udpstorm": 1,
"worm": 1,
"ipsweep": 2,
"nmap": 2,
"portsweep": 2,
"satan": 2,
"mscan": 2,
"saint": 2,
"ftp_write": 3,
"guess_passwd": 3,
"imap": 3,
"multihop": 3,
"phf": 3,
"spy": 3,
"warezclient": 3,
"warezmaster": 3,
"sendmail": 3,
"named": 3,
"snmpgetattack": 3,
"snmpguess": 3,
"xlock": 3,
"xsnoop": 3,
"httptunnel": 3,
"buffer_overflow": 4,
"loadmodule": 4,
"perl": 4,
"rootkit": 4,
"ps": 4,
"sqlattack": 4,
"xterm": 4,
}
)
newdf["label"] = newlabeldf
newdf_test["label"] = newlabeldf_test
to_drop_DoS = [0, 1]
to_drop_Probe = [0, 2]
to_drop_R2L = [0, 3]
to_drop_U2R = [0, 4]
DoS_df = newdf[newdf["label"].isin(to_drop_DoS)]
Probe_df = newdf[newdf["label"].isin(to_drop_Probe)]
R2L_df = newdf[newdf["label"].isin(to_drop_R2L)]
U2R_df = newdf[newdf["label"].isin(to_drop_U2R)]
DoS_df_test = newdf_test[newdf_test["label"].isin(to_drop_DoS)]
Probe_df_test = newdf_test[newdf_test["label"].isin(to_drop_Probe)]
R2L_df_test = newdf_test[newdf_test["label"].isin(to_drop_R2L)]
U2R_df_test = newdf_test[newdf_test["label"].isin(to_drop_U2R)]
print("Train:")
print("Dimensions of DoS:", DoS_df.shape)
print("Dimensions of Probe:", Probe_df.shape)
print("Dimensions of R2L:", R2L_df.shape)
print("Dimensions of U2R:", U2R_df.shape)
print()
print("Test:")
print("Dimensions of DoS:", DoS_df_test.shape)
print("Dimensions of Probe:", Probe_df_test.shape)
print("Dimensions of R2L:", R2L_df_test.shape)
print("Dimensions of U2R:", U2R_df_test.shape)
X_DoS = DoS_df.drop("label", 1)
Y_DoS = DoS_df.label
X_Probe = Probe_df.drop("label", 1)
Y_Probe = Probe_df.label
X_R2L = R2L_df.drop("label", 1)
Y_R2L = R2L_df.label
X_U2R = U2R_df.drop("label", 1)
Y_U2R = U2R_df.label
X_DoS_test = DoS_df_test.drop("label", 1)
Y_DoS_test = DoS_df_test.label
X_Probe_test = Probe_df_test.drop("label", 1)
Y_Probe_test = Probe_df_test.label
X_R2L_test = R2L_df_test.drop("label", 1)
Y_R2L_test = R2L_df_test.label
X_U2R_test = U2R_df_test.drop("label", 1)
Y_U2R_test = U2R_df_test.label
colNames = list(X_DoS)
colNames_test = list(X_DoS_test)
from sklearn import preprocessing
scaler1 = preprocessing.StandardScaler().fit(X_DoS)
X_DoS = scaler1.transform(X_DoS)
scaler2 = preprocessing.StandardScaler().fit(X_Probe)
X_Probe = scaler2.transform(X_Probe)
scaler3 = preprocessing.StandardScaler().fit(X_R2L)
X_R2L = scaler3.transform(X_R2L)
scaler4 = preprocessing.StandardScaler().fit(X_U2R)
X_U2R = scaler4.transform(X_U2R)
scaler5 = preprocessing.StandardScaler().fit(X_DoS_test)
X_DoS_test = scaler5.transform(X_DoS_test)
scaler6 = preprocessing.StandardScaler().fit(X_Probe_test)
X_Probe_test = scaler6.transform(X_Probe_test)
scaler7 = preprocessing.StandardScaler().fit(X_R2L_test)
X_R2L_test = scaler7.transform(X_R2L_test)
scaler8 = preprocessing.StandardScaler().fit(X_U2R_test)
X_U2R_test = scaler8.transform(X_U2R_test)
from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_estimators=10, n_jobs=2)
rfe = RFE(estimator=clf, n_features_to_select=13, step=1)
rfe.fit(X_DoS, Y_DoS.astype(int))
X_rfeDoS = rfe.transform(X_DoS)
true = rfe.support_
rfecolindex_DoS = [i for i, x in enumerate(true) if x]
rfecolname_DoS = list(colNames[i] for i in rfecolindex_DoS)
rfe.fit(X_Probe, Y_Probe.astype(int))
X_rfeProbe = rfe.transform(X_Probe)
true = rfe.support_
rfecolindex_Probe = [i for i, x in enumerate(true) if x]
rfecolname_Probe = list(colNames[i] for i in rfecolindex_Probe)
rfe.fit(X_R2L, Y_R2L.astype(int))
X_rfeR2L = rfe.transform(X_R2L)
true = rfe.support_
rfecolindex_R2L = [i for i, x in enumerate(true) if x]
rfecolname_R2L = list(colNames[i] for i in rfecolindex_R2L)
rfe.fit(X_U2R, Y_U2R.astype(int))
X_rfeU2R = rfe.transform(X_U2R)
true = rfe.support_
rfecolindex_U2R = [i for i, x in enumerate(true) if x]
rfecolname_U2R = list(colNames[i] for i in rfecolindex_U2R)
print("Features selected for DoS:", rfecolname_DoS)
print()
print("Features selected for Probe:", rfecolname_Probe)
print()
print("Features selected for R2L:", rfecolname_R2L)
print()
print("Features selected for U2R:", rfecolname_U2R)
print(X_rfeDoS.shape)
print(X_rfeProbe.shape)
print(X_rfeR2L.shape)
print(X_rfeU2R.shape)
clf_DoS = RandomForestClassifier(n_estimators=10, n_jobs=2)
clf_Probe = RandomForestClassifier(n_estimators=10, n_jobs=2)
clf_R2L = RandomForestClassifier(n_estimators=10, n_jobs=2)
clf_U2R = RandomForestClassifier(n_estimators=10, n_jobs=2)
clf_DoS.fit(X_DoS, Y_DoS.astype(int))
clf_Probe.fit(X_Probe, Y_Probe.astype(int))
clf_R2L.fit(X_R2L, Y_R2L.astype(int))
clf_U2R.fit(X_U2R, Y_U2R.astype(int))
clf_rfeDoS = RandomForestClassifier(n_estimators=10, n_jobs=2)
clf_rfeProbe = RandomForestClassifier(n_estimators=10, n_jobs=2)
clf_rfeR2L = RandomForestClassifier(n_estimators=10, n_jobs=2)
clf_rfeU2R = RandomForestClassifier(n_estimators=10, n_jobs=2)
clf_rfeDoS.fit(X_rfeDoS, Y_DoS.astype(int))
clf_rfeProbe.fit(X_rfeProbe, Y_Probe.astype(int))
clf_rfeR2L.fit(X_rfeR2L, Y_R2L.astype(int))
clf_rfeU2R.fit(X_rfeU2R, Y_U2R.astype(int))
clf_DoS.predict(X_DoS_test)
clf_DoS.predict_proba(X_DoS_test)[0:10]
Y_DoS_pred = clf_DoS.predict(X_DoS_test)
pd.crosstab(
Y_DoS_test, Y_DoS_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
Y_Probe_pred = clf_Probe.predict(X_Probe_test)
pd.crosstab(
Y_Probe_test,
Y_Probe_pred,
rownames=["Actual attacks"],
colnames=["Predicted attacks"],
)
Y_R2L_pred = clf_R2L.predict(X_R2L_test)
pd.crosstab(
Y_R2L_test, Y_R2L_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
Y_U2R_pred = clf_U2R.predict(X_U2R_test)
pd.crosstab(
Y_U2R_test, Y_U2R_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
from sklearn.model_selection import cross_val_score
from sklearn import metrics
accuracy = cross_val_score(clf_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="accuracy")
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(clf_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="precision")
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(clf_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="recall")
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="f1")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(clf_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="accuracy")
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(clf_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="recall_macro")
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(clf_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="accuracy")
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(clf_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="recall_macro")
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
X_DoS_test2 = X_DoS_test[:, rfecolindex_DoS]
X_Probe_test2 = X_Probe_test[:, rfecolindex_Probe]
X_R2L_test2 = X_R2L_test[:, rfecolindex_R2L]
X_U2R_test2 = X_U2R_test[:, rfecolindex_U2R]
X_U2R_test2.shape
Y_DoS_pred2 = clf_rfeDoS.predict(X_DoS_test2)
pd.crosstab(
Y_DoS_test, Y_DoS_pred2, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
Y_Probe_pred2 = clf_rfeProbe.predict(X_Probe_test2)
pd.crosstab(
Y_Probe_test,
Y_Probe_pred2,
rownames=["Actual attacks"],
colnames=["Predicted attacks"],
)
Y_R2L_pred2 = clf_rfeR2L.predict(X_R2L_test2)
pd.crosstab(
Y_R2L_test, Y_R2L_pred2, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
Y_U2R_pred2 = clf_rfeU2R.predict(X_U2R_test2)
pd.crosstab(
Y_U2R_test, Y_U2R_pred2, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
accuracy = cross_val_score(
clf_rfeDoS, X_DoS_test2, Y_DoS_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_rfeDoS, X_DoS_test2, Y_DoS_test, cv=10, scoring="precision"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(clf_rfeDoS, X_DoS_test2, Y_DoS_test, cv=10, scoring="recall")
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_rfeDoS, X_DoS_test2, Y_DoS_test, cv=10, scoring="f1")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_rfeProbe, X_Probe_test2, Y_Probe_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_rfeProbe, X_Probe_test2, Y_Probe_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_rfeProbe, X_Probe_test2, Y_Probe_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(
clf_rfeProbe, X_Probe_test2, Y_Probe_test, cv=10, scoring="f1_macro"
)
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_rfeR2L, X_R2L_test2, Y_R2L_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_rfeR2L, X_R2L_test2, Y_R2L_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_rfeR2L, X_R2L_test2, Y_R2L_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_rfeR2L, X_R2L_test2, Y_R2L_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_rfeU2R, X_U2R_test2, Y_U2R_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_rfeU2R, X_U2R_test2, Y_U2R_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_rfeU2R, X_U2R_test2, Y_U2R_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_rfeU2R, X_U2R_test2, Y_U2R_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
from sklearn.neighbors import KNeighborsClassifier
clf_KNN_DoS = KNeighborsClassifier()
clf_KNN_Probe = KNeighborsClassifier()
clf_KNN_R2L = KNeighborsClassifier()
clf_KNN_U2R = KNeighborsClassifier()
clf_KNN_DoS.fit(X_DoS, Y_DoS.astype(int))
clf_KNN_Probe.fit(X_Probe, Y_Probe.astype(int))
clf_KNN_R2L.fit(X_R2L, Y_R2L.astype(int))
clf_KNN_U2R.fit(X_U2R, Y_U2R.astype(int))
Y_DoS_pred = clf_KNN_DoS.predict(X_DoS_test)
pd.crosstab(
Y_DoS_test, Y_DoS_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
Y_Probe_pred = clf_KNN_Probe.predict(X_Probe_test)
pd.crosstab(
Y_Probe_test,
Y_Probe_pred,
rownames=["Actual attacks"],
colnames=["Predicted attacks"],
)
Y_R2L_pred = clf_KNN_R2L.predict(X_R2L_test)
pd.crosstab(
Y_R2L_test, Y_R2L_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
Y_U2R_pred = clf_KNN_U2R.predict(X_U2R_test)
pd.crosstab(
Y_U2R_test, Y_U2R_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
from sklearn.model_selection import cross_val_score
from sklearn import metrics
accuracy = cross_val_score(
clf_KNN_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_KNN_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="precision"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(clf_KNN_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="recall")
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_KNN_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="f1")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_KNN_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_KNN_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_KNN_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(
clf_KNN_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="f1_macro"
)
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_KNN_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_KNN_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_KNN_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_KNN_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_KNN_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_KNN_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_KNN_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_KNN_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
from sklearn.svm import SVC
clf_SVM_DoS = SVC(kernel="linear", C=1.0, random_state=0)
clf_SVM_Probe = SVC(kernel="linear", C=1.0, random_state=0)
clf_SVM_R2L = SVC(kernel="linear", C=1.0, random_state=0)
clf_SVM_U2R = SVC(kernel="linear", C=1.0, random_state=0)
clf_SVM_DoS.fit(X_DoS, Y_DoS.astype(int))
clf_SVM_Probe.fit(X_Probe, Y_Probe.astype(int))
clf_SVM_R2L.fit(X_R2L, Y_R2L.astype(int))
clf_SVM_U2R.fit(X_U2R, Y_U2R.astype(int))
Y_DoS_pred = clf_SVM_DoS.predict(X_DoS_test)
pd.crosstab(
Y_DoS_test, Y_DoS_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
Y_Probe_pred = clf_SVM_Probe.predict(X_Probe_test)
pd.crosstab(
Y_Probe_test,
Y_Probe_pred,
rownames=["Actual attacks"],
colnames=["Predicted attacks"],
)
Y_R2L_pred = clf_SVM_R2L.predict(X_R2L_test)
pd.crosstab(
Y_R2L_test, Y_R2L_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
Y_U2R_pred = clf_SVM_U2R.predict(X_U2R_test)
pd.crosstab(
Y_U2R_test, Y_U2R_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
from sklearn.model_selection import cross_val_score
from sklearn import metrics
accuracy = cross_val_score(
clf_SVM_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_SVM_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="precision"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(clf_SVM_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="recall")
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_SVM_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="f1")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_SVM_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_SVM_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_SVM_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(
clf_SVM_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="f1_macro"
)
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_SVM_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_SVM_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_SVM_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_SVM_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_SVM_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_SVM_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_SVM_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_SVM_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
from sklearn.ensemble import VotingClassifier
clf_voting_DoS = VotingClassifier(
estimators=[("rf", clf_DoS), ("knn", clf_KNN_DoS), ("svm", clf_SVM_DoS)],
voting="hard",
)
clf_voting_Probe = VotingClassifier(
estimators=[("rf", clf_Probe), ("knn", clf_KNN_Probe), ("svm", clf_SVM_Probe)],
voting="hard",
)
clf_voting_R2L = VotingClassifier(
estimators=[("rf", clf_R2L), ("knn", clf_KNN_R2L), ("svm", clf_SVM_R2L)],
voting="hard",
)
clf_voting_U2R = VotingClassifier(
estimators=[("rf", clf_U2R), ("knn", clf_KNN_U2R), ("svm", clf_SVM_U2R)],
voting="hard",
)
clf_voting_DoS.fit(X_DoS, Y_DoS.astype(int))
clf_voting_Probe.fit(X_Probe, Y_Probe.astype(int))
clf_voting_R2L.fit(X_R2L, Y_R2L.astype(int))
clf_voting_U2R.fit(X_U2R, Y_U2R.astype(int))
Y_DoS_pred = clf_voting_DoS.predict(X_DoS_test)
pd.crosstab(
Y_DoS_test, Y_DoS_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
Y_Probe_pred = clf_voting_Probe.predict(X_Probe_test)
pd.crosstab(
Y_Probe_test,
Y_Probe_pred,
rownames=["Actual attacks"],
colnames=["Predicted attacks"],
)
Y_R2L_pred = clf_voting_R2L.predict(X_R2L_test)
pd.crosstab(
Y_R2L_test, Y_R2L_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
Y_U2R_pred = clf_voting_U2R.predict(X_U2R_test)
pd.crosstab(
Y_U2R_test, Y_U2R_pred, rownames=["Actual attacks"], colnames=["Predicted attacks"]
)
accuracy = cross_val_score(
clf_voting_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_voting_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="precision"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_voting_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="recall"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_voting_DoS, X_DoS_test, Y_DoS_test, cv=10, scoring="f1")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_voting_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_voting_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_voting_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(
clf_voting_Probe, X_Probe_test, Y_Probe_test, cv=10, scoring="f1_macro"
)
print("F-mesaure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_voting_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_voting_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_voting_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_voting_R2L, X_R2L_test, Y_R2L_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))
accuracy = cross_val_score(
clf_voting_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="accuracy"
)
print("Accuracy: %0.5f (+/- %0.5f)" % (accuracy.mean(), accuracy.std() * 2))
precision = cross_val_score(
clf_voting_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="precision_macro"
)
print("Precision: %0.5f (+/- %0.5f)" % (precision.mean(), precision.std() * 2))
recall = cross_val_score(
clf_voting_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="recall_macro"
)
print("Recall: %0.5f (+/- %0.5f)" % (recall.mean(), recall.std() * 2))
f = cross_val_score(clf_voting_U2R, X_U2R_test, Y_U2R_test, cv=10, scoring="f1_macro")
print("F-measure: %0.5f (+/- %0.5f)" % (f.mean(), f.std() * 2))