-
Notifications
You must be signed in to change notification settings - Fork 0
/
Neural-Network-Regression.py
298 lines (233 loc) · 10.7 KB
/
Neural-Network-Regression.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
# import pandas as pd
# import numpy as np
# import tensorflow as tf
# from sklearn.preprocessing import StandardScaler, LabelEncoder
# from sklearn.model_selection import train_test_split
# from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, confusion_matrix
# import joblib
# # Load the test data
# testData = pd.read_csv("D:/University/Year 3/CS3072-CS3605 FYP/Perseus-FYP/Datasets/Balanced-SYN-V2.csv")
# data = testData.dropna().copy()
# data.replace([np.inf, -np.inf], np.nan, inplace=True)
# # Selecting the top features identified by Random Forest
# selected_features = [
# 'Source Port', 'Destination Port',
# 'Protocol', 'Flow Duration', 'Total Fwd Packets', 'Total Backward Packets',
# 'Flow Bytes/s', 'Flow Packets/s'
# ]
# x = data[selected_features]
# y = data['Label']
# # Scaling the features
# scaler = StandardScaler()
# x_scaled = scaler.fit_transform(x)
# # Encoding the target variable Normal vs SYN Flood
# label_encoder = LabelEncoder()
# y_encoded = label_encoder.fit_transform(y)
# # Splitting the data into training and testing sets
# x_train, x_test, y_train, y_test = train_test_split(x_scaled, y_encoded, test_size=0.2, random_state=42)
# # Neural network model
# Perseus = tf.keras.models.Sequential([
# tf.keras.layers.Dense(256, activation='relu', input_shape=(8,)),
# tf.keras.layers.Dense(128, activation='relu'),
# tf.keras.layers.Dense(128, activation='linear'),
# tf.keras.layers.Dropout(0.3),
# tf.keras.layers.Dense(64, activation='relu'),
# tf.keras.layers.Dense(64, activation='linear'),
# tf.keras.layers.Dense(32, activation='relu'),
# tf.keras.layers.Dropout(0.3),
# tf.keras.layers.Dense(1, activation='sigmoid')
# ])
# # Compile the model
# Perseus.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss='binary_crossentropy', metrics=['accuracy'])
# # Assuming x_train, y_train, x_test, y_test are properly prepared
# Perseus.fit(x_train, y_train, batch_size= 32, epochs=10)
# # Evaluate the model
# y_pred = Perseus.predict(x_test)
# y_pred_binary = (y_pred > 0.5).astype(int)
# # Generating the confusion matrix
# cm = confusion_matrix(y_test, y_pred_binary)
# cm_df = pd.DataFrame(cm, index=['Actual 0', 'Actual 1'], columns=['Predicted 0', 'Predicted 1'])
# print("Confusion Matrix:")
# print(cm_df)
# # Calculate different metrics
# accuracy = accuracy_score(y_test, y_pred_binary)
# precision = precision_score(y_test, y_pred_binary)
# recall = recall_score(y_test, y_pred_binary)
# f1 = f1_score(y_test, y_pred_binary)
# print(f"Test Accuracy: {accuracy * 100:.2f}%")
# print(f"Precision: {precision:.2f}")
# print(f"Recall: {recall:.2f}")
# print(f"F1 Score: {f1:.2f}")
# #saving the scaler
# joblib.dump(scaler, 'scaler.save')
# # saving the model
# Perseus.save('NN-model')
######################################################pretty decent model ############################
# import pandas as pd
# import numpy as np
# import tensorflow as tf
# from sklearn.preprocessing import LabelEncoder, MinMaxScaler
# from sklearn.model_selection import train_test_split
# from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, confusion_matrix
# import joblib
# # Load the test data
# testData = pd.read_csv("D:/University/Year 3/CS3072-CS3605 FYP/Perseus-FYP/Datasets/Balanced-SYN-V2.csv")
# data = testData.copy()
# # Handle missing and infinite values
# data.replace([np.inf, -np.inf], np.nan, inplace=True)
# data.dropna(subset=['Label'], inplace=True) # Ensure labels are not NaN
# data.fillna(0, inplace=True) # Fill NaN values with a placeholder (e.g., 0)
# # Selecting the top features identified by Random Forest
# selected_features = [
# 'Source Port', 'Destination Port', 'Protocol', 'Flow Duration',
# 'Total Fwd Packets', 'Total Backward Packets', 'Flow Bytes/s', 'Flow Packets/s'
# ]
# x = data[selected_features]
# y = data['Label']
# # Scaling the features with RobustScaler
# scaler = MinMaxScaler()
# x_scaled = scaler.fit_transform(x)
# # Encoding the target variable Normal vs SYN Flood
# label_encoder = LabelEncoder()
# y_encoded = label_encoder.fit_transform(y)
# # Splitting the data into training and testing sets
# x_train, x_test, y_train, y_test = train_test_split(x_scaled, y_encoded, test_size=0.2, random_state=42)
# # Neural network model
# Perseus = tf.keras.models.Sequential([
# tf.keras.layers.Dense(256, activation='relu', input_shape=(8,)),
# tf.keras.layers.Dense(128, activation='relu'),
# tf.keras.layers.Dense(128, activation='relu'),
# tf.keras.layers.Dense(128, activation='relu'),
# tf.keras.layers.Dense(64, activation='relu'),
# tf.keras.layers.Dense(32, activation='relu'),
# tf.keras.layers.Dropout(0.3),
# tf.keras.layers.Dense(256, activation='relu'),
# tf.keras.layers.Dense(128, activation='relu'),
# tf.keras.layers.Dense(128, activation='relu'),
# tf.keras.layers.Dense(64, activation='relu'),
# tf.keras.layers.Dense(32, activation='relu'),
# tf.keras.layers.Dense(32, activation='relu'),
# tf.keras.layers.Dropout(0.3),
# tf.keras.layers.Dense(32, activation='relu'),
# tf.keras.layers.Dense(128, activation='relu'),
# tf.keras.layers.Dense(1, activation='sigmoid')
# ])
# # Compile the model with updated learning rate parameter
# Perseus.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])
# # Train the model
# Perseus.fit(x_train, y_train, batch_size=50, epochs=15)
# # Evaluate the model
# y_pred = Perseus.predict(x_test)
# y_pred_binary = (y_pred > 0.5).astype(int)
# # Generating the confusion matrix
# cm = confusion_matrix(y_test, y_pred_binary)
# cm_df = pd.DataFrame(cm, index=['Actual 0', 'Actual 1'], columns=['Predicted 0', 'Predicted 1'])
# print("Confusion Matrix:")
# print(cm_df)
# # Calculate different metrics
# accuracy = accuracy_score(y_test, y_pred_binary)
# precision = precision_score(y_test, y_pred_binary)
# recall = recall_score(y_test, y_pred_binary)
# f1 = f1_score(y_test, y_pred_binary)
# print(f"Test Accuracy: {accuracy * 100:.2f}%")
# print(f"Precision: {precision:.2f}")
# print(f"Recall: {recall:.2f}")
# print(f"F1 Score: {f1:.2f}")
# # Saving the scaler
# joblib.dump(scaler, 'scaler.save')
# # Saving the model
# Perseus.save('NN-model')
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, confusion_matrix, roc_auc_score
import joblib
import matplotlib.pyplot as plt
# Load the test data
testData = pd.read_csv("D:/University/Year 3/CS3072-CS3605 FYP/Perseus-FYP/Datasets/Balanced-SYN-V2.csv")
data = testData.copy()
# Handle missing and infinite values
data.replace([np.inf, -np.inf], np.nan, inplace=True)
data.dropna(subset=['Label'], inplace=True) # Ensure labels are not NaN
data.fillna(0, inplace=True) # Fill NaN values with a placeholder (e.g., 0)
# Selecting the top features identified by Random Forest
selected_features = [
'Source Port', 'Destination Port', 'Protocol', 'Flow Duration',
'Total Fwd Packets', 'Total Backward Packets', 'Flow Bytes/s', 'Flow Packets/s'
]
x = data[selected_features]
y = data['Label']
# Scaling the features with MinMaxScaler
scaler = MinMaxScaler()
x_scaled = scaler.fit_transform(x)
# Encoding the target variable Normal vs SYN Flood
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)
# Splitting the data into training and testing sets
x_train, x_test, y_train, y_test = train_test_split(x_scaled, y_encoded, test_size=0.2, random_state=42)
# Neural network model
Perseus = tf.keras.models.Sequential([
tf.keras.layers.Dense(256, activation='relu', input_shape=(8,)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
# Compile the model with updated learning rate parameter
Perseus.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])
# Train the model with validation split
history = Perseus.fit(x_train, y_train, batch_size=50, epochs=15, validation_split=0.2)
# Evaluate the model
y_pred = Perseus.predict(x_test)
y_pred_binary = (y_pred > 0.5).astype(int)
# Generating the confusion matrix
cm = confusion_matrix(y_test, y_pred_binary)
cm_df = pd.DataFrame(cm, index=['Predicted Negative', 'Predicted Positive'], columns=['Actual Negative', 'Actual Positive'])
# Visualization of the confusion matrix with color
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation='nearest', cmap='Blues') # Use the 'Blues' colormap for light to dark blue
plt.title('NN model, Confusion Matrix')
plt.colorbar()
tick_marks = np.arange(len(cm_df.columns))
plt.xticks(tick_marks, cm_df.columns, rotation=45)
plt.yticks(tick_marks, cm_df.index)
# Labeling the plot
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
plt.text(j, i, cm[i, j], ha="center", va="center", color="white" if cm[i, j] > cm.max()/2. else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
# Calculate different metrics
accuracy = accuracy_score(y_test, y_pred_binary)
precision = precision_score(y_test, y_pred_binary)
recall = recall_score(y_test, y_pred_binary)
f1 = f1_score(y_test, y_pred_binary)
print(f"Test Accuracy: {accuracy * 100:.2f}%")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"F1 Score: {f1:.2f}")
# Print validation accuracy and loss
val_accuracy = history.history['val_accuracy'][-1]
val_loss = history.history['val_loss'][-1]
print(f"Validation Accuracy: {val_accuracy * 100:.2f}%")
print(f"Validation Loss: {val_loss:.2f}")
# Saving the scaler
joblib.dump(scaler, 'scaler.save')
# Saving the model
Perseus.save('NN-model')