-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval.py
64 lines (58 loc) · 2.04 KB
/
eval.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
import pandas as pd
import numpy as np
import pickle
# load the test data from disk
testing_df = pd.read_pickle("testing_df.pkl")
normal, dos, r2l, u2r, probe = [], [], [], [], []
data = testing_df.values
for row in data:
if row[38]== "normal":
row = np.delete(row, 38)
normal.append(row.reshape(1,122))
if row[38]=="dos":
row = np.delete(row, 38)
dos.append(row.reshape(1,122))
if row[38]== "r2l":
row = np.delete(row, 38)
r2l.append(row.reshape(1,122))
if row[38]== "u2r":
row = np.delete(row, 38)
u2r.append(row.reshape(1,122))
if row[38]== "probe":
row = np.delete(row, 38)
probe.append(row.reshape(1,122))
# load the trained model from disk
filename = "random_forest_model.sav"
random_forest_model = pickle.load(open(filename, 'rb'))
filename2 = "Linear_SVM_model.sav"
svm_model = pickle.load(open(filename2, 'rb'))
def main(class_name):
if class_name=="normal":
ind = np.random.randint(0,9709)
test = normal[ind]
if class_name=="dos":
ind = np.random.randint(0,7635)
test = dos[ind]
if class_name=="r2l":
ind = np.random.randint(0,2708)
test = r2l[ind]
if class_name=="u2r":
ind = np.random.randint(0,66)
test = u2r[ind]
if class_name=="probe":
ind = np.random.randint(0,2420)
test = probe[ind]
print(ind)
predictions = random_forest_model.predict(test)
probabilities = random_forest_model.predict_proba(test)
probabilities = probabilities[0]
predictionssvm = svm_model.predict(test)
probabilitiessvm = svm_model.predict_proba(test)
probabilitiessvm = probabilitiessvm[0]
for i in range(5):
probabilities[i] = "{:.2f}".format(probabilities[i]*100)
print(predictions[0], list(probabilities))
for i in range(5):
probabilitiessvm[i] = "{:.2f}".format(probabilitiessvm[i]*100)
print(predictionssvm[0], list(probabilitiessvm))
return(predictions[0], list(probabilities), predictionssvm[0], list(probabilitiessvm))