具体knn算法概念参考knn代码python实现
上面是参考《机器学习实战》的代码,和knn的思想
# _*_ encoding=utf8 _*_
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 导入手写体识别的数据
mnist = input_data.read_data_sets("../data", one_hot=True)
# 训练集和测试集
X_train, Y_train = mnist.train.next_batch(5000) # 数据和labels
X_test, Y_test = mnist.test.next_batch(100)
# 定义输入
x_train = tf.placeholder(tf.float32, shape=(None,784))
x_test = tf.placeholder(tf.float32, shape=(784))
# L1距离也就是城市街区距离 |x1-x2|+|y1-y2|
distance = tf.reduce_sum(tf.abs(tf.add(x_train,tf.negative(x_test))),reduction_indices = 1)
# 返回最近的坐标,0纵轴 1横轴
pred = tf.arg_min(distance, 0)
accuracy = 0
# 初始化
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(len(X_test)):
# 获取当前样本的最近邻索引,当前样本和每一个训练的样本找一个最近的l1距离,得到这个最小距离的下标
nn_index = sess.run(pred, feed_dict={x_train:X_train, x_test: X_test[i, :]})
# 由最邻近索引找到label,然后最邻近的label与真实标签比较 np.argmax找最大的下标
# 由l1距离找到的最小值对应的坐标,通过该最坐标找到对应行label的最大值的下标,这个下标对应的就是数字的大小
print("预测次数", i, "预测标签:", np.argmax(Y_train[nn_index]),"真实标签:", np.argmax(Y_test[i]))
# 计算准确率
if np.argmax(Y_train[nn_index]) == np.argmax(Y_test[i]):
accuracy += 1
print("Accuracy:", float(accuracy)/len(X_test))