forked from yweng0828/for-Farm-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kNN.py
39 lines (24 loc) · 818 Bytes
/
kNN.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
#!/usr/bin/env python
# coding: utf-8
# In[3]:
import preprocess
from preprocess import cross_10folds
from preprocess import kNNclassify
# In[4]:
# 使用10折交叉验证
def handw_KNN(totdata_x, totdata_y):
res = 0.0
for j in range(0, 10):
train_x, train_y, test_x, test_y = cross_10folds(totdata_x, totdata_y, j)
#print(train_x.shape)
#print(train_y.shape)
#print(test_x.shape)
#print(test_y.shape)
right = 0
for i in range(test_x.shape[0]):
if(kNNclassify(test_x[i], train_x, train_y, 5)==test_y[i]):
right = right + 1
res += right/test_y.shape[0]
print("第 %d 次的准确率为 %f" %(j, right/test_y.shape[0]))
print("最后的准确率为 %f" %(res/10))
# In[ ]: