-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintrusion_detection_system(1).py
294 lines (238 loc) · 10.9 KB
/
intrusion_detection_system(1).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
# -*- coding: utf-8 -*-
"""intrusion detection system
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1kyt50JZi3xZa8yRFvHBtmNPxHqaBSsHn
"""
#n -import important packages (this might change as we move forward with the project)
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import time
from sklearn.model_selection import cross_val_score
#N - updated: libraries for Evaluate and measure the accuracy of the model
from sklearn.metrics import accuracy_score, f1_score, recall_score, precision_score, confusion_matrix
#n -libraries for the files in google drive
from pydrive.auth import GoogleAuth
from google.colab import drive
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
from google.colab import drive
drive.mount('/content/drive')
#load dataset
df = pd.read_csv('/content/drive/MyDrive/kddcup99_csv.csv')
df.columns
df.info() #n
#n -check for any missing values
print(df.isnull().sum())
duplicates = df.duplicated()
print('Number of duplicate entries:', duplicates.sum())
#Remove duplicate rows
df = df.drop_duplicates()
print('Number of duplicate entries after removing:', df.duplicated().sum())
df.label.value_counts()
#AlAnoud AlJebreen -Convert categorical data into numerical data
df = pd.get_dummies(df, columns=['protocol_type', 'service', 'flag', 'label'])
#NS - Normalization of dataset
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
df_normalized = scaler.fit_transform(df.drop('label_normal', axis=1))
df_normalized = pd.DataFrame(df_normalized, columns=df.drop('label_normal', axis=1).columns)
df_normalized = pd.concat([df_normalized, df['label_normal']], axis=1)
df.to_csv('newkddcup99.csv', index=False)
#NS
corr = df.corr()
plt.figure(figsize =(15, 12))
sns.heatmap(corr)
plt.show()
#UPDATE: kept the unimportant columns to introduce a little noise
#df.drop('lnum_access_files', axis = 1, inplace = True)
df.drop('is_guest_login', axis = 1, inplace = True)
#NS - This variable is highly correlated with rerror_rate and should be ignored for analysis.
df.drop('srv_rerror_rate', axis = 1, inplace = True)
#NS - This variable is highly correlated with srv_serror_rate and should be ignored for analysis.
df.drop('dst_host_srv_serror_rate', axis = 1, inplace = True)
#NS - This variable is highly correlated with rerror_rate and should be ignored for analysis.
df.drop('dst_host_serror_rate', axis = 1, inplace = True)
#NS - This variable is highly correlated with srv_rerror_rate and should be ignored for analysis.
df.drop('dst_host_rerror_rate', axis = 1, inplace = True)
#NS - This variable is highly correlated with rerror_rate and should be ignored for analysis.
df.drop('dst_host_srv_rerror_rate', axis = 1, inplace = True)
#NS - This variable is highly correlated with srv_rerror_rate and should be ignored for analysis.
df.drop('dst_host_same_srv_rate', axis = 1, inplace = True)
#df
#AlAnoud AlJebreen -Feature Selection PCA
from sklearn.decomposition import PCA
x = df.drop('label_normal', axis=1)
y = df['label_normal']
pca = PCA(n_components=3)
x_pca = pca.fit_transform(x)
#AlAnoud AlJebreen -Feature Selection RFE
from sklearn.feature_selection import RFE
from sklearn.linear_model import LinearRegression
x = df.drop('label_normal', axis=1)
y = df['label_normal']
model = LinearRegression()
rfe = RFE(model, n_features_to_select=3)
rfe.fit(x,y)
#SK - Split the dataset into training and testing sets (40% for test data and 60% for train data)
X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size=0.4, random_state=42)
#print the shapes of the training and test sets
print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape)
#SK - Save the training and testing datasets into separate CSV files
X_train.to_csv('train_data.csv', index=False)
X_test.to_csv('test_data.csv', index=False)
Y_train.to_csv('train_labels.csv', index=False)
Y_test.to_csv('test_labels.csv', index=False)
#SK - First Model: Decision Tree classifier
from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier()
#SK - Train the model using the training data
#NS - Added specific columns for training and testing
#X_train_subset = X_train[['label_phf', 'label_pod', 'label_portsweep', 'label_rootkit',
#'label_satan', 'label_smurf', 'label_spy', 'label_teardrop',
#'label_warezclient', 'label_warezmaster']]
start = time.time()
dtc.fit(X_train, Y_train)
print("Processing time for Training using Decision Tree Classifier: %s seconds " % (time.time() - start))
#SK - Make predictions on the test data
#NS - Added specific columns for training and testing
#X_test_subset = X_test[['label_phf', 'label_pod', 'label_portsweep', 'label_rootkit',
#'label_satan', 'label_smurf', 'label_spy', 'label_teardrop',
#'label_warezclient', 'label_warezmaster']]
start = time.time()
Y_pred = dtc.predict(X_test)
print("Processing time for Testing using Decision Tree Classifier: %s seconds " % (time.time() - start))
#SK - Calculate the accuracy, recall, precision.
#NS - Updated - calculate f1-score, confusion matrix
accuracy = accuracy_score(Y_test, Y_pred)
recall= recall_score(Y_test, Y_pred )
precision= precision_score(Y_test, Y_pred )
f1score = f1_score(Y_test, Y_pred)
conf_matrix = confusion_matrix(Y_test, Y_pred)
#NS - Perform cross-validation and get the mean accuracy score
scores = cross_val_score(dtc, X_train, Y_train, cv=10)
print("Cross-validation scores:", scores)
print("Mean cross-validation score:", scores.mean())
#SK - Print the results
#NS - Updated the decimal required for better comparison
print("Decision Tree Classifier:")
print("The accuracy of the model is : {:.4f}%".format(accuracy*100))
print("Recall = {:.4f} " .format(recall*100))
print("Precison = {:.4f} ".format(precision*100))
print("F1-score: ", f1score)
print("Confusion Matrix:\n", conf_matrix)
#NS - Second Model: Random Forest
from sklearn.ensemble import RandomForestClassifier
#NS - Create a random forest classifier with 100 trees
rfc = RandomForestClassifier(n_estimators=100)
#NS - Train the model using the training data
start = time.time()
rfc.fit(X_train, Y_train)
print("Processing time for Training using Random Forest Classifier: %s seconds " % (time.time() - start))
#NS - Make predictions on the test data
start = time.time()
Y_pred = rfc.predict(X_test)
print("Processing time for Testing using Random Forest Classifier: %s seconds " % (time.time() - start))
#NS - Calculate the accuracy, f1-score,recall, precision and confusion matrix
accuracy = accuracy_score(Y_test, Y_pred)
recall= recall_score(Y_test, Y_pred )
precision= precision_score(Y_test, Y_pred )
f1score = f1_score(Y_test, Y_pred)
conf_matrix = confusion_matrix(Y_test, Y_pred)
#NS - Perform cross-validation and get the mean accuracy score
scores = cross_val_score(dtc, X_train, Y_train, cv=10)
print("Cross-validation scores:", scores)
print("Mean cross-validation score:", scores.mean())
#NS - Print the results
print("Random Forest Classifier:")
print("The accuracy of the model is : {:.4f}%".format(accuracy*100))
print("Recall = {:.4f} " .format(recall*100))
print("Precison = {:.4f} ".format(precision*100))
print("F1-score: ", f1score)
print("Confusion Matrix:\n", conf_matrix)
#NS Third Model - Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB
gbc = GaussianNB()
#NS - Train the model using the training data
start = time.time()
gbc.fit(X_train, Y_train)
print("Processing time for Training using Gaussian Naive Bayes Classifier: %s seconds " % (time.time() - start))
#NS - Make predictions on the test data
start = time.time()
Y_pred_gbc = gbc.predict(X_test)
print("Processing time for Testing using Gaussian Naive Bayes Classifier: %s seconds " % (time.time() - start))
#NS - Calculate the accuracy, f1-score,recall, precision and confusion matrix
accuracy = accuracy_score(Y_test, Y_pred_gbc)
recall= recall_score(Y_test, Y_pred_gbc )
precision= precision_score(Y_test, Y_pred_gbc )
f1score = f1_score(Y_test, Y_pred_gbc)
conf_matrix = confusion_matrix(Y_test, Y_pred_gbc)
#NS - Perform cross-validation and get the mean accuracy score
scores = cross_val_score(dtc, X_train, Y_train, cv=10)
print("Cross-validation scores:", scores)
print("Mean cross-validation score:", scores.mean())
#NS - Print the results
print("Gaussian Naive Bayes Classifier:")
print("The accuracy of the model is : {:.4f}%".format(accuracy*100))
print("Recall = {:.4f} " .format(recall*100))
print("Precison = {:.4f} ".format(precision*100))
print("F1-score: ", f1score)
print("Confusion Matrix:\n", conf_matrix)
#NS Fourth Model - XGBoost Classifier
from xgboost import XGBClassifier
xgb = XGBClassifier()
#NS - Train the model using the training data
start = time.time()
xgb.fit(X_train, Y_train)
print("Processing time for Training using XGBoost Classifier: %s seconds " % (time.time() - start))
#NS - Make predictions on the test data
start = time.time()
Y_pred_xgb = xgb.predict(X_test)
print("Processing time for Testing using XGBoost Classifier: %s seconds " % (time.time() - start))
#NS - Calculate the accuracy, f1-score,recall and precision
accuracy = accuracy_score(Y_test, Y_pred_xgb)
recall= recall_score(Y_test, Y_pred_xgb )
precision= precision_score(Y_test, Y_pred_xgb )
f1score = f1_score(Y_test, Y_pred_xgb)
conf_matrix = confusion_matrix(Y_test, Y_pred_xgb)
#NS - Perform cross-validation and get the mean accuracy score
scores = cross_val_score(dtc, X_train, Y_train, cv=10)
print("Cross-validation scores:", scores)
print("Mean cross-validation score:", scores.mean())
#NS - Print the results
print("XG Boost Classifier:")
print("The accuracy of the model is : {:.4f}%".format(accuracy*100))
print("Recall = {:.4f} " .format(recall*100))
print("Precison = {:.4f} ".format(precision*100))
print("F1-score: ", f1score)
print("Confusion Matrix:\n", conf_matrix)
import matplotlib.pyplot as plt
import numpy as np
#NS - Define metrics for all models
metrics = {
'Decision Tree': [0.7656, 0.6203, 1.00, 0.6304],
'Random Forest': [0.9998, 0.9997, 0.9999, 0.9998],
'Gaussian Naive Bayes': [0.8277, 0.7071, 0.9980, 0.7492],
'XGBoost': [0.9997, 0.9997, 0.9997, 0.9997]
}
#NS - Create a bar plot
fig, ax = plt.subplots(figsize=(10, 6))
bar_width = 0.2
opacity = 0.8
index = np.arange(len(metrics))
rects1 = ax.bar(index, metrics['Decision Tree'], bar_width, alpha=opacity, color='b', label='Decision Tree')
rects2 = ax.bar(index + bar_width, metrics['Random Forest'], bar_width, alpha=opacity, color='g', label='Random Forest')
rects3 = ax.bar(index + 2*bar_width, metrics['Gaussian Naive Bayes'], bar_width, alpha=opacity, color='r', label='Gaussian Naive Bayes')
rects4 = ax.bar(index + 3*bar_width, metrics['XGBoost'], bar_width, alpha=opacity, color='c', label='XGBoost')
ax.set_xlabel('Metrics')
ax.set_ylabel('Scores')
ax.set_title('Comparison of ML Models')
ax.set_xticks(index + bar_width)
ax.set_xticklabels(('F1-score', 'Precision', 'Recall', 'Accuracy'))
ax.legend()
plt.tight_layout()
plt.show()