TensorFlow 读取CSV数据代码实现

本文介绍了如何在TensorFlow中使用代码读取CSV数据,包括两种方法:一是读取tf_read.csv文件,二是处理Iris_train.csv和Iris_test.csv数据集。
摘要由CSDN通过智能技术生成

TensorFlow 读取CSV数据原理在此就不做详细介绍,直接通过代码实现:

方法一:

详细读取tf_read.csv 代码

#coding:utf-8

import tensorflow as tf

filename_queue = tf.train.string_input_producer(["/home/yongcai/tf_read.csv"])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)

record_defaults = [[1.], [1.], [1.], [1.]]
col1, col2, col3, col4 = tf.decode_csv(value, record_defaults=record_defaults)

features = tf.stack([col1, col2, col3])

init_op = tf.global_variables_initializer()
local_init_op = tf.local_variables_initializer()

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)

    try:
        for i in range(30):
            example, label = sess.run([features, col4])
            print(example)
            # print(label)
    except tf.errors.OutOfRangeError:
        print 'Done !!!'

    finally:
        coord.request_stop()
        coord.join(threads)

tf_read.csv 数据:

-0.76	15.67	-0.12	15.67
-0.48	12.52	-0.06	12.51
1.33	9.11	0.12	9.1
-0.88	20.35	-0.18	20.36
-0.25	3.99	-0.01	3.99
-0.87	26.25	-0.23	26.25
-1.03	2.87	-0.03	2.87
-0.51	7.81	-0.04	7.81
-1.57	14.46	-0.23	14.46
-0.1	10.02	-0.01	10.02
-0.56	8.92	-0.05	8.92
-1.2	4.1	-0.05	4.1
-0.77	5.15	-0.04	5.15
-0.88	4.48	-0.04	4.48
-2.7	10.82	-0.3	10.82
-1.23	2.4	-0.03	2.4
-0.77	5.16	-0.04	5.15
-0.81	6.15	-0.05	6.15
-0.6	5.01	-0.03	5
-1.25	4.75	-0.06	4.75
-2.53	7.31	-0.19	7.3
-1.15	16.39	-0.19	16.39
-1.7	5.19	-0.09	5.18
-0.62	3.23	-0.02	3.22
-0.74	17.43	-
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值