How to read data from csv file in tensorflow?

by hal.littel , in category: Third Party Scripts , 11 days ago

How to read data from csv file in tensorflow?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , 10 days ago

@hal.littel 

To read data from a CSV file in TensorFlow, you can use the tf.data.experimental.make_csv_dataset() function. Here is an example code snippet that demonstrates how to read data from a CSV file using TensorFlow:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import tensorflow as tf

# Define the file path to the CSV file
file_path = 'data.csv'

# Define the column names and data types
columns = ['feature1', 'feature2', 'label']
column_defaults = [tf.float32, tf.float32, tf.int32]

# Create a dataset from the CSV file
dataset = tf.data.experimental.make_csv_dataset(
    file_path,
    batch_size=32,
    column_names=columns,
    column_defaults=column_defaults,
    label_name='label',
    num_epochs=1
)

# Iterate over the dataset and print the data
for batch in dataset:
    print('Features:', batch['feature1'], batch['feature2'])
    print('Labels:', batch['label'])


In this code snippet:

  • file_path is the path to the CSV file.
  • columns is a list of column names in the CSV file.
  • column_defaults is a list of data types for each column.
  • tf.data.experimental.make_csv_dataset() is used to create a dataset from the CSV file.
  • batch_size defines the number of examples in each batch.
  • column_names and column_defaults are used to specify the column names and data types for each column.
  • label_name specifies the column that contains the labels.
  • num_epochs specifies the number of times to iterate over the dataset.


You can then iterate over the dataset to access the features and labels.