Loading single CSV File
To get the single CSV data file from the URL, we use the Keras get_file function. Here we will use the Titanic Dataset.
To use this, we add the following lines in our code:
Python3
import tensorflow as tf from tensorflow.keras import layers import pandas as pd data_path = tf.keras.utils.get_file( "data_train.csv" , "https://storage.googleapis.com/tf-datasets/titanic/train.csv" ) data_train_tf = tf.data.experimental.make_csv_dataset( data_path, batch_size = 10 , label_name = 'survived' , num_epochs = 1 , ignore_errors = True ,) |
The data now can be used as a dict where the key is the column name and values are the data records. The first item in the dataset is our data columns; the other is label data. In our data batch, each column/feature name acts as a key, and all values in the column are its value.
Python3
for batch, label in data_train_tf.take( 1 ): for key, value in batch.items(): print (f "{key:10s}: {value}" ) |
Output:
Load CSV data in Tensorflow
This article will look at the ways to load CSV data in the Python programming language using TensorFlow.
TensorFlow library provides the make_csv_dataset( ) function, which is used to read the data and use it in our programs.
Contact Us