TensorFlow读取CSV数据(批量)

直接上代码:

# -*- 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
时间: 2024-08-03 19:51:13

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

TensorFlow读取CSV数据

代码来源于官方文档,做了一些小小的调整: # -*- coding:utf-8 -*- import tensorflow as tf filename_queue = tf.train.string_input_producer(["file01.csv", "file02.csv"]) reader = tf.TextLineReader() key, value = reader.read(filename_queue) # Default values, i

(4)cocos2dx读取csv数据文件

cocos2dx中读取数据文件可能有很多种,像读取xml,lua,csv,json等,这些都可以作为配置数据的格式. 最近用到了读取csv数据文件,所以在网上找了一下关于这方面的技术博客.果然,网上各路大神都是不吝啬的, 不说废话了,直接上代码.代码如下(测试通过,可读取数据): .h头文件 // // QLCSVFile.h // // Created by quasi_lee on 15-7-7. // // #ifndef __QLCSVFile__ #define __QLCSVFile

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

读取CSV数据并写入txt文件 package com.vfsd; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.csvreader.CsvReader; /****************************************************************************

tensorflow 对csv数据进行批量获取

代码如下: #读取文件数据 def read_data(file_queue):    # 读取的时候需要跳过第一行    reader = tf.TextLineReader(skip_header_lines=1)    key, value = reader.read(file_queue)    # 对于数据源中空的值设置默认值    record_defaults = [[''], [''], [''], [''], [0.], [0.], [0.], [0.], [''],[0],

PHP读取CSV数据写入数据库

/*读取csv文件*/ public function testCsv(){ $fileName = "tel.csv"; $fp=fopen($fileName,"r"); $data = fgetcsv($fp); $count = 1; $result = array(); while(!feof($fp) && $data = fgetcsv($fp)) { if($count>1 && !empty($data)) {

python读取csv数据(添加列名,指定分隔方式)

现有CSV/EXCEL文件一个,为简化期间,为一个3x3的数据文件,内容如下:1,2,32,1,33,2,1用pandas.read读取以后,第一行自动被识别为columns,造成数据出错   1 2 30 2 1 31 3 2 1有没有什么命令可以添加自定义的columns的名字,比如我想命名为 A, B, C三列,该怎么操作呢? pd.read_csv(file, header=None, names = ['a','b','c'] ) 原文地址:https://www.cnblogs.co

matlab读取csv文件数据并绘图

circle.m(画二维圆的函数) %该函数是画二维圆圈,输入圆心坐标和半径%rectangle()函数参数‘linewidth’修饰曲线的宽度%'edgecolor','r',edgecolor表示边框颜色,r表示颜色参数%'facecolor','b',facecolor表示内部填充颜色,b表示颜色参数function [] = circle( x,y,r )rectangle('Position',[x-r,y-r,2*r,2*r],'Curvature',[1,1],'linewidth

Python 处理 CSV 数据

CSV 是一种用逗号来分隔的一种数据格式,全称为 " 逗号分隔值文件格式 " ,CSV 文件在 Excel 中打开会显示成表格 CSV 文件:    CSV 数据:    在 Excel 中打开会显示为: Python 读取 CSV 数据: #!/usr/bin/env python #-*- coding:utf-8 -*- import codecs with codecs.open("1.csv", encoding="utf-8") as

python读取csv文件并添加索引

对于csv文件进行处理一个重要的步骤是为数据添加索引,方便后续的数据操作,这里我们使用pandas库中的read_csv()函数,在读取csv数据的同时可以对其添加行索引和列索引. import pandas as pd obj=pd.read_csv('testdata.csv') print(obj) read_csv()不对属性进行设置的缺省状态下,对于csv文件进行读取操作后,即使原来的数据存在索引,也会自动添加数字的行索引和列索引. obj=pd.read_csv('testdata.