具体代码如下
import numpy as np
import matplotlib.pyplot as plt
class myKNN():
def __init__(self, k):
self.k = k
def compute(self, X_train, y_train, X_test):
dist = []
i = 0
# 计算欧式距离
for x in X_train:
# dist[i] = np.sqrt(np.sum(np.square(x-X_test)))形式错误,要用append对空列表进行赋值
dist.append(np.sqrt(np.sum(np.square(x-X_test))))
i += 1
# 对欧式距离进行排序, 去除前k个小的值, dist_k表示前k个小的值的下标
dist_k = np.argsort(dist)[0:self.k]
# 先取出k个最小距离的y值大小
y_train_k = []
for y in dist_k:
y_train_k.append(y_train[y])
y_train_k = np.array(y_train_k)
y_train_k = y_train_k + 1
return np.argmax(np.bincount(y_train_k))
def draw(X_train, y_train, X_new):
# 正负实例点初始化
X_po=np.zeros(X_train.shape[1])
X_ne=np.zeros(X_train.shape[1])
# 区分正、负实例点
for i in range(y_train.shape[0]):
if y_train[i]==1:
X_po=np.vstack((X_po,X_train[i]))
else:
X_ne=np.vstack((X_ne,X_train[i]))
# 实例点绘图
plt.plot(X_po[1:,0],X_po[1:,1],"g*", label="1")
plt.plot(X_ne[1:, 0], X_ne[1:, 1], "rx", label="-1")
plt.plot(X_new[:, 0], X_new[:, 1], "bo", label="test_points")
# 测试点坐标值标注
for xy in zip(X_new[:, 0], X_new[:, 1]):
plt.annotate("test{}".format(xy),xy)
# 设置坐标轴
plt.axis([0,10,0,10])
plt.xlabel("x1")
plt.ylabel("x2")
# 显示图例
plt.legend()
# 显示图像
plt.show()
def main():
# 训练数据
X_train = np.array([[5, 4],
[9, 6],
[4, 7],
[2, 3],
[8, 1],
[7, 2]])
y_train = np.array([1, 1, 1, -1, -1, -1])
# 测试数据
X_new = np.array([[5, 3]])
k = 3
KNN = myKNN(k)
if KNN.compute(X_train, y_train, X_new) == 0:
print('当k={}, 测试数据被分类为{}'.format(k, -1))
else:
print('当k={}, 测试数据被分类为{}'.format(k, 1))
draw(X_train, y_train, X_new)
if __name__ == '__main__':
main()
输出如下