学习进度-k近邻算法

import numpy as np
import os
import tensorflow as tf
# 防止意外报错
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 导入mnist数据集
from tensorflow.examples.tutorials.mnist import input_data
# onehot标签标识一个长度为n的数组,只有一个元素是1,其他的都是0,用来表示mnist中标签数据
mnist = input_data.read_data_sets("mnist_data/", one_hot=True)
# 对mnist数据集做一个数量限制
Xtrain,Ytrain=mnist.train.next_batch(5000)#使用5000个训练数据
Xtest,Ytest=mnist.train.next_batch(200) # 使用200个测试数据
print('Xtrain.shape: ', Xtrain.shape, ', Xtest.shape: ',Xtest.shape)
print('Ytrain.shape: ', Ytrain.shape, ', Ytest.shape: ',Ytest.shape)
# 计算图输入占位符
#train 使用全部样本,test 逐个样本进行测试
xtrain=tf.placeholder("float",[None,784])#图片训练集
xtest=tf.placeholder("float",[784])#测试集
#使用L1距离进行最近邻计算
#计算L1距离
distance=tf.reduce_sum(tf.abs(tf.add(xtrain,tf.negative(xtest))),axis=1)
# 预测: 获得最小距离的索引 (根据最近邻的类标签进行判断)
pred = tf.arg_min(distance, 0)
#评估:判断给定的一条测试样本是否预测正确
#评估正确率
accuracy=0
# 初始化节点
init = tf.global_variables_initializer()
#启动会话
with tf.Session() as sess:
sess.run(init)
Ntest=len(Xtest)#测试样本的数量
for i in range(Ntest):
# 获取当前测试样本的最近邻
nn_index = sess.run(pred, feed_dict={xtrain: Xtrain, xtest: Xtest[i, :]})#一个样本一个样本的输入
# 获得最近邻预测标签,然后与真实的类标签比较,由于是 one_hot 编码,所以要用 argmax 将类标取出
pred_class_label = np.argmax(Ytrain[nn_index])
true_class_label = np.argmax(Ytest[i])
print("Test", i, "Predicted Class Label:", pred_class_label,
"True Class Label:", true_class_label)
# 计算准确率
if pred_class_label == true_class_label:
accuracy += 1

print("Done!")
accuracy /= Ntest
print("Accuracy:", accuracy)

原文地址:https://www.cnblogs.com/kongfanbing/p/14310599.html