-
Notifications
You must be signed in to change notification settings - Fork 45
/
type_test.py
159 lines (116 loc) · 5.71 KB
/
type_test.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
import json
import numpy as np
import pandas as pd
from keras.models import model_from_json
from typeAD import RLenv
import matplotlib.pyplot as plt
from typeAD import huber_loss
import itertools
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.metrics import confusion_matrix
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
if __name__ == "__main__":
formated_test_path = "../../datasets/formated/formated_test_type.data"
with open("models/type_model.json", "r") as jfile:
model = model_from_json(json.load(jfile))
model.load_weights("models/type_model.h5")
model.compile(loss=huber_loss,optimizer="sgd")
env = RLenv('test',formated_test_path = formated_test_path)
total_reward = 0
true_labels = np.zeros(len(env.attack_types),dtype=int)
estimated_labels = np.zeros(len(env.attack_types),dtype=int)
estimated_correct_labels = np.zeros(len(env.attack_types),dtype=int)
states , labels = env.get_full()
q = model.predict(states)
actions = np.argmax(q,axis=1)
true_labels += np.sum(labels).values
for indx,a in enumerate(actions):
estimated_labels[a] +=1
if a == np.argmax(labels.iloc[indx].values):
total_reward += 1
estimated_correct_labels[a] += 1
action_dummies = pd.get_dummies(actions)
posible_actions = np.arange(len(env.attack_types))
for non_existing_action in posible_actions:
if non_existing_action not in action_dummies.columns:
action_dummies[non_existing_action] = np.uint8(0)
normal_f1_score = f1_score(labels['normal'].values,action_dummies[0].values)
dos_f1_score = f1_score(labels['DoS'].values,action_dummies[1].values)
probe_f1_score = f1_score(labels['Probe'].values,action_dummies[2].values)
r2l_f1_score = f1_score(labels['R2L'].values,action_dummies[3].values)
u2r_f1_score = f1_score(labels['U2R'].values,action_dummies[4].values)
Accuracy = [normal_f1_score,dos_f1_score,probe_f1_score,r2l_f1_score,u2r_f1_score]
Mismatch = abs(estimated_correct_labels - true_labels)+abs(estimated_labels-estimated_correct_labels)
print('\r\nTotal reward: {} | Number of samples: {} | Accuracy = {}%'.format(total_reward,
len(states),float(100*total_reward/len(states))))
outputs_df = pd.DataFrame(index = env.attack_types,columns = ["Estimated","Correct","Total","F1_score","Mismatch"])
for indx,att in enumerate(env.attack_types):
outputs_df.iloc[indx].Estimated = estimated_labels[indx]
outputs_df.iloc[indx].Correct = estimated_correct_labels[indx]
outputs_df.iloc[indx].Total = true_labels[indx]
outputs_df.iloc[indx].F1_score = Accuracy[indx]*100
outputs_df.iloc[indx].Mismatch = abs(Mismatch[indx])
print(outputs_df)
#%%
fig, ax = plt.subplots()
width = 0.35
pos = np.arange(len(true_labels))
p1 = plt.bar(pos, estimated_correct_labels,width,color='g')
p1 = plt.bar(pos+width,
(np.abs(estimated_correct_labels-true_labels)),width,
color='r')
p2 = plt.bar(pos+width,np.abs(estimated_labels-estimated_correct_labels),width,
bottom=(np.abs(estimated_correct_labels-true_labels)),
color='b')
ax.set_xticks(pos+width/2)
ax.set_xticklabels(env.attack_types,rotation='vertical')
#ax.set_yscale('log')
#ax.set_ylim([0, 100])
ax.set_title('Test set scores, Acc = {:.2f}'.format(100*total_reward/len(states)))
plt.legend(('Correct estimated','False negative','False positive'))
plt.tight_layout()
#plt.show()
plt.savefig('results/test_type_improved.svg', format='svg', dpi=1000)
#%% Agregated precision
aggregated_data_test = np.argmax(labels.values,axis=1)
print('Performance measures on Test data')
print('Accuracy = {:.4f}'.format(accuracy_score( aggregated_data_test,actions)))
print('F1 = {:.4f}'.format(f1_score(aggregated_data_test,actions, average='weighted')))
print('Precision_score = {:.4f}'.format(precision_score(aggregated_data_test,actions, average='weighted')))
print('recall_score = {:.4f}'.format(recall_score(aggregated_data_test,actions, average='weighted')))
cnf_matrix = confusion_matrix(aggregated_data_test,actions)
np.set_printoptions(precision=2)
plt.figure()
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=env.attack_types, normalize=True,
title='Normalized confusion matrix')
plt.savefig('results/confusion_matrix_type_imp.svg', format='svg', dpi=1000)