时间:2021-05-22
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.511.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 -0.13 17.41-0.77 15.41 -0.12 15.410 47 0 47.010.25 3.98 0.01 3.98-1.1 9.01 -0.1 9.01-1.02 3.87 -0.04 3.87方法二:
详细读取 Iris_train.csv, Iris_test.csv 代码
#coding:utf-8 import tensorflow as tfimport os os.chdir("/home/yongcai/")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) init_op = tf.global_variables_initializer()local_init_op = tf.local_variables_initializer()# output read data resultwith tf.Session() as sess: sess.run(init_op) sess.run(local_init_op) coord = tf.train.Coordinator() thread = tf.train.start_queue_runners(coord=coord) try: example, label = sess.run([x_test, y_test]) print example print label except tf.errors.OutOfRangeError: print 'Done !!!' finally: coord.request_stop() coord.join(threads=thread)Iris_train.csv 数据:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species21 5.4 3.4 1.7 0.2 Iris-setosa22 5.1 3.7 1.5 0.4 Iris-setosa23 4.6 3.6 1 0.2 Iris-setosa24 5.1 3.3 1.7 0.5 Iris-setosa25 4.8 3.4 1.9 0.2 Iris-setosa26 5 3 1.6 0.2 Iris-setosa27 5 3.4 1.6 0.4 Iris-setosa28 5.2 3.5 1.5 0.2 Iris-setosa29 5.2 3.4 1.4 0.2 Iris-setosa30 4.7 3.2 1.6 0.2 Iris-setosa31 4.8 3.1 1.6 0.2 Iris-setosa32 5.4 3.4 1.5 0.4 Iris-setosa33 5.2 4.1 1.5 0.1 Iris-setosa34 5.5 4.2 1.4 0.2 Iris-setosa35 4.9 3.1 1.5 0.1 Iris-setosa36 5 3.2 1.2 0.2 Iris-setosa37 5.5 3.5 1.3 0.2 Iris-setosa38 4.9 3.1 1.5 0.1 Iris-setosa39 4.4 3 1.3 0.2 Iris-setosa40 5.1 3.4 1.5 0.2 Iris-setosa41 5 3.5 1.3 0.3 Iris-setosa42 4.5 2.3 1.3 0.3 Iris-setosa43 4.4 3.2 1.3 0.2 Iris-setosa44 5 3.5 1.6 0.6 Iris-setosa45 5.1 3.8 1.9 0.4 Iris-setosa46 4.8 3 1.4 0.3 Iris-setosa47 5.1 3.8 1.6 0.2 Iris-setosa48 4.6 3.2 1.4 0.2 Iris-setosa49 5.3 3.7 1.5 0.2 Iris-setosa50 5 3.3 1.4 0.2 Iris-setosa71 5.9 3.2 4.8 1.8 Iris-versicolor72 6.1 2.8 4 1.3 Iris-versicolor73 6.3 2.5 4.9 1.5 Iris-versicolor74 6.1 2.8 4.7 1.2 Iris-versicolor75 6.4 2.9 4.3 1.3 Iris-versicolor76 6.6 3 4.4 1.4 Iris-versicolor77 6.8 2.8 4.8 1.4 Iris-versicolor78 6.7 3 5 1.7 Iris-versicolor79 6 2.9 4.5 1.5 Iris-versicolor80 5.7 2.6 3.5 1 Iris-versicolor81 5.5 2.4 3.8 1.1 Iris-versicolor82 5.5 2.4 3.7 1 Iris-versicolor83 5.8 2.7 3.9 1.2 Iris-versicolor84 6 2.7 5.1 1.6 Iris-versicolor85 5.4 3 4.5 1.5 Iris-versicolor86 6 3.4 4.5 1.6 Iris-versicolor87 6.7 3.1 4.7 1.5 Iris-versicolor88 6.3 2.3 4.4 1.3 Iris-versicolor89 5.6 3 4.1 1.3 Iris-versicolor90 5.5 2.5 4 1.3 Iris-versicolor91 5.5 2.6 4.4 1.2 Iris-versicolor92 6.1 3 4.6 1.4 Iris-versicolor93 5.8 2.6 4 1.2 Iris-versicolor94 5 2.3 3.3 1 Iris-versicolor95 5.6 2.7 4.2 1.3 Iris-versicolor96 5.7 3 4.2 1.2 Iris-versicolor97 5.7 2.9 4.2 1.3 Iris-versicolor98 6.2 2.9 4.3 1.3 Iris-versicolor99 5.1 2.5 3 1.1 Iris-versicolor100 5.7 2.8 4.1 1.3 Iris-versicolor121 6.9 3.2 5.7 2.3 Iris-virginica122 5.6 2.8 4.9 2 Iris-virginica123 7.7 2.8 6.7 2 Iris-virginica124 6.3 2.7 4.9 1.8 Iris-virginica125 6.7 3.3 5.7 2.1 Iris-virginica126 7.2 3.2 6 1.8 Iris-virginica127 6.2 2.8 4.8 1.8 Iris-virginica128 6.1 3 4.9 1.8 Iris-virginica129 6.4 2.8 5.6 2.1 Iris-virginica130 7.2 3 5.8 1.6 Iris-virginica131 7.4 2.8 6.1 1.9 Iris-virginica132 7.9 3.8 6.4 2 Iris-virginica133 6.4 2.8 5.6 2.2 Iris-virginica134 6.3 2.8 5.1 1.5 Iris-virginica135 6.1 2.6 5.6 1.4 Iris-virginica136 7.7 3 6.1 2.3 Iris-virginica137 6.3 3.4 5.6 2.4 Iris-virginica138 6.4 3.1 5.5 1.8 Iris-virginica139 6 3 4.8 1.8 Iris-virginica140 6.9 3.1 5.4 2.1 Iris-virginica141 6.7 3.1 5.6 2.4 Iris-virginica142 6.9 3.1 5.1 2.3 Iris-virginica143 5.8 2.7 5.1 1.9 Iris-virginica144 6.8 3.2 5.9 2.3 Iris-virginica145 6.7 3.3 5.7 2.5 Iris-virginica146 6.7 3 5.2 2.3 Iris-virginica147 6.3 2.5 5 1.9 Iris-virginica148 6.5 3 5.2 2 Iris-virginica149 6.2 3.4 5.4 2.3 Iris-virginica150 5.9 3 5.1 1.8 Iris-virginicaIris_test.csv 数据:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species1 5.1 3.5 1.4 0.2 tf_read2 4.9 3 1.4 0.2 Iris-setosa3 4.7 3.2 1.3 0.2 Iris-setosa4 4.6 3.1 1.5 0.2 Iris-setosa5 5 3.6 1.4 0.2 Iris-setosa6 5.4 3.9 1.7 0.4 Iris-setosa7 4.6 3.4 1.4 0.3 Iris-setosa8 5 3.4 1.5 0.2 Iris-setosa9 4.4 2.9 1.4 0.2 Iris-setosa10 4.9 3.1 1.5 0.1 Iris-setosa11 5.4 3.7 1.5 0.2 Iris-setosa12 4.8 3.4 1.6 0.2 Iris-setosa13 4.8 3 1.4 0.1 Iris-setosa14 4.3 3 1.1 0.1 Iris-setosa15 5.8 4 1.2 0.2 Iris-setosa16 5.7 4.4 1.5 0.4 Iris-setosa17 5.4 3.9 1.3 0.4 Iris-setosa18 5.1 3.5 1.4 0.3 Iris-setosa19 5.7 3.8 1.7 0.3 Iris-setosa20 5.1 3.8 1.5 0.3 Iris-setosa51 7 3.2 4.7 1.4 Iris-versicolor52 6.4 3.2 4.5 1.5 Iris-versicolor53 6.9 3.1 4.9 1.5 Iris-versicolor54 5.5 2.3 4 1.3 Iris-versicolor55 6.5 2.8 4.6 1.5 Iris-versicolor56 5.7 2.8 4.5 1.3 Iris-versicolor57 6.3 3.3 4.7 1.6 Iris-versicolor58 4.9 2.4 3.3 1 Iris-versicolor59 6.6 2.9 4.6 1.3 Iris-versicolor60 5.2 2.7 3.9 1.4 Iris-versicolor61 5 2 3.5 1 Iris-versicolor62 5.9 3 4.2 1.5 Iris-versicolor63 6 2.2 4 1 Iris-versicolor64 6.1 2.9 4.7 1.4 Iris-versicolor65 5.6 2.9 3.6 1.3 Iris-versicolor66 6.7 3.1 4.4 1.4 Iris-versicolor67 5.6 3 4.5 1.5 Iris-versicolor68 5.8 2.7 4.1 1 Iris-versicolor69 6.2 2.2 4.5 1.5 Iris-versicolor70 5.6 2.5 3.9 1.1 Iris-versicolor101 6.3 3.3 6 2.5 Iris-virginica102 5.8 2.7 5.1 1.9 Iris-virginica103 7.1 3 5.9 2.1 Iris-virginica104 6.3 2.9 5.6 1.8 Iris-virginica105 6.5 3 5.8 2.2 Iris-virginica106 7.6 3 6.6 2.1 Iris-virginica107 4.9 2.5 4.5 1.7 Iris-virginica108 7.3 2.9 6.3 1.8 Iris-virginica109 6.7 2.5 5.8 1.8 Iris-virginica110 7.2 3.6 6.1 2.5 Iris-virginica111 6.5 3.2 5.1 2 Iris-virginica112 6.4 2.7 5.3 1.9 Iris-virginica113 6.8 3 5.5 2.1 Iris-virginica114 5.7 2.5 5 2 Iris-virginica115 5.8 2.8 5.1 2.4 Iris-virginica116 6.4 3.2 5.3 2.3 Iris-virginica117 6.5 3 5.5 1.8 Iris-virginica118 7.7 3.8 6.7 2.2 Iris-virginica119 7.7 2.6 6.9 2.3 Iris-virginica120 6 2.2 5 1.5 Iris-virginica以上这篇TensorFlow 读取CSV数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
从csv文件构建Tensorflow的数据集当我们有一系列CSV文件,如何构建Tensorflow的数据集呢?基本步骤获得一组CSV文件的路径将这组文件
1.读取数据用pandas中的read_csv()函数读取出csv文件中的数据:importpandasaspddf=pd.read_csv("comments
数据GDP.csv文件,存储1879~2019年河南省GDP数据绘图#读取数据,首先将excel格式的转化为csv格式再读取h
一次性读取csv文件内所有行的数据复制代码代码如下:读取csv文件的某一行数据复制代码代码如下:读取csv文件制定行数(行区间)复制代码代码如下:另外从网上找的
本文实例讲述了php读取csv数据保存到数组的方法。分享给大家供大家参考。具体分析如下:csv是常用的excel格式的替代品,很多时候我们导出数据是都会导成cs