数据集:MINST
数据预处理参考了https://blog.csdn.net/simple_the_best/article/details/75267863
处理出来有用的信息也就是 28 × 28 28 \times 28 28×28的矩阵和Label信息。
KNN的实现:
def KNN(train_dataset, train_labels, input_vec, distance, k=1):
dis_labels = []
n = len(train_dataset)
for i in range(n):
vec = train_dataset[i]
label = train_labels[i]
dis_labels.append([distance(vec, input_vec), label])
dis_labels.sort