Forked from ericness/convert_dataframe_to_dataset.py
Created
December 29, 2019 23:28
-
-
Save GaneshGS/187147ea539f15d17535b9038364b329 to your computer and use it in GitHub Desktop.
Convert pandas DataFrame into TensorFlow Dataset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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