zoukankan      html  css  js  c++  java
  • 学习进度-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)

  • 相关阅读:
    单相、二相、三相区别
    Live for Speed 车模、赛道模型导出
    Lenovo/IBM Thinkpad X41 Tablet
    科普题外话 赛车性能的关键指标: 马力和扭力
    MAME™ Official Developer Documentation!
    Visual Studio 2005 & SQL Server 2005 are COMING!
    星际争霸(Star Craft)的Sprites导出
    推荐一本关于操作系统实践的好书
    科普题外话:Experimental Advanced Superconducting Tokamak - 人造太阳
    The Space Elevator -通往地球同步轨道的天梯
  • 原文地址:https://www.cnblogs.com/kongfanbing/p/14310599.html
Copyright © 2011-2022 走看看