python-tensorflowHow do I read a CSV file using Python and TensorFlow?
Reading a CSV file using Python and TensorFlow is quite simple. The following example code block demonstrates how to read a CSV file and load it into a TensorFlow dataset:
import tensorflow as tf
# Create a TensorFlow dataset object from the CSV file
dataset = tf.data.experimental.make_csv_dataset(
'my_file.csv',
batch_size=32,
label_name='target'
)
# Print the first batch of data
for features, label in dataset.take(1):
print("Features:", features)
print("Label:", label)
Output example
Features: {'feature1': <tf.Tensor: shape=(32,), dtype=float32, numpy=
array([1.2, 0.4, 3.5, ..., 0.9, 0.2, 1.7], dtype=float32)>, 'feature2': <tf.Tensor: shape=(32,), dtype=float32, numpy=
array([0.4, 0.9, 0.2, ..., 0.2, 0.5, 0.6], dtype=float32)>}
Label: <tf.Tensor: shape=(32,), dtype=int32, numpy=array([1, 0, 0, ..., 0, 1, 0], dtype=int32)>
The code consists of the following parts:
- Importing the TensorFlow library.
- Creating a TensorFlow dataset object from the CSV file.
- Looping through the dataset to print the first batch of data.
For more information on how to read a CSV file using Python and TensorFlow, please refer to the TensorFlow documentation.
More of Python Tensorflow
- How can I use Python and TensorFlow to handle illegal hardware instructions in Zsh?
- How do I resolve a SymbolAlreadyExposedError when the symbol "zeros" is already exposed as () in TensorFlow Python util tf_export?
- How can I use YOLOv3 with Python and TensorFlow?
- ¿Cómo implementar reconocimiento facial con TensorFlow y Python?
- How do I use Python TensorFlow 1.x?
- How can I determine the best Python version to use for TensorFlow?
- How can I use Python and TensorFlow to implement YOLO object detection?
- How can I use Python TensorFlow in W3Schools?
- How do I install Tensorflow with a Python wheel (whl) file?
- How can I use Python and TensorFlow to run computations on the CPU?
See more codes...