KNN(k-Nearest-Neighbors)算法阶段总结

代码如图所示

KNN的过程

代码1:

from math import sqrt//导入平方根函数

distance-=【】

for x_train in X_train

        d  =sqrt(np.sum(( x_train  - x)**2))

            distance append(d)

            distances .show()

            nearest =  np.argsort(distances)

            k=6

topK_y =[y_train[i] for i in nearest[:k]]

topK_y

from collections  import counter//对于数据属性出现频率进行记录

votes = Counter({0:1,1:5})

votes.most_common(2)//找出票数最多的2个元素

predict_y =  votes.most_common(1)[0][0]

你可能感兴趣的:(KNN(k-Nearest-Neighbors)算法阶段总结)