直接上代码:

# -*- coding:utf-8 -*-
import tensorflow as tf def read_data(file_queue):
reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(file_queue)
defaults = [[0], [0.], [0.], [0.], [0.], ['']]
Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species = tf.decode_csv(value, defaults) #因为使用的是鸢尾花数据集,这里需要对y值做转换
preprocess_op = tf.case({
tf.equal(Species, tf.constant('Iris-setosa')): lambda: tf.constant(0),
tf.equal(Species, tf.constant('Iris-versicolor')): lambda: tf.constant(1),
tf.equal(Species, tf.constant('Iris-virginica')): lambda: tf.constant(2),
}, lambda: tf.constant(-1), exclusive=True) return tf.stack([SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm]), preprocess_op def create_pipeline(filename, batch_size, num_epochs=None):
file_queue = tf.train.string_input_producer([filename], num_epochs=num_epochs)
example, label = read_data(file_queue) min_after_dequeue = 1000
capacity = min_after_dequeue + batch_size
example_batch, label_batch = tf.train.shuffle_batch(
[example, label], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue
) return example_batch, label_batch x_train_batch, y_train_batch = create_pipeline('Iris-train.csv', 50, num_epochs=1000)
x_test, y_test = create_pipeline('Iris-test.csv', 60) init_op = tf.global_variables_initializer()
local_init_op = tf.local_variables_initializer() # local variables like epoch_num, batch_size
with tf.Session() as sess:
sess.run(init_op)
sess.run(local_init_op) # Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord) # Retrieve a single instance:
try:
#while not coord.should_stop():
while True:
example, label = sess.run([x_train_batch, y_train_batch])
print (example)
print (label)
except tf.errors.OutOfRangeError:
print ('Done reading')
finally:
coord.request_stop() coord.join(threads)
sess.close()

数据集是鸢尾花数据集,大家自行下载吧,下面给个示例:

Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species
21,5.4,3.4,1.7,0.2,Iris-setosa
22,5.1,3.7,1.5,0.4,Iris-setosa
23,4.6,3.6,1.0,0.2,Iris-setosa
24,5.1,3.3,1.7,0.5,Iris-setosa
25,4.8,3.4,1.9,0.2,Iris-setosa
26,5.0,3.0,1.6,0.2,Iris-setosa
27,5.0,3.4,1.6,0.4,Iris-setosa
28,5.2,3.5,1.5,0.2,Iris-setosa
29,5.2,3.4,1.4,0.2,Iris-setosa
30,4.7,3.2,1.6,0.2,Iris-setosa
31,4.8,3.1,1.6,0.2,Iris-setosa
32,5.4,3.4,1.5,0.4,Iris-setosa
33,5.2,4.1,1.5,0.1,Iris-setosa
34,5.5,4.2,1.4,0.2,Iris-setosa
35,4.9,3.1,1.5,0.1,Iris-setosa
36,5.0,3.2,1.2,0.2,Iris-setosa
37,5.5,3.5,1.3,0.2,Iris-setosa

TensorFlow读取CSV数据(批量)的更多相关文章

  1. TensorFlow读取CSV数据

    代码来源于官方文档,做了一些小小的调整: # -*- coding:utf-8 -*- import tensorflow as tf filename_queue = tf.train.string ...

  2. Java读取CSV数据并写入txt文件

    读取CSV数据并写入txt文件 package com.vfsd; import java.io.BufferedWriter; import java.io.File; import java.io ...

  3. java 读取CSV数据并写入txt文本

    java 读取CSV数据并写入txt文本 package com.vfsd; import java.io.BufferedWriter; import java.io.File; import ja ...

  4. tensorflow读取训练数据方法

    1. 预加载数据 Preloaded data # coding: utf-8 import tensorflow as tf # 设计Graph x1 = tf.constant([2, 3, 4] ...

  5. PHP读取CSV数据写入数据库

    /*读取csv文件*/ public function testCsv(){ $fileName = "tel.csv"; $fp=fopen($fileName,"r& ...

  6. SQL 读取csv 文件批量插入数据

    use test /* create table temp_pre ( vc_product_id varchar(20) default '', en_in_amount numeric(9,2)d ...

  7. Tensorflow读取csv文件(转)

    常用的直接读取方法实例:#加载包 import tensorflow as tf import os #设置工作目录 os.chdir("你自己的目录") #查看目录 print( ...

  8. pandas读取csv数据时设置index

    比如读取数据时想把第一列设为index,那么只需要简单的 pd.read_csv("new_wordvecter.csv",index_col=[0]) 这里index_col可以 ...

  9. python 读取csv 数据并画图分析

    数据源 : https://pan.baidu.com/s/1eR593Uy    密码: yqjh python环境 python3 #encoding: utf-8 import csv impo ...

随机推荐

  1. Python3中StringIO

    关于调用StringIO模块,python3中需要这样: from io import StringIO 而python2还是 from StringIO import StringIO

  2. 记一次treegrid checkbox 选择问题

    最后选择 select 方法来实现 checkRow checked 属性也不行 getChecked 方法并不能取到这两种方法的行数据

  3. grafana----alert

    Alert只有grafana V4.0以上. Introduction(介绍) Grafana中的alert允许在dashboard panels你附加一些规则.当你保存仪表板Grafana将提取的报 ...

  4. 生日蛋糕 POJ - 1190 搜索 数学

    http://poj.org/problem?id=1190 题解:四个剪枝. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #inc ...

  5. android gradle jnilibs

    https://blog.csdn.net/xx326664162/article/details/51167849 [ABIXCPU] Android jniLibs下目录详解(.so文件) htt ...

  6. [python2] python 打印表格 prettytable

    rpm包: [root@D129 cli]# yum info python-prettytable Loaded plugins: fastestmirror Loading mirror spee ...

  7. 动态代理Dynamic Proxy

    代理模式是常用的Java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类 预处理消息,过滤消息,把消息转发给委托类,以及事后处理消息等. 代理类与委托类之间通常会存在关联关系,一 ...

  8. Python多线程中阻塞(join)与锁(Lock)的使用误区

    参考资料:https://blog.csdn.net/cd_xuyue/article/details/52052893 1使用两个循环分别处理start和join函数.即可实现并发. threads ...

  9. 图->遍历

    文字描述 从图中某一顶点出发遍历图中其余顶点,且使每一个顶点仅被访问一次,这一过程就叫图的遍历. 深度优先搜索:类似树的先根遍历:假设初始状态下,图中所有顶点都未曾被访问,则从某个顶点出发,访问此顶点 ...

  10. LeetCode 589 N-ary Tree Preorder Traversal 解题报告

    题目要求 Given an n-ary tree, return the preorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返回 ...