-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy path04_1_feature_selection_for_attack_files.py
101 lines (86 loc) · 4.77 KB
/
04_1_feature_selection_for_attack_files.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
## "attacks" folder (with attack csv files) is required for the operation of the program.
## "attacks" folder must be located in the same directory as the program.
## the purpose of this code is to determine which features to use in the machine learning phase.
## for this purpose, the importance weights of the attacks are calculated.
## this calculation was made using sklearn-RandomForestRegressor.
## the some codes parts used for calculation and graphing are taken from the following site.
## http://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.html
import numpy as np
import os
import pandas as pd
import matplotlib.pyplot as plt
#%matplotlib inline
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import RandomForestRegressor
import sklearn as sk
import time
seconds = time.time()
def folder(f_name): #this function creates a folder named "feaure_pics" in the program directory.
try:
if not os.path.exists(f_name):
os.makedirs(f_name)
except OSError:
print ("The folder could not be created!")
# CSV files names:
csv_files=os.listdir("attacks")# It creates a list of file names in the "attacks" folder.
# Headers of column
main_labels=["Flow Duration","Total Fwd Packets", "Total Backward Packets","Total Length of Fwd Packets","Total Length of Bwd Packets","Fwd Packet Length Max","Fwd Packet Length Min",
"Fwd Packet Length Mean","Fwd Packet Length Std","Bwd Packet Length Max","Bwd Packet Length Min","Bwd Packet Length Mean","Bwd Packet Length Std",
"Flow Bytes/s","Flow Packets/s","Flow IAT Mean","Flow IAT Std","Flow IAT Max","Flow IAT Min","Fwd IAT Total","Fwd IAT Mean","Fwd IAT Std","Fwd IAT Max",
"Fwd IAT Min","Bwd IAT Total","Bwd IAT Mean","Bwd IAT Std","Bwd IAT Max","Bwd IAT Min","Fwd PSH Flags","Bwd PSH Flags","Fwd URG Flags","Bwd URG Flags",
"Fwd Header Length","Bwd Header Length","Fwd Packets/s","Bwd Packets/s","Min Packet Length","Max Packet Length","Packet Length Mean","Packet Length Std",
"Packet Length Variance","FIN Flag Count","SYN Flag Count","RST Flag Count","PSH Flag Count","ACK Flag Count","URG Flag Count","CWE Flag Count",
"ECE Flag Count","Down/Up Ratio","Average Packet Size","Avg Fwd Segment Size","Avg Bwd Segment Size","Fwd Avg Bytes/Bulk",
"Fwd Avg Packets/Bulk","Fwd Avg Bulk Rate","Bwd Avg Bytes/Bulk","Bwd Avg Packets/Bulk","Bwd Avg Bulk Rate","Subflow Fwd Packets","Subflow Fwd Bytes",
"Subflow Bwd Packets","Subflow Bwd Bytes","Init_Win_bytes_forward","Init_Win_bytes_backward","act_data_pkt_fwd",
"min_seg_size_forward","Active Mean","Active Std","Active Max","Active Min",
"Idle Mean","Idle Std","Idle Max", "Idle Min","Label"]
ths = open("importance_list_for_attack_files.csv", "w")
folder("./feaure_pics/")
for j in csv_files:
df=pd.read_csv(".\\attacks\\"+j,usecols=main_labels)
df=df.fillna(0)
attack_or_not=[]
for i in df["Label"]:#it changes the normal label to "1" and the attack tag to "0" for use in the machine learning algorithm
if i =="BENIGN":
attack_or_not.append(1)
else:
attack_or_not.append(0)
df["Label"]=attack_or_not
y = df["Label"].values
del df["Label"]
X = df.values
#computing the feature importances
forest = sk.ensemble.RandomForestRegressor(n_estimators=250,random_state=0)
forest.fit(X, y)
importances = forest.feature_importances_
std = np.std([tree.feature_importances_ for tree in forest.estimators_],
axis=0)
indices = np.argsort(importances)[::-1]
refclasscol=list(df.columns.values)
impor_bars = pd.DataFrame({'Features':refclasscol[0:20],'importance':importances[0:20]})
impor_bars = impor_bars.sort_values('importance',ascending=False).set_index('Features')
plt.rcParams['figure.figsize'] = (10, 5)
impor_bars.plot.bar();
#printing the feature importances
count=0
fea_ture=j[0:-4]+"=["
for i in impor_bars.index:
fea_ture=fea_ture+"\""+str(i)+"\","
count+=1
if count==5:
fea_ture=fea_ture[0:-1]+"]"
break
print(j[0:-4],"importance list:")
print(j[0:-4],"\n",impor_bars.head(20),"\n\n\n")
print(fea_ture)
plt.title(j[0:-4]+" Attack - Feature Importance")
plt.ylabel('Importance')
plt.savefig("./feaure_pics/"+j[0:-4]+".pdf",bbox_inches='tight', papertype = 'a4', orientation = 'portrait', format = 'pdf')
ths.write(( fea_ture ) )
plt.tight_layout()
plt.show()
print("-----------------------------------------------------------------------------------------------\n\n\n\n")
print("mission accomplished!")
print("Total operation time: = ",time.time()- seconds ,"seconds")
ths.close()