Classify structured data with feature columns | TensorFlow Core
Skip to main content English Español – América Latina Français Indonesia Italiano Polski Português – Brasil Tiếng Việt Türkçe Русский עברית العربيّة فارسی हिंदी বাংলা ภาษาไทย 中文 – 简体 日本語 한국어 GitHub Sign in TensorFlow Core TensorFlow Learn TensorFlow Core Classify structured data with feature columns Stay organized with collections Save and categorize content based on your preferences.
View on TensorFlow.org Run in Google Colab View source on GitHub Download notebook This tutorial demonstrates how to classify structured data (e.g. tabular data in a CSV). We will use Keras to define the model, and tf.feature_column as a bridge to map from columns in a CSV to features used to train the model. This tutorial contains complete code to: Load a CSV file using Pandas . Build an input pipeline to batch and shuffle the rows using tf.data .
Map from columns in the CSV to features used to train the model using feature columns. Build, train, and evaluate a model using Keras. The Dataset We will use a simplified version of the PetFinder dataset . There are several thousand rows in the CSV. Each row describes a pet, and each column describes an attribute. We will use this information to predict the speed at which the pet will be adopted. Following is a description of this dataset.
Notice there are both numeric and categorical columns. There is a free text column which we will not use in this tutorial. Column Description Feature Type Data Type Type Type of animal (Dog, Cat) Categorical string Age Age of the pet Numerical integer Breed1 Primary breed of the pet Categorical string Color1 Color 1 of pet Categorical string Color2 Color 2 of pet Categorical string MaturitySize Size at maturity Categorical string FurLength Fur length Categorical string Vaccinated Pet has been vaccinated Categorical string Sterilized Pet has been sterilized Categorical string Health Health Condition Categorical string Fee Adoption Fee Numerical integer Description Profile write-up for this pet Text string PhotoAmt Total uploaded photos for this pet Numerical integer AdoptionSpeed Speed of adoption Classification integer Import TensorFlow and other libraries pip install -q scikit-learn tf-keras import os os . environ [ 'TF_USE_LEGACY_KERAS' ] = '1' import numpy as np import pandas as pd import tensorflow as tf from tensorflow import feature_column from tensorflow.keras import layers from sklearn.model_selection import train_test_split Use Pandas to create a dataframe Pandas is a Python library with many helpful utilities for loading and working with structured data.
We will use Pandas to download the dataset from a URL, and load it into a dataframe. import os dataset_url = 'http://storage.googleapis.com/download.tensorflow.org/data/petfinder-mini.zip' csv_file = 'datasets/petfinder-mini/petfinder-mini.csv' extracted_dir = tf . keras . utils . get_file ( 'petfinder_mini.zip' , dataset_url , extract = True , cache_dir = '.' ) if not os . path . exists ( csv_file ): csv_file = os . path . join ( extracted_dir , 'petfinder-mini' , 'petfinder-mini.csv' ) dataframe = pd . read_csv ( csv_file ) Downloading data from http://storage.googleapis.com/download.tensorflow.org/data/petfinder-mini.zip 1668792/1668792 [==============================] - 0s 0us/step dataframe . head () Create target variable The task in the original dataset is to predict the speed at which a pet will be adopted (e.g., in the first week, the first month, the first three months, and so on).
Let's simplify this for our tutorial. Here, we will transform this into a binary classification problem, and simply predict whether the pet was adopted, or not. After modifying the label column, 0 will indicate the pet was not adopted, and 1 will indicate it was. # In the original dataset "4" indicates the pet was not adopted. dataframe [ 'target' ] = np . where ( dataframe [ 'AdoptionSpeed' ] == 4 , 0 , 1 ) # Drop un-used columns. dataframe = dataframe . drop ( columns = [ 'AdoptionSpeed' , 'Description' ]) Split the dataframe into train, validation, and test The dataset we downloaded was a single CSV file.
We will split this into train, validation, and test sets. train , test = train_test_split ( dataframe , test_size = 0.2 ) train , val = train_test_split ( train , test_size = 0.2 ) print ( len ( train ), 'train examples' ) print ( len ( val ), 'validation examples' ) print ( len ( test ), 'test examples' ) 7383 train examples 1846 validation examples 2308 test examples Create an input pipeline using tf.data Next, we will wrap the dataframes with tf.data .
This will enable us to use feature columns as a bridge to map from the columns in the Pandas dataframe to features used to train the model. If we were working with a very large CSV file (so large that it does not fit into memory), we would use tf.data to read it from disk directly. That is not covered in this tutorial. # A utility method to create a tf.data dataset from a Pandas Dataframe def df_to_dataset ( dataframe , shuffle = True , batch_size = 32 ): dataframe = dataframe . copy () labels = dataframe . pop ( 'target' ) ds = tf . data .
Dataset . from_tensor_slices (( dict ( dataframe ), labels )) if shuffle : ds = ds . shuffle ( buffer_size = len ( dataframe )) ds = ds . batch ( batch_size ) return ds batch_size = 5 # A small batch sized is used for demonstration purposes train_ds = df_to_dataset ( train , batch_size = batch_size ) val_ds = df_to_dataset ( val , shuffle = False , batch_size = batch_size ) test_ds = df_to_dataset ( test , shuffle = False , batch_size = batch_size ) WARNING: All log messages before absl::InitializeLog() is called are written to STDERR W0000 00:00:1794976637.458362 601911 dlopen_checker.cc:38] Could not load dynamic library 'libcusolver.so.11'; dlerror: /tmpfs/src/tf_docs_env/lib/python3.11/site-packages/tensorflow/python/platform/../../../nvidia/cusolver/lib/../../cusparse/lib/libcusparse.so.12: undefined symbol: __nvJitLinkGetErrorLogSize_12_9, version libnvJitLink.so.12 W0000 00:00:1794976637.458406 601911 dlopen_checker.cc:38] Could not load dynamic library 'libcusparse.so.12'; dlerror: /tmpfs/src/tf_docs_env/lib/python3.11/site-packages/tensorflow/python/platform/../../../nvidia/cusparse/lib/libcusparse.so.12: undefined symbol: __nvJitLinkGetErrorLogSize_12_9, version libnvJitLink.so.12 W0000 00:00:1794976637.458410 601911 gpu_device.cc:2359] Cannot dlopen some GPU libraries.
Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... Understand the input pipeline Now that we have created the input pipeline, let's call it to see the format of the data it returns. We have used a small batch size to keep the output readable. for feature_batch , label_batch in train_ds . take ( 1 ): print ( 'Every feature:' , list ( feature_batch . keys ())) print ( 'A batch of ages:' , feature_batch [ 'Age' ]) print ( 'A batch of targets:' , label_batch ) Every feature: ['Type', 'Age', 'Breed1', 'Gender', 'Color1', 'Color2', 'MaturitySize', 'FurLength', 'Vaccinated', 'Sterilized', 'Health', 'Fee', 'PhotoAmt'] A batch of ages: tf.Tensor([24 4 2 3 17], shape=(5,), dtype=int64) A batch of targets: tf.Tensor([0 1 0 1 0], shape=(5,), dtype=int64) We can see that the dataset returns a dictionary of column names (from the dataframe) that map to column values from rows in the dataframe.
Demonstrate several types of feature columns TensorFlow provides many types of feature columns. In this section, we will create several types of feature columns, and demonstrate how they transform a column from the dataframe. # We will use this batch to demonstrate several types of feature columns example_batch = next ( iter ( train_ds ))[ 0 ] # A utility method to create a feature c
The rest of this page is on the live site.