1. 使用python 自实现
import numpy as np
def o_distance(x1,y1):
return np.sqrt(np.sum(np.square(x1-y1)))
class KNN:
def __init__(self,X,y):
self.X = X
self.y = y
def classify(self,x):
distance = [o_distance(x,i) for i in self.X]
print('distance:',distance)
min_dist = np.argmin(distance)
print('near point:',X[min_dist])
return y[min_dist]
if __name__ == "__main__":
X = np.array([(5,4),(9,6),(4,7),(2,3),(8,1),(7,2)])
y = np.array([1,1,1,-1,-1,-1])
point = np.array((5,3))
knn = KNN(X,y)
print('point class: ',knn.classify(point))
2. python 调库实现
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
if __name__ == "__main__":
X = np.array([(5,4),(9,6),(4,7),(2,3),(8,1),(7,2)])
y = np.array([1,1,1,-1,-1,-1])
point = np.array([(5,3)])
for i in range(len(X)):
k = i + 1
knn = KNeighborsClassifier(n_neighbors= k)
knn.fit(X,y)
k_class = knn.predict(point)
print('k = {}, point class: {}'.format(k,k_class))