'How to convert pandas dataframe to tensorflow dataset?
I am reading a csv file into a pandas dataframe.
train_data = pd.read_csv('mnist_test.csv');
Sample data
label pixel1 pixel2 pixel3 ... pixel781 pixel782 pixel783 pixel784
0 6 149 149 150 ... 106 112 120 107
1 5 126 128 131 ... 184 184 182 180
2 10 85 88 92 ... 226 225 224 222
3 0 203 205 207 ... 230 240 253 255
4 3 188 191 193 ... 49 46 46 53
how can I convert this dataframe into a tensorflow dataset.
Solution 1:[1]
import tensorflow as tf
ds = tf.data.Dataset.from_tensor_slices(dict(train_data))
See tensorflow.org/tutorials/load_data/pandas_dataframe for details.
Solution 2:[2]
For the sake of completeness,
import tensorflow as tf
ds = tf.data.Dataset.from_tensor_slices(train_data.to_dict(orient="list"))
print(ds)
TensorSliceDataset element_spec={'label': TensorSpec(shape=(), dtype=tf.int32, name=None), ...}
Solution 3:[3]
from datasets import load_dataset
dataset = load_dataset('csv', data_files='my_file.csv')
dataset = load_dataset('csv', data_files=['my_file_1.csv', 'my_file_2.csv', 'my_file_3.csv'])
dataset = load_dataset('csv', data_files={'train': ['my_train_file_1.csv', 'my_train_file_2.csv'],'test': 'my_test_file.csv'})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | proedig |
| Solution 2 | Eduardo Cuesta |
| Solution 3 | Susaj S N |
