封装代码实现
import numpy as np
from math import sqrt
from collections import Counter
def kNN_classfy(k,X_train,y_train,x):
assert 1 <= k <=X_train.shape[0],"k must be valid"
assert X_train.shape[0]==y_train.shape[0],"the size of X_train must equal to the sise of y_train"
assert X_train.shape[1]==x.shape[0],"the feature number of x must be equal to X_train"
#距离求解
distance = [sqrt(np.sum((x_train-x)**2)) for x_train in X_train]
#最近距离的索引排序
nearest=np.argsort(distance)
#求前k个最近的点的y值
top_K=[y_train[i] for i in nearest[:k]]
#投票
votes=Counter(top_K)
#返回投票结果
return votes.most_common(1)[0][0]
运行结果:
结果相同(%run 文件名运行脚本文件)
模仿sklearn中的KNN模型改进封装代码
import numpy as np
from math import sqrt
from collections import Counter
def kNN_classfy(k,X_train,y_train,x):
assert 1 <= k <=X_train.shape[0],"k must be valid"
assert X_train.shape[0]==y_train.shape[0],"the size of X_train must equal to the sise of y_train"
assert X_train.shape[1]==x.shape[0],"the feature number of x must be equal to X_train"
#距离求解
distance = [sqrt(np.sum((x_train-x)**2)) for x_train in X_train]
#最近距离的索引排序
nearest=np.argsort(distance)
#求前k个最近的点的y值
top_K=[y_train[i] for i in nearest[:k]]
#投票
votes=Counter(top_K)
#返回投票结果
return votes.most_common(1)[0][0]
运行结果:
注:KNN模型测试集要求输入的矩阵,所以需要将x进行reshape变换为矩阵