首页 > python教程

TensorFlow 读取CSV数据的实例

时间:2020-10-18 python教程 查看: 1082

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.7615.67-0.1215.67
-0.4812.52-0.0612.51
1.339.110.129.1
-0.8820.35-0.1820.36
-0.253.99-0.013.99
-0.8726.25-0.2326.25
-1.032.87-0.032.87
-0.517.81-0.047.81
-1.5714.46-0.2314.46
-0.110.02-0.0110.02
-0.568.92-0.058.92
-1.24.1-0.054.1
-0.775.15-0.045.15
-0.884.48-0.044.48
-2.710.82-0.310.82
-1.232.4-0.032.4
-0.775.16-0.045.15
-0.816.15-0.056.15
-0.65.01-0.035
-1.254.75-0.064.75
-2.537.31-0.197.3
-1.1516.39-0.1916.39
-1.75.19-0.095.18
-0.623.23-0.023.22
-0.7417.43-0.1317.41
-0.7715.41-0.1215.41
047047.01
0.253.980.013.98
-1.19.01-0.19.01
-1.023.87-0.043.87

方法二:

详细读取 Iris_train.csv, Iris_test.csv 代码

#coding:utf-8

import tensorflow as tf
import 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 result
with 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 数据:

IdSepalLengthCmSepalWidthCmPetalLengthCmPetalWidthCmSpecies
215.43.41.70.2Iris-setosa
225.13.71.50.4Iris-setosa
234.63.610.2Iris-setosa
245.13.31.70.5Iris-setosa
254.83.41.90.2Iris-setosa
26531.60.2Iris-setosa
2753.41.60.4Iris-setosa
285.23.51.50.2Iris-setosa
295.23.41.40.2Iris-setosa
304.73.21.60.2Iris-setosa
314.83.11.60.2Iris-setosa
325.43.41.50.4Iris-setosa
335.24.11.50.1Iris-setosa
345.54.21.40.2Iris-setosa
354.93.11.50.1Iris-setosa
3653.21.20.2Iris-setosa
375.53.51.30.2Iris-setosa
384.93.11.50.1Iris-setosa
394.431.30.2Iris-setosa
405.13.41.50.2Iris-setosa
4153.51.30.3Iris-setosa
424.52.31.30.3Iris-setosa
434.43.21.30.2Iris-setosa
4453.51.60.6Iris-setosa
455.13.81.90.4Iris-setosa
464.831.40.3Iris-setosa
475.13.81.60.2Iris-setosa
484.63.21.40.2Iris-setosa
495.33.71.50.2Iris-setosa
5053.31.40.2Iris-setosa
715.93.24.81.8Iris-versicolor
726.12.841.3Iris-versicolor
736.32.54.91.5Iris-versicolor
746.12.84.71.2Iris-versicolor
756.42.94.31.3Iris-versicolor
766.634.41.4Iris-versicolor
776.82.84.81.4Iris-versicolor
786.7351.7Iris-versicolor
7962.94.51.5Iris-versicolor
805.72.63.51Iris-versicolor
815.52.43.81.1Iris-versicolor
825.52.43.71Iris-versicolor
835.82.73.91.2Iris-versicolor
8462.75.11.6Iris-versicolor
855.434.51.5Iris-versicolor
8663.44.51.6Iris-versicolor
876.73.14.71.5Iris-versicolor
886.32.34.41.3Iris-versicolor
895.634.11.3Iris-versicolor
905.52.541.3Iris-versicolor
915.52.64.41.2Iris-versicolor
926.134.61.4Iris-versicolor
935.82.641.2Iris-versicolor
9452.33.31Iris-versicolor
955.62.74.21.3Iris-versicolor
965.734.21.2Iris-versicolor
975.72.94.21.3Iris-versicolor
986.22.94.31.3Iris-versicolor
995.12.531.1Iris-versicolor
1005.72.84.11.3Iris-versicolor
1216.93.25.72.3Iris-virginica
1225.62.84.92Iris-virginica
1237.72.86.72Iris-virginica
1246.32.74.91.8Iris-virginica
1256.73.35.72.1Iris-virginica
1267.23.261.8Iris-virginica
1276.22.84.81.8Iris-virginica
1286.134.91.8Iris-virginica
1296.42.85.62.1Iris-virginica
1307.235.81.6Iris-virginica
1317.42.86.11.9Iris-virginica
1327.93.86.42Iris-virginica
1336.42.85.62.2Iris-virginica
1346.32.85.11.5Iris-virginica
1356.12.65.61.4Iris-virginica
1367.736.12.3Iris-virginica
1376.33.45.62.4Iris-virginica
1386.43.15.51.8Iris-virginica
139634.81.8Iris-virginica
1406.93.15.42.1Iris-virginica
1416.73.15.62.4Iris-virginica
1426.93.15.12.3Iris-virginica
1435.82.75.11.9Iris-virginica
1446.83.25.92.3Iris-virginica
1456.73.35.72.5Iris-virginica
1466.735.22.3Iris-virginica
1476.32.551.9Iris-virginica
1486.535.22Iris-virginica
1496.23.45.42.3Iris-virginica
1505.935.11.8Iris-virginica

Iris_test.csv 数据:

IdSepalLengthCmSepalWidthCmPetalLengthCmPetalWidthCmSpecies
15.13.51.40.2tf_read
24.931.40.2Iris-setosa
34.73.21.30.2Iris-setosa
44.63.11.50.2Iris-setosa
553.61.40.2Iris-setosa
65.43.91.70.4Iris-setosa
74.63.41.40.3Iris-setosa
853.41.50.2Iris-setosa
94.42.91.40.2Iris-setosa
104.93.11.50.1Iris-setosa
115.43.71.50.2Iris-setosa
124.83.41.60.2Iris-setosa
134.831.40.1Iris-setosa
144.331.10.1Iris-setosa
155.841.20.2Iris-setosa
165.74.41.50.4Iris-setosa
175.43.91.30.4Iris-setosa
185.13.51.40.3Iris-setosa
195.73.81.70.3Iris-setosa
205.13.81.50.3Iris-setosa
5173.24.71.4Iris-versicolor
526.43.24.51.5Iris-versicolor
536.93.14.91.5Iris-versicolor
545.52.341.3Iris-versicolor
556.52.84.61.5Iris-versicolor
565.72.84.51.3Iris-versicolor
576.33.34.71.6Iris-versicolor
584.92.43.31Iris-versicolor
596.62.94.61.3Iris-versicolor
605.22.73.91.4Iris-versicolor
61523.51Iris-versicolor
625.934.21.5Iris-versicolor
6362.241Iris-versicolor
646.12.94.71.4Iris-versicolor
655.62.93.61.3Iris-versicolor
666.73.14.41.4Iris-versicolor
675.634.51.5Iris-versicolor
685.82.74.11Iris-versicolor
696.22.24.51.5Iris-versicolor
705.62.53.91.1Iris-versicolor
1016.33.362.5Iris-virginica
1025.82.75.11.9Iris-virginica
1037.135.92.1Iris-virginica
1046.32.95.61.8Iris-virginica
1056.535.82.2Iris-virginica
1067.636.62.1Iris-virginica
1074.92.54.51.7Iris-virginica
1087.32.96.31.8Iris-virginica
1096.72.55.81.8Iris-virginica
1107.23.66.12.5Iris-virginica
1116.53.25.12Iris-virginica
1126.42.75.31.9Iris-virginica
1136.835.52.1Iris-virginica
1145.72.552Iris-virginica
1155.82.85.12.4Iris-virginica
1166.43.25.32.3Iris-virginica
1176.535.51.8Iris-virginica
1187.73.86.72.2Iris-virginica
1197.72.66.92.3Iris-virginica
12062.251.5Iris-virginica

以上这篇TensorFlow 读取CSV数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持python博客。

展开全文
上一篇:tensorflow对图像进行拼接的例子
下一篇:浅谈tensorflow之内存暴涨问题
输入字:
相关知识
Python 实现图片色彩转换案例

我们在看动漫、影视作品中,当人物在回忆过程中,体现出来的画面一般都是黑白或者褐色的。本文将提供将图片色彩转为黑白或者褐色风格的案例详解,感兴趣的小伙伴可以了解一下。

python初学定义函数

这篇文章主要为大家介绍了python的定义函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助,希望能够给你带来帮助

图文详解Python如何导入自己编写的py文件

有时候自己写了一个py文件,想要把它导入到另一个py文件里面,所以下面这篇文章主要给大家介绍了关于Python如何导入自己编写的py文件的相关资料,需要的朋友可以参考下

python二分法查找实例代码

二分算法是一种效率比较高的查找算法,其输入的是一个有序的元素列表,如果查找元素包含在列表中,二分查找返回其位置,否则返回NONE,下面这篇文章主要给大家介绍了关于python二分法查找的相关资料,需要的朋友可以参考下