-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcap_parser.py
163 lines (143 loc) · 4.32 KB
/
pcap_parser.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
# Specify the CSV file path
csv_file_path = '/dataset/output_ndpi_training_testing.csv'
# Import necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Load the dataset
dataset = pd.read_csv(csv_file_path)
# Separate features and target
X = dataset.drop('Category', axis = 1)
y = dataset['Category']
# Split the dataset into an 90-10 training-test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 42)
# Create an instance of the StandardScaler class
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
weights = np.array([[0.2, 0, 0, 0, 0], [0, 0.3, 0, 0, 0], [0, 0, 0.2, 0, 0], [0, 0, 0, 0.2, 0], [0, 0, 0, 0, 0.1]])
# Fit the StandardScaler on the features from the training set and transform it
X_train = sc.fit_transform(X_train)
X_train = np.dot(X_train, weights)
# Apply the transform to the test set
X_test = sc.transform(X_test)
X_test = np.dot(X_test, weights)
#logistic regression
def logisticRegression():
from sklearn.linear_model import LogisticRegression
classifierLG = LogisticRegression(random_state = 0)
classifierLG.fit(X_train, y_train)
y_pred = classifierLG.predict(X_test)
from sklearn.metrics import accuracy_score
yt =[]
yp = []
j = -1
for i in y_test:
j += 1
if i=="Network":
continue
yt.append(i)
yp.append(y_pred[j])
print(accuracy_score(yt, yp))
#K-NN
def KNearestNeighbours():
from sklearn.neighbors import KNeighborsClassifier
classifierKNN = KNeighborsClassifier(n_neighbors=2, metric='minkowski', p=1)
classifierKNN.fit(X_train, y_train)
y_pred = classifierKNN.predict(X_test)
from sklearn.metrics import accuracy_score
yt =[]
yp = []
j = -1
for i in y_test:
j += 1
if i=="Network":
continue
yt.append(i)
yp.append(y_pred[j])
print(accuracy_score(yt, yp))
#SVM
def SVM():
from sklearn.svm import SVC
classifierSVM = SVC(kernel = 'linear', random_state = 0)
classifierSVM.fit(X_train, y_train)
y_pred = classifierSVM.predict(X_test)
from sklearn.metrics import accuracy_score
yt = []
yp = []
j = -1
for i in y_test:
j += 1
if i=="Network":
continue
yt.append(i)
yp.append(y_pred[j])
print(accuracy_score(yt, yp))
#Kernel SVM
def KernelSVM():
from sklearn.svm import SVC
classifierKSVM = SVC(kernel = 'rbf', random_state = 0)
classifierKSVM.fit(X_train, y_train)
y_pred = classifierKSVM.predict(X_test)
from sklearn.metrics import accuracy_score
yt = []
yp = []
j = -1
for i in y_test:
j += 1
if i=="Network":
continue
yt.append(i)
yp.append(y_pred[j])
print(accuracy_score(yt, yp))
#Naive Baeyes
def NaiveBaeyes():
from sklearn.naive_bayes import GaussianNB
classifierNB = GaussianNB()
classifierNB.fit(X_train, y_train)
y_pred = classifierNB.predict(X_test)
from sklearn.metrics import accuracy_score
yt = []
yp = []
j = -1
for i in y_test:
j += 1
if i=="Network":
continue
yt.append(i)
yp.append(y_pred[j])
print(accuracy_score(yt, yp))
#Decision Tree
def DecisionTree():
from sklearn.tree import DecisionTreeClassifier
classifierDT = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
classifierDT.fit(X_train, y_train)
y_pred = classifierDT.predict(X_test)
from sklearn.metrics import accuracy_score
yt = []
yp = []
j = -1
for i in y_test:
j += 1
if i=="Network":
continue
yt.append(i)
yp.append(y_pred[j])
print(accuracy_score(yt, yp))
#Random Forest Classifier
def RandomForestClassifier():
from sklearn.ensemble import RandomForestClassifier
classifierRF = RandomForestClassifier(n_estimators = 100, criterion = 'entropy', random_state = 0)
classifierRF.fit(X_train, y_train)
y_pred = classifierRF.predict(X_test)
from sklearn.metrics import accuracy_score
yt = []
yp = []
j = -1
for i in y_test:
j += 1
if i=="Network":
continue
yt.append(i)
yp.append(y_pred[j])
print(accuracy_score(yt, yp))