Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GaneshGS/187147ea539f15d17535b9038364b329 to your computer and use it in GitHub Desktop.
Save GaneshGS/187147ea539f15d17535b9038364b329 to your computer and use it in GitHub Desktop.
Convert pandas DataFrame into TensorFlow Dataset
import numpy as np
import pandas as pd
import tensorflow as tf
tf.enable_eager_execution()
training_df: pd.DataFrame = pd.DataFrame(
data={
'feature1': np.random.rand(10),
'feature2': np.random.rand(10),
'feature3': np.random.rand(10),
'target': np.random.randint(0, 3, 10)
}
)
features = ['feature1', 'feature2', 'feature3']
print(training_df)
training_dataset = (
tf.data.Dataset.from_tensor_slices(
(
tf.cast(training_df[features].values, tf.float32),
tf.cast(training_df['target'].values, tf.int32)
)
)
)
for features_tensor, target_tensor in training_dataset:
print(f'features:{features_tensor} target:{target_tensor}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment