Load CSV data in Tensorflow Last Updated : 23 Sep, 2022 Comments Improve Suggest changes Like Article Like Report This article will look at the ways to load CSV data in the Python programming language using TensorFlow. TensorFlow library provides the make_csv_dataset( ) function, which is used to read the data and use it in our programs. Loading single CSV File To get the single CSV data file from the URL, we use the Keras get_file function. Here we will use the Titanic Dataset. To use this, we add the following lines in our code: Python3 import tensorflow as tf from tensorflow.keras import layers import pandas as pd data_path = tf.keras.utils.get_file("data_train.csv", "https://storage.googleapis.com/tf-datasets/titanic/train.csv") data_train_tf = tf.data.experimental.make_csv_dataset( data_path, batch_size=10, label_name='survived', num_epochs=1, ignore_errors=True,) The data now can be used as a dict where the key is the column name and values are the data records. The first item in the dataset is our data columns; the other is label data. In our data batch, each column/feature name acts as a key, and all values in the column are its value. Python3 for batch, label in data_train_tf.take(1): for key, value in batch.items(): print(f"{key:10s}: {value}") Output: Loading Multiple CSVs Files: The primary use of make_csv_dataset method can be seen when we have to import multiple CSV files into our dataset. We will use the fonts dataset, which contains different language fonts. Example: In this example, we use the Keras get_file feature to read multiple datasets onto the disk, and cache_dir and cache_subdir define where to store these. Once we have the datasets saved, then using the file_pattern command in our make_csv_dataset we can specify the path to all files to be imported. Create a new file and execute the following code: Python3 fonts = tf.keras.utils.get_file('fonts.zip', "https://archive.ics.uci.edu/ml/machine-learning-databases/00417/fonts.zip", cache_dir='.', cache_subdir='fonts', extract=True) fonts_data = tf.data.experimental.make_csv_dataset( file_pattern="fonts/*.csv", batch_size=10, num_epochs=1, num_parallel_reads=4, shuffle_buffer_size=10000) for features in fonts_data.take(1): for i, (name, value) in enumerate(features.items()): if i > 15: break print(f"{name:20s}: {value}") print(f"[total: {len(features)} features]") We are displaying the first 15 features of each dataset and their values. The final count of total feature is displayed using len( ) function. In this example, we have 412 features in total. Output: Comment More infoAdvertise with us Next Article Load CSV data in Tensorflow S sunnydrall Follow Improve Article Tags : Python Deep Learning AI-ML-DS Tensorflow Practice Tags : python Similar Reads Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.It can 5 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Linear Regression in Machine learning Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea 15+ min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Support Vector Machine (SVM) Algorithm Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or 9 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Like