-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.py
55 lines (43 loc) · 1.44 KB
/
App.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
from Classifier import Classififer
import pandas as pd
import numpy as np
from Dataset import Dataset
import os
# Get the path separator for the current system
os_sep = os.path.sep
class App:
def __init__(self):
self.classifier = Classififer().get_classifier() # DecisionTreeClassifier 决策树分类器
# 对模型进行训练
def train(self):
df = pd.read_csv('data' + os_sep + 'train.csv', header=None)
data = np.array(df)
self.x_train = data[:, :-1]
self.y_train = data[:, -1:]
self.classifier.fit(self.x_train, self.y_train) # fit 是 拟合 的意思
# 对模型进行测试
def test(self):
self.ds_obj = Dataset()
ds = self.ds_obj.read_dataset()
new_ds = []
for row in ds:
new_ds.append(row[1:])
self.x_test = np.array(new_ds)
self.results = self.classifier.predict(self.x_test)
def post_test(self):
client_ip_ids = []
total_test, _ = self.x_test.shape
for i in range(total_test):
if self.results[i] == 1:
if self.x_test[i, 1] not in client_ip_ids:
client_ip_ids.append(self.x_test[i, 1])
dos_ips = self.ds_obj.detransform_client_ip(np.array(client_ip_ids, dtype="int64"))
for ip in dos_ips:
print(ip)
def run(self):
self.train()
self.test()
self.post_test()
if __name__ == '__main__':
app = App()
app.run()