-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn_test.py
81 lines (66 loc) · 2.64 KB
/
knn_test.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('http_final_revise_test.csv', encoding="euc-kr")
df_z = np.array(data.dip)
df_xy = data[["sport","dport"]]
data_xy = data[["sport","dport"]].values
s_port = int(input('sport : '))
d_port = int(input('dport : '))
target = [s_port, d_port]
def data_set():
size = len(df_xy)
class_target = np.tile(target, (size,1))
class_z = np.array(df_z)
return df_xy, class_target, class_z
dataset, class_target, class_z = data_set()
def classify(dataset, class_target, class_category, k):
diffMat = class_target - dataset
sqDiffMat = diffMat**2
row_sum = sqDiffMat.sum(axis=1)
distance = np.sqrt(row_sum)
sortDist = distance.argsort()
class_result = {}
for i in range(k):
c = class_category[sortDist[i]]
class_result[c] = class_result.get(c, 0) + 1
return class_result
k = int(input('k값을 입력해주세요 :'))
class_result = classify(data_xy, class_target, class_z, k) # classify()함수호출
print(class_result)
def resultprint(class_result):
a_1 = a_2 = a_3 = a_4 = a_5 = 0
for c in class_result.keys():
if c == '172.30.1.53':
a_1 = class_result[c]
elif c == '210.102.100.235':
a_2 = class_result[c]
elif c == '218.54.130.120':
a_3 = class_result[c]
elif c == '218.54.130.30':
a_4 = class_result[c]
else :
a_5 = class_result[c]
if a_1 > a_2 and a_1 > a_3 and a_1 > a_4 and a_1 > a_5:
result = "a_1"
elif a_2 > a_1 and a_2 > a_3 and a_2 > a_4 and a_2 > a_5:
result = "a_2"
elif a_3 > a_2 and a_3 > a_1 and a_3 > a_4 and a_3 > a_5:
result = "a_3"
elif a_4 > a_2 and a_4 > a_3 and a_4 > a_1 and a_4 > a_5:
result = "a_4"
elif a_5 > a_2 and a_5 > a_3 and a_5 > a_4 and a_5 > a_1:
result = "a_5"
else:
result = "k값을 변경해주세요."
return result
for c in range(len(df_xy)):
data_xy[c][1] = data_xy[c][1]/10
if df_z[c] == 'a_1': plt.scatter(data_xy[c][0],data_xy[c][1],marker='o',color='b')
elif df_z[c] == 'a_2': plt.scatter(data_xy[c][0],data_xy[c][1],marker='o',color='g')
elif df_z[c] == 'a_3': plt.scatter(data_xy[c][0],data_xy[c][1],marker='o',color='y')
elif df_z[c] == 'a_4': plt.scatter(data_xy[c][0],data_xy[c][1],marker='o',color='c')
elif df_z[c] == 'a_5': plt.scatter(data_xy[c][0],data_xy[c][1],marker='o',color='m')
else: plt.scatter(data_xy[c][0],data_xy[c][1],marker='x',color='k')
plt.scatter(s_port,d_port,marker='*',color='r')
plt.show()