Skip to content

Instantly share code, notes, and snippets.

@ericness
Last active August 26, 2022 20:25
Show Gist options
  • Save ericness/3bec512bcda167a24df29d73803f3447 to your computer and use it in GitHub Desktop.
Save ericness/3bec512bcda167a24df29d73803f3447 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}')
@aglaiawong
Copy link

Line number 18 does not return a dataset type object but a TensorSliceDataset object. Could you suggest how to convert it to Dataset?

I think it does return a dataset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment