How to load CSV files in a TensorFlow program?

Member

by adelia , in category: General Help , 3 months ago

How to load CSV files in a TensorFlow program?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ethelyn , 3 months ago

@adelia 

To load CSV files in a TensorFlow program, you can follow these steps:


Step 1: Import Required Libraries

1
2
import tensorflow as tf
import pandas as pd


Step 2: Read CSV File

1
df = pd.read_csv('your_file.csv')


Step 3: Extract Features and Labels

1
2
features = df.iloc[:, :-1] # Extract all columns except the last one
labels = df.iloc[:, -1] # Extract only the last column


Step 4: Convert Data to TensorFlow Tensors

1
2
features_tensor = tf.convert_to_tensor(features.values)
labels_tensor = tf.convert_to_tensor(labels.values)


Step 5: Create a TensorFlow Dataset

1
dataset = tf.data.Dataset.from_tensor_slices((features_tensor, labels_tensor))


Now you can use the created dataset for further processing, such as model training, validation, or testing.