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]
封装代码实现import numpy as npfrom math import sqrtfrom collections import Counterdef kNN_classfy(k,X_train,y_train,x): assert 1 &lt;= k &lt;=X_train.shape[0],"k must be valid" assert X_train.s...