逗號分隔值(Comma-Separated Values,CSV,有時也稱爲字符分隔值,由於分隔字符也能夠不是逗號),其文件以純文本形式存儲表格數據(數字和文本)。純文本意味着該文件是一個字符序列,不含必須像二進制數字那樣被解讀的數據。CSV文件由任意數目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最多見的是逗號或製表符。一般,全部記錄都有徹底相同的字段序列.python
import csv import os import numpy as np import random import requests # name of data file # 數據集名稱 birth_weight_file = 'birth_weight.csv' # download data and create data file if file does not exist in current directory # 若是當前文件夾下沒有birth_weight.csv數據集則下載dat文件並生成csv文件 if not os.path.exists(birth_weight_file): birthdata_url = 'https://github.com/nfmcclure/tensorflow_cookbook/raw/master/01_Introduction/07_Working_with_Data_Sources/birthweight_data/birthweight.dat' birth_file = requests.get(birthdata_url) birth_data = birth_file.text.split('\r\n') # split分割函數,以一行做爲分割函數,windows中換行符號爲'\r\n',每一行後面都有一個'\r\n'符號。 birth_header = birth_data[0].split('\t') # 每一列的標題,標在第一行,便是birth_data的第一個數據。並使用製表符做爲劃分。 birth_data = [[float(x) for x in y.split('\t') if len(x) >= 1] for y in birth_data[1:] if len(y) >= 1] print(np.array(birth_data).shape) # (189, 9) # 此爲list數據形式不是numpy數組不能使用np,shape函數,可是咱們可使用np.array函數將list對象轉化爲numpy數組後使用shape屬性進行查看。 with open(birth_weight_file, "w", newline='') as f: # with open(birth_weight_file, "w") as f: writer = csv.writer(f) writer.writerows([birth_header]) writer.writerows(birth_data) f.close()
birth_data = [] with open(birth_weight_file) as csvfile: csv_reader = csv.reader(csvfile) # 使用csv.reader讀取csvfile中的文件 birth_header = next(csv_reader) # 讀取第一行每一列的標題 for row in csv_reader: # 將csv 文件中的數據保存到birth_data中 birth_data.append(row) birth_data = [[float(x) for x in row] for row in birth_data] # 將數據從string形式轉換爲float形式 birth_data = np.array(birth_data) # 將list數組轉化成array數組便於查看數據結構 birth_header = np.array(birth_header) print(birth_data.shape) # 利用.shape查看結構。 print(birth_header.shape) # # (189, 9) # (9,)
import pandas as pd csv_data = pd.read_csv('birth_weight.csv') # 讀取訓練數據 print(csv_data.shape) # (189, 9) N = 5 csv_batch_data = csv_data.tail(N) # 取後5條數據 print(csv_batch_data.shape) # (5, 9) train_batch_data = csv_batch_data[list(range(3, 6))] # 取這20條數據的3到5列值(索引從0開始) print(train_batch_data) # RACE SMOKE PTL # 184 0.0 0.0 0.0 # 185 0.0 0.0 1.0 # 186 0.0 1.0 0.0 # 187 0.0 0.0 0.0 # 188 0.0 0.0 1.0
Tensorflow簡單CNN實現github
利用TFRecords存儲與讀取帶標籤的圖片windows
'''使用Tensorflow讀取csv數據''' filename = 'birth_weight.csv' file_queue = tf.train.string_input_producer([filename]) # 設置文件名隊列,這樣作可以批量讀取文件夾中的文件 reader = tf.TextLineReader(skip_header_lines=1) # 使用tensorflow文本行閱讀器,而且設置忽略第一行 key, value = reader.read(file_queue) defaults = [[0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.]] # 設置列屬性的數據格式 LOW, AGE, LWT, RACE, SMOKE, PTL, HT, UI, BWT = tf.decode_csv(value, defaults) # 將讀取的數據編碼爲咱們設置的默認格式 vertor_example = tf.stack([AGE, LWT, RACE, SMOKE, PTL, HT, UI]) # 讀取獲得的中間7列屬性爲訓練特徵 vertor_label = tf.stack([BWT]) # 讀取獲得的BWT值表示訓練標籤 # 用於給取出的數據添加上batch_size維度,以批處理的方式讀出數據。能夠設置批處理數據大小,是否重複讀取數據,容量大小,隊列末尾大小,讀取線程等屬性。 example_batch, label_batch = tf.train.shuffle_batch([vertor_example, vertor_label], batch_size=10, capacity=100, min_after_dequeue=10) # 初始化Session with tf.Session() as sess: coord = tf.train.Coordinator() # 線程管理器 threads = tf.train.start_queue_runners(coord=coord) print(sess.run(tf.shape(example_batch))) # [10 7] print(sess.run(tf.shape(label_batch))) # [10 1] print(sess.run(example_batch)[3]) # [ 19. 91. 0. 1. 1. 0. 1.] coord.request_stop() coord.join(threads) ''' 對於使用全部Tensorflow的I/O操做來講開啓和關閉線程管理器都是必要的操做 with tf.Session() as sess: coord = tf.train.Coordinator() # 線程管理器 threads = tf.train.start_queue_runners(coord=coord) # Your code here~ coord.request_stop() coord.join(threads) '''
還有其餘使用python讀取文件的各類方法,這裏介紹三種,不按期進行補充。數組