KNN,即K nearest neighbor,K近邻算法。KNN的思想非常简单,所需的数学知识较少。...KNN的计算中,先计算距离,比较常见的是欧拉距离:
?
也就是两个点(或者多个点)对应的横纵坐标差的平方和,然后开平方。...根据欧拉距离写一个KNN的实现:
def KNN_test(X_train, y_train, test, K):
distance = []
for t in X_train:...(data[:, :2], target, X, 6)
sklearn中的实现:
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier...(n_neighbors=6)
knn.fit(data[:, :2], target)
knn.predict(X.reshape(1, -1))