-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab_compute_matrix.py
206 lines (154 loc) · 8.83 KB
/
tab_compute_matrix.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
import pandas as pd
import sys
import os
def simple_clf(true_label,predicted_label,total):
# Count occurrences where values in Column1 and Column2 are the same
same_values_count = (true_label == predicted_label).sum()
accuracy = same_values_count / total
return accuracy
def input_processor(true_label,predicted_label, total):
same_values_count = (true_label == predicted_label).sum()
accuracy = same_values_count / total
different_value = (true_label != predicted_label).sum()
missclassification = different_value/total
return accuracy,missclassification
folder_path = './results/tabular/NIDS/' # Replace this with the path to your folder
# Get a list of all files in the folder
files = os.listdir(folder_path)
# Filter out only the CSV files
csv_files = [file for file in files if file.endswith('.csv')]
output_file = 'output.txt'
if os.path.exists(output_file):
os.remove(output_file)
# Iterate through each CSV file
for csv_file in csv_files:
file_name = csv_file
print(file_name)
file_path = os.path.join(folder_path, csv_file)
# file_name = 'Result_with_ResNet_AlexNet.csv'
df = pd.read_csv(file_path)
length = len(df)
probability_criteria = 0.99996
probability_criteria_2 = 0.8
# Split the string based on the underscore character
split_values = file_name.split('_')
# Extract 'ResNet' and 'AlexNet'
binary_clf = split_values[0]
multiclass_clf = split_values[1].split('.')[0] # Removing the '.csv' extension
# Open a file in write mode to redirect the output
with open(output_file, 'a') as f:
# Redirect stdout to the file
sys.stdout = f
print("Binary CLF: ",binary_clf)
print("Multiclass CLF: ",multiclass_clf)
predict_clf_label = df['Predicted CLF']
# print("===========================")
print("Simple Class Classifier")
# print("===========================")
true_clf_label = df['True CLF Labels']
simple_accuracy = simple_clf(true_clf_label,predict_clf_label,length)
simple_miss = 1 - simple_accuracy
print(f'Accuracy: {simple_accuracy},Misclassification: {simple_miss}')
print("******************************************************************************")
# print("===========================")
print("Input Processor")
# print("===========================")
filtered_ip = df[df['Predicted Label'] == 1]
true_clf_label_filtered = filtered_ip['True CLF Labels']
predict_clf_label_filtered = filtered_ip['Predicted CLF']
ip_accuracy,ip_missclassification = input_processor(true_clf_label_filtered,predict_clf_label_filtered,length)
# print("Accuracy: ",ip_accuracy)
# print("Misclassification:", ip_missclassification)
df_phi = df[df['Predicted Label']== 0]
ip_phi = df_phi['Predicted Label'].count()/length
# print("Phi: ",ip_phi)
same_values_count = (df_phi['True CLF Labels'] == df_phi['Predicted CLF']).sum()
ip_phi_c = same_values_count/len(df_phi)
# print("Phi_c:",ip_phi_c)
diff_values_count = (df_phi['True CLF Labels'] != df_phi['Predicted CLF']).sum()
ip_phi_m = diff_values_count/len(df_phi)
# print("Phi_m:",ip_phi_m)
print(f'Accuracy: {ip_accuracy}, Misclassification: {ip_missclassification}, Phi: {ip_phi}, Phi_c: {ip_phi_c}, Phi_m: {ip_phi_m}')
print("******************************************************************************")
# print("===========================")
print(f"Output Processor with {probability_criteria}")
# print("===========================")
filtered_op = df[df['Probability'] >= probability_criteria]
true_clf_label_filtered = filtered_op['True CLF Labels']
predict_clf_label_filtered = filtered_op['Predicted CLF']
op_accuracy,op_missclassification = input_processor(true_clf_label_filtered,predict_clf_label_filtered,length)
# print("Accuracy: ",op_accuracy)
# print("Misclassification:", op_missclassification)
df_phi = df[df['Probability']< probability_criteria]
op_phi = df_phi['Predicted Label'].count()/length
# print("Phi: ",op_phi)
same_values_count = (df_phi['True CLF Labels'] == df_phi['Predicted CLF']).sum()
op_phi_c = same_values_count/len(df_phi)
# print("Phi_c:", op_phi_c)
diff_values_count = (df_phi['True CLF Labels'] != df_phi['Predicted CLF']).sum()
op_phi_m = diff_values_count/len(df_phi)
# print("Phi_m:", op_phi_m)
print(f'Accuracy: {op_accuracy}, Misclassification: {op_missclassification}, Phi: {op_phi}, Phi_c: {op_phi_c}, Phi_m: {op_phi_m}')
print("******************************************************************************")
# print("===========================")
print(f"Safety Wrapper with {probability_criteria}")
# print("===========================")
filtered_sw = df[(df['Predicted Label'] == 1) & (df['Probability'] >= probability_criteria)]
true_clf_label_filtered = filtered_sw['True CLF Labels']
predict_clf_label_filtered = filtered_sw['Predicted CLF']
sw_accuracy,sw_missclassification = input_processor(true_clf_label_filtered,predict_clf_label_filtered,length)
# print("Accuracy: ",sw_accuracy)
# print("Misclassification:", sw_missclassification)
df_phi = df[(df['Predicted Label'] == 0) | (df['Probability'] < probability_criteria)]
sw_phi = df_phi['Predicted Label'].count()/length
# print("Phi: ",sw_phi)
same_values_count = (df_phi['True CLF Labels'] == df_phi['Predicted CLF']).sum()
sw_phi_c = same_values_count/len(df_phi)
# print("Phi_c:",sw_phi_c)
diff_values_count = (df_phi['True CLF Labels'] != df_phi['Predicted CLF']).sum()
sw_phi_m = diff_values_count/len(df_phi)
# print("Phi_m:",sw_phi_m)
print(f'Accuracy: {sw_accuracy}, Misclassification: {sw_missclassification}, Phi: {sw_phi}, Phi_c: {sw_phi_c}, Phi_m: {sw_phi_m}')
print("######################################################################")
print("===========================")
print(f"Output Processor {probability_criteria_2}")
print("===========================")
filtered_op2 = df[df['Probability'] >= probability_criteria_2]
true_clf_label_filtered = filtered_op2['True CLF Labels']
predict_clf_label_filtered = filtered_op2['Predicted CLF']
op_accuracy, op_missclassification = input_processor(true_clf_label_filtered, predict_clf_label_filtered, length)
# print("Accuracy: ", op_accuracy)
# print("Misclassification:", op_missclassification)
df_phi = df[df['Probability'] < probability_criteria_2]
op_phi = df_phi['Predicted Label'].count() / length
# print("Phi: ", op_phi)
same_values_count = (df_phi['True CLF Labels'] == df_phi['Predicted CLF']).sum()
op_phi_c = same_values_count / len(df_phi)
# print("Phi_c:", op_phi_c)
diff_values_count = (df_phi['True CLF Labels'] != df_phi['Predicted CLF']).sum()
op_phi_m = diff_values_count / len(df_phi)
# print("Phi_m:", op_phi_m)
print(f'Accuracy: {op_accuracy}, Misclassification: {op_missclassification}, Phi: {op_phi}, Phi_c: {op_phi_c}, Phi_m: {op_phi_m}')
print("******************************************************************************")
# print("===========================")
print(f'Safety Wrapper with {probability_criteria_2}')
# print("===========================")
filtered_sw2 = df[(df['Predicted Label'] == 1) & (df['Probability'] >= probability_criteria_2)]
true_clf_label_filtered = filtered_sw2['True CLF Labels']
predict_clf_label_filtered = filtered_sw2['Predicted CLF']
sw_accuracy, sw_missclassification = input_processor(true_clf_label_filtered, predict_clf_label_filtered, length)
# print("Accuracy: ", sw_accuracy)
# print("Misclassification:", sw_missclassification)
df_phi = df[(df['Predicted Label'] == 0) | (df['Probability'] < probability_criteria_2)]
sw_phi = df_phi['Predicted Label'].count() / length
# print("Phi: ", sw_phi)
same_values_count = (df_phi['True CLF Labels'] == df_phi['Predicted CLF']).sum()
sw_phi_c = same_values_count / len(df_phi)
# print("Phi_c:", sw_phi_c)
diff_values_count = (df_phi['True CLF Labels'] != df_phi['Predicted CLF']).sum()
sw_phi_m = diff_values_count / len(df_phi)
# print("Phi_m:", sw_phi_m)
print(f'Accuracy: {sw_accuracy}, Misclassification: {sw_missclassification}, Phi: {sw_phi}, Phi_c: {sw_phi_c}, Phi_m: {sw_phi_m}')
print("######################################################################")
# Reset stdout to the console
sys.stdout = sys.__stdout__