Tensorflow读取csv文件(转)
常用的直接读取方法实例:
#加载包
import tensorflow as tf
import os
#设置工作目录
os.chdir("你自己的目录")
#查看目录
print(os.getcwd())
#读取函数定义
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)
#处理
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)
print(x_train_batch,y_train_batch)
结果:
Tensor(“shuffle_batch_2:0”, shape=(50, 4), dtype=float32) Tensor(“shuffle_batch_2:1”, shape=(50,), dtype=int32)
从它的数据维度可知,数据已经读入。
一个完整的例子见github:https://github.com/zhangdm/machine-learning-summary/tree/master/tensorflow/tensorflow_iris_nn
Tensorflow读取csv文件(转)的更多相关文章
- sparkR读取csv文件
sparkR读取csv文件 The general method for creating SparkDataFrames from data sources is read.df. This met ...
- VB6.0 读取CSV文件
最近做了一个Upload文件的需求,文件的格式为CSV,读取文件的方法整理了一下,如下: 1.先写了一个读取CSV文件的Function: '读取CSV文件 '假设传入的参数strFile=C:\Do ...
- php读取csv文件,在linux上出现中文读取不到的情况 解决方法
今,php读取csv文件,在linux上出现中文读取不到的情况,google,后找到解决办法<?phpsetlocale(LC_ALL, 'zh_CN');$row = 1;$handle = ...
- 使用univocity-parsers创建和读取csv文件
import com.univocity.parsers.csv.CsvFormat;import com.univocity.parsers.csv.CsvParser;import com.uni ...
- Python 读取csv文件到excel
朋友问我如何通过python把csv格式的文件另存为xls文件,自己想了想通过读取csv文件然后再保存到xls文件中即可,也许还有其他简单的方法,但这里也为了练习python语法及其他知识,所以采用了 ...
- 转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法
1. 转换成CSV文件: http://www.dotnetgallery.com/lab/resource93-Export-to-CSV-file-from-Data-Table-in-Aspne ...
- java读取CSV文件添加到sqlserver数据库
在直接将CSV文件导入sqlserver数据库时出现了错误,原因还未找到,初步怀疑是数据中含有特殊字符.于是只能用代码导数据了. java读取CSV文件的代码如下: package experimen ...
- C#:StreamReader读取.CSV文件(转换成DataTable)
using System.Data; using System.IO; /// <summary> /// Stream读取.csv文件 /// </summary> /// ...
- R语言如何读取.csv文件
以下是我关于如何在R语言中读取.csv文件及一些需要注意的细节的总结,希望能帮助到大家~
随机推荐
- linux常用命令---持续添加中...
1.cp -r 源文件夹 目的文件夹 // -r 可递归所有子目录及文件 2.grep -r 查找内容 ./* //递归查找当前目录下所有文件指定内容 3. 查看系统运行时间 who - ...
- YTU 2715: 函数---判断某年某月某日是这一年中的第几天
2715: 函数---判断某年某月某日是这一年中的第几天 时间限制: 1 Sec 内存限制: 128 MB 提交: 380 解决: 155 题目描述 在主程序(main)中输入某年某月某日,例如2 ...
- POJ3090 Visible Lattice Points 欧拉函数
欧拉函数裸题,直接欧拉函数值乘二加一就行了.具体证明略,反正很简单. 题干: Description A lattice point (x, y) in the first quadrant (x a ...
- MySQL社区版是世界上最流行的开源数据库的免费
昨天晚上搞了很久,终于搞清楚mysql的安装配置了,我真是太low了.当我在云服务器上登进Mysql时,真是高兴哈哈,咱一步一步来,彻底搞懂Mysql的安装配置. 我的安装环境: 阿里云服务器 1 2 ...
- Maven远程中央仓库地址
阿里云 - http://maven.aliyun.com/nexus/content/groups/public/ Apache Snapshots - https://repository.apa ...
- java线程系列---Runnable和Thread的区别 (转载)
转自:http://blog.csdn.net/wwww1988600/article/details/7309070 在java中可有两种方式实现多线程,一种是继承 Thread类,一种是实现Run ...
- 【废弃】【WIP】JavaScript 函数
创建: 2017/10/09 更新: 2017/11/03 加上[wip] 废弃: 2019/02/19 重构此篇.原文归入废弃 增加[废弃中]标签与总体任务 结束: 2019/03/12 完成废弃 ...
- centos安装 memcached
.安装 yum -y update yum install -y libevent libevent-devel yum install -y memcached 2.配置 vi /etc/sysco ...
- int(3)和int(11)区别
- 51nod 1577 线性基
思路: http://blog.csdn.net/yxuanwkeith/article/details/53524757 //By SiriusRen #include <bits/stdc+ ...