-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn_test.py
63 lines (56 loc) · 1.6 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
#%%
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics.pairwise import euclidean_distances
#%%
# df = pd.read_excel(r'data\fruit_data.xlsx')
# with pd.option_context('display.max_rows', 8): display(df)
# x = df[['width', 'height', 'mass', 'color_score']]
# y = df['fruit_name']
#%%
x = pd.DataFrame({
'กบ': [0,1,0,0,0,0,1,1,0],
'ปลาหมอ': [0,1,0,0,1,0,1,0,1],
'จระเข้': [0,1,0,0,1,0,1,0,0],
'ปลาหมอ':[0,0,0,0,1,0,1,0,1],
'เต่า':[0,1,0,0,0,0,1,0,0],
'dog':[1,0,1,1,0,1,0,0,0],
'bird':[1,0,0,1,0,1,0,0,0],
})
x = x.T
y =[0,1,2,1,2,2,3]
y = pd.DataFrame(y,columns=['animal'])
x = pd.concat([x,y])
#%%
# x_train, x_test, y_train, y_test = train_test_split(x, y)
# # #scaling after splitting
# # scaler = StandardScaler()
# # x_train = scaler.fit_transform(x_train)
# # x_test = scaler.transform(x_test)
k = 5
# model = KNeighborsClassifier(n_neighbors=k)
# model.fit(x.T, y)
x_pred = [[0,0,1,0,0,0,0,0,0]]
# x_pred_sc = scaler.transform(x_pred)
y_pred = model.predict(x_pred)
a = x_pred[0][0]
b = x_pred[0][1]
c = x_pred[0][2]
d = x_pred[0][3]
e = x_pred[0][4]
f = x_pred[0][5]
g = x_pred[0][6]
g2 = x_pred[0][7]
g3 = x_pred[0][8]
g4 = x_pred[0][9]
y = y[0][10]
print(y)
# print('K =', k)
# print('Prediction:')
# # print(f'width: {w}, height: {h}, mass: {m}, ', end='')
# # print(f'color score {c} => fruit: {f}')
# # print()
# print('Accuracy:', '{:.2f}'.format(model.score(x_test, y_test)))
# %%