-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfeature_selection.py
147 lines (109 loc) · 4.5 KB
/
feature_selection.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
# Modified from source: https://machinelearningmastery.com/feature-selection-machine-learning-python/
# Feature Selection with Univariate Statistical Tests
from pandas import read_csv
from numpy import set_printoptions
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
from sklearn.feature_selection import chi2
# select best features from all features using ANOVA (f_classif())
def univariate_stat(df, names, no_of_best):
print("##############################")
print("########## f_classif #########")
print("##############################")
# considering the last column as class labels
array = df.values
X = array[:,0:len(names)-1]
Y = array[:,len(names)-1]
stat_list = [f_classif, chi2]
for stat_test in stat_list:
# feature extraction
test = SelectKBest(score_func=stat_test, k=no_of_best)
fit = test.fit(X, Y)
# summarize scores
set_printoptions(precision=3)
# print(fit.scores_)
score = {}
for i,j in zip(names, list(fit.scores_)):
score[i] = j
feature_scores = dict(sorted(score.items(), key=lambda item: item[1], reverse=True))
# print(feature_scores)
print("")
print("{:<15} {:<10}".format('Feature','Score'))
for k, v in feature_scores.items():
print("{:<15} {:<10}".format(k, v))
# Feature Extraction with RFE
from pandas import read_csv
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
def recursive_feature_eliminate(df, names, no_of_best):
print("##############################")
print("############# RFE ############")
print("##############################")
# considering the last column as class labels
array = df.values
X = array[:,0:len(names)-1]
Y = array[:,len(names)-1]
# feature extraction
model = LogisticRegression(solver='lbfgs')
rfe = RFE(model, no_of_best)
fit = rfe.fit(X, Y)
# print("Num Features: %d" % fit.n_features_)
# print("Selected Features: %s" % fit.support_)
# print("Feature Ranking: %s" % fit.ranking_)
selection = {}
for i,j in zip(names, list(fit.ranking_)):
selection[i] = j
support = {}
for i,j in zip(names, list(fit.support_)):
support[i] = j
# print(support)
print("{:<15} {:<10}".format('Feature','Support'))
for k, v in support.items():
print("{:<15} {:<10}".format(k, v))
feature_rank = dict(sorted(selection.items(), key=lambda item: item[1]))
# print(feature_rank)
print("")
print("{:<15} {:<10}".format('Feature','Rank'))
for k, v in feature_rank.items():
print("{:<15} {:<10}".format(k, v))
# Feature Importance with Extra Trees Classifier
from pandas import read_csv
from sklearn.ensemble import ExtraTreesClassifier
def extra_tree_classifier(df, names):
print("##############################")
print("#### ExtraTreesClassifier ####")
print("##############################")
# considering the last column as class labels
array = df.values
X = array[:,0:len(names)-1]
Y = array[:,len(names)-1]
# feature extraction
model = ExtraTreesClassifier(n_estimators=10)
model.fit(X, Y)
# print(model.feature_importances_)
importance = {}
for i,j in zip(names, list(model.feature_importances_)):
importance[i] = j
feature_importance = dict(sorted(importance.items(), key=lambda item: item[1], reverse=True))
# print(feature_importance)
print("{:<15} {:<10}".format('Feature','Importance'))
for k, v in feature_importance.items():
print("{:<15} {:<10}".format(k, v))
if __name__ == "__main__":
filename = "labeled_dataset.csv"
# filename = input("Enter the filename: ")
names = ['ip.hdr_len', 'ip.flags.rb',\
'ip.flags.df', 'ip.flags.mf', 'ip.frag_offset', 'ip.ttl',\
'ip.len', 'tcp.seq', 'tcp.ack', 'tcp.len', \
'tcp.hdr_len', 'tcp.flags.fin', 'tcp.flags.syn', 'tcp.flags.reset',\
'tcp.flags.push', 'tcp.flags.ack', 'tcp.flags.urg', 'tcp.flags.cwr', 'tcp.window_size',\
'tcp.urgent_pointer', 'os']
df = read_csv(filename, usecols=names)
# no_of_best = int(input("Enter the no. of best features: "))
no_of_best = 10
print("")
univariate_stat(df, names, no_of_best)
print("")
recursive_feature_eliminate(df, names, no_of_best)
print("")
extra_tree_classifier(df, names)