月落星稀天欲明,孤灯未灭梦难成。
失眠怎么办?当然是码代码喽!本次实现的是k邻近算法,先来个基础版本的,数据集还是Mnist,具体算法的数学描述,直接搬运《统计学习方法》的相关章节(主要是欧氏距离和投票表决)如下:
import numpy as np
def output_label(data_train,input,k):
data_input=np.tile(input,(data_train.shape[0],1))
data_dis=data_input-data_train
data_dis=data_dis**2
data_dis=data_dis.sum(axis=1)
data_dis=data_dis**0.5
data_dis_sort=data_dis.argsort()#返回由小到大的数据的位置索引
data_result_label=[]
for i in range(k):
data_result_label.append(train_label[data_dis_sort[i]])
result=np.argmax(np.bincount(data_result_label))
return result
train_file=open("D:\\bpnetwork\\mnist_train.csv",'r')
test_file=open("D:\\bpnetwork\\mnist_test.csv",'r')
train_label=[]
test_label=[]
train=[]
test=[]
for line in train_file:
value=line.split(',')
train_label.append(int(value[0]))
tempt_list=value[1:]
tempt_list=list(map(int,tempt_list))
train.append(tempt_list)
data_train=np.array(train)
cnt_correct=0
for line in test_file:
value=line.split(',')
test_label.append(int(value[0]))
tempt_list=value[1:]
tempt_list=list(map(int,tempt_list))
test.append(tempt_list)
data_test=np.array(test)
for i in range(data_test.shape[0]):
input=data_test[i]
if(output_label(data_train,input,4)==test_label[i]):
cnt_correct+=1
r=cnt_correct/data_test.shape[0]
print(r)
train_file.close()
test_file.close()
预测正确率如下图: