Classify structured data using Keras Preprocessing Layers

View on View source on GitHub Download notebook

This tutorial demonstrates how to classify structured data (e.g. tabular data in a CSV). You will use Keras to define the model, and preprocessing layers 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
  • Map from columns in the CSV to features used to train the model using Keras Preprocessing layers.
  • Build, train, and evaluate a model using Keras.

The Dataset

You 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. You will use this information to predict if 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 you 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 sklearn
import numpy as np
import pandas as pd
import tensorflow as tf

from sklearn.model_selection import train_test_split
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing

Use Pandas to create a dataframe

Pandas is a Python library with many helpful utilities for loading and working with structured data. You will use Pandas to download the dataset from a URL, and load it into a dataframe.

import pathlib

dataset_url = ''
csv_file = 'datasets/petfinder-mini/petfinder-mini.csv'

tf.keras.utils.get_file('', dataset_url,
                        extract=True, cache_dir='.')
dataframe = pd.read_csv(csv_file)
Downloading data from
1671168/1668792 [==============================] - 0s 0us/step

Create target variable

The task in the Kaggle competition 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, you 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 you downloaded was a single CSV file. You 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

Next, you will wrap the dataframes with, in order to shuffle and batch the data. If you were working with a very large CSV file (so large that it does not fit into memory), you would use to read it from disk directly. That is not covered in this tutorial.

# A utility method to create a dataset from a Pandas Dataframe
def df_to_dataset(dataframe, shuffle=True, batch_size=32):
  dataframe = dataframe.copy()
  labels = dataframe.pop('target')
  ds =, labels))
  if shuffle:
    ds = ds.shuffle(buffer_size=len(dataframe))
  ds = ds.batch(batch_size)
  ds = ds.prefetch(batch_size)
  return ds

Now that you have created the input pipeline, let's call it to see the format of the data it returns. You have used a small batch size to keep the output readable.

batch_size = 5
train_ds = df_to_dataset(train, batch_size=batch_size)
[(train_features, label_batch)] = train_ds.take(1)
print('Every feature:', list(train_features.keys()))
print('A batch of ages:', train_features['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([7 3 1 4 2], shape=(5,), dtype=int64)
A batch of targets: tf.Tensor([0 0 1 0 1], shape=(5,), dtype=int64)

You 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 the use of preprocessing layers.

The Keras preprocessing layers API allows you to build Keras-native input processing pipelines. You will use 3 preprocessing layers to demonstrate the feature preprocessing code.

You can find a list of available preprocessing layers here.

Numeric columns

For each of the Numeric feature, you will use a Normalization() layer to make sure the mean of each feature is 0 and its standard deviation is 1.

get_normalization_layer function returns a layer which applies featurewise normalization to numerical features.

def get_normalization_layer(name, dataset):
  # Create a Normalization layer for our feature.
  normalizer = preprocessing.Normalization(axis=None)

  # Prepare a Dataset that only yields our feature.
  feature_ds = x, y: x[name])

  # Learn the statistics of the data.

  return normalizer
photo_count_col = train_features['PhotoAmt']
layer = get_normalization_layer('PhotoAmt', train_ds)
<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
       [ 1.0716735],
       [-0.8334968]], dtype=float32)>

Categorical columns

In this dataset, Type is represented as a string (e.g. 'Dog', or 'Cat'). You cannot feed strings directly to a model. The preprocessing layer takes care of representing strings as a one-hot vector.

get_category_encoding_layer function returns a layer which maps values from a vocabulary to integer indices and one-hot encodes the features.

def get_category_encoding_layer(name, dataset, dtype, max_tokens=None):
  # Create a StringLookup layer which will turn strings into integer indices
  if dtype == 'string':
    index = preprocessing.StringLookup(max_tokens=max_tokens)
    index = preprocessing.IntegerLookup(max_tokens=max_tokens)

  # Prepare a Dataset that only yields our feature
  feature_ds = x, y: x[name])

  # Learn the set of possible values and assign them a fixed integer index.

  # Create a Discretization for our integer indices.
  encoder = preprocessing.CategoryEncoding(num_tokens=index.vocabulary_size())

  # Apply one-hot encoding to our indices. The lambda function captures the
  # layer so we can use them, or include them in the functional model later.
  return lambda feature: encoder(index(feature))
type_col = train_features['Type']
layer = get_category_encoding_layer('Type', train_ds, 'string')
<tf.Tensor: shape=(5, 4), dtype=float32, numpy=
array([[0., 0., 1., 0.],
       [0., 0., 0., 1.],
       [0., 0., 0., 1.],
       [0., 0., 1., 0.],
       [0., 0., 1., 0.]], dtype=float32)>

Often, you don't want to feed a number directly into the model, but instead use a one-hot encoding of those inputs. Consider raw data that represents a pet's age.

type_col = train_features['Age']
category_encoding_layer = get_category_encoding_layer('Age', train_ds,
                                                      'int64', 5)
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[0., 1., 0., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.]], dtype=float32)>

Choose which columns to use

You have seen how to use several types of preprocessing layers. Now you will use them to train a model. You will be using Keras-functional API to build the model. The Keras functional API is a way to create models that are more flexible than the tf.keras.Sequential API.

The goal of this tutorial is to show you the complete code (e.g. mechanics) needed to work with preprocessing layers. A few columns have been selected arbitrarily to train our model.

Earlier, you used a small batch size to demonstrate the input pipeline. Let's now create a new input pipeline with a larger batch size.

batch_size = 256
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)
all_inputs = []
encoded_features = []

# Numeric features.
for header in ['PhotoAmt', 'Fee']:
  numeric_col = tf.keras.Input(shape=(1,), name=header)
  normalization_layer = get_normalization_layer(header, train_ds)
  encoded_numeric_col = normalization_layer(numeric_col)
# Categorical features encoded as integers.
age_col = tf.keras.Input(shape=(1,), name='Age', dtype='int64')
encoding_layer = get_category_encoding_layer('Age', train_ds, dtype='int64',
encoded_age_col = encoding_layer(age_col)
# Categorical features encoded as string.
categorical_cols = ['Type', 'Color1', 'Color2', 'Gender', 'MaturitySize',
                    'FurLength', 'Vaccinated', 'Sterilized', 'Health', 'Breed1']
for header in categorical_cols:
  categorical_col = tf.keras.Input(shape=(1,), name=header, dtype='string')
  encoding_layer = get_category_encoding_layer(header, train_ds, dtype='string',
  encoded_categorical_col = encoding_layer(categorical_col)

Create, compile, and train the model

Now you can create our end-to-end model.

all_features = tf.keras.layers.concatenate(encoded_features)
x = tf.keras.layers.Dense(32, activation="relu")(all_features)
x = tf.keras.layers.Dropout(0.5)(x)
output = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(all_inputs, output)

Let's visualize our connectivity graph:

# rankdir='LR' is used to make the graph horizontal.
tf.keras.utils.plot_model(model, show_shapes=True, rankdir="LR")


Train the model, epochs=10, validation_data=val_ds)
Epoch 1/10
29/29 [==============================] - 2s 19ms/step - loss: 0.6824 - accuracy: 0.4887 - val_loss: 0.5852 - val_accuracy: 0.7134
Epoch 2/10
29/29 [==============================] - 0s 7ms/step - loss: 0.5998 - accuracy: 0.6500 - val_loss: 0.5615 - val_accuracy: 0.7340
Epoch 3/10
29/29 [==============================] - 0s 7ms/step - loss: 0.5775 - accuracy: 0.6676 - val_loss: 0.5489 - val_accuracy: 0.7243
Epoch 4/10
29/29 [==============================] - 0s 7ms/step - loss: 0.5553 - accuracy: 0.6928 - val_loss: 0.5403 - val_accuracy: 0.7264
Epoch 5/10
29/29 [==============================] - 0s 7ms/step - loss: 0.5553 - accuracy: 0.7045 - val_loss: 0.5338 - val_accuracy: 0.7248
Epoch 6/10
29/29 [==============================] - 0s 7ms/step - loss: 0.5417 - accuracy: 0.7058 - val_loss: 0.5290 - val_accuracy: 0.7281
Epoch 7/10
29/29 [==============================] - 0s 7ms/step - loss: 0.5369 - accuracy: 0.7093 - val_loss: 0.5251 - val_accuracy: 0.7302
Epoch 8/10
29/29 [==============================] - 0s 7ms/step - loss: 0.5364 - accuracy: 0.7135 - val_loss: 0.5222 - val_accuracy: 0.7291
Epoch 9/10
29/29 [==============================] - 0s 7ms/step - loss: 0.5296 - accuracy: 0.7154 - val_loss: 0.5198 - val_accuracy: 0.7362
Epoch 10/10
29/29 [==============================] - 0s 7ms/step - loss: 0.5288 - accuracy: 0.7222 - val_loss: 0.5175 - val_accuracy: 0.7313
<tensorflow.python.keras.callbacks.History at 0x7f51e056a510>
loss, accuracy = model.evaluate(test_ds)
print("Accuracy", accuracy)
10/10 [==============================] - 0s 5ms/step - loss: 0.5209 - accuracy: 0.7331
Accuracy 0.7331022620201111

Inference on new data

You can now save and reload the Keras model. Follow the tutorial here for more information on TensorFlow models.'my_pet_classifier')
reloaded_model = tf.keras.models.load_model('my_pet_classifier')
WARNING:absl:Function `_wrapped_model` contains input name(s) PhotoAmt, Fee, Age, Type, Color1, Color2, Gender, MaturitySize, FurLength, Vaccinated, Sterilized, Health, Breed1 with unsupported characters which will be renamed to photoamt, fee, age, type, color1, color2, gender, maturitysize, furlength, vaccinated, sterilized, health, breed1 in the SavedModel.
INFO:tensorflow:Assets written to: my_pet_classifier/assets
INFO:tensorflow:Assets written to: my_pet_classifier/assets

To get a prediction for a new sample, you can simply call model.predict(). There are just two things you need to do:

  1. Wrap scalars into a list so as to have a batch dimension (models only process batches of data, not single samples)
  2. Call convert_to_tensor on each feature
sample = {
    'Type': 'Cat',
    'Age': 3,
    'Breed1': 'Tabby',
    'Gender': 'Male',
    'Color1': 'Black',
    'Color2': 'White',
    'MaturitySize': 'Small',
    'FurLength': 'Short',
    'Vaccinated': 'No',
    'Sterilized': 'No',
    'Health': 'Healthy',
    'Fee': 100,
    'PhotoAmt': 2,

input_dict = {name: tf.convert_to_tensor([value]) for name, value in sample.items()}
predictions = reloaded_model.predict(input_dict)
prob = tf.nn.sigmoid(predictions[0])

    "This particular pet had a %.1f percent probability "
    "of getting adopted." % (100 * prob)
This particular pet had a 86.6 percent probability of getting adopted.

Next steps

The best way to learn more about classifying structured data is to try it yourself. You may want to find another dataset to work with, and training a model to classify it using code similar to the above. To improve accuracy, think carefully about which features to include in your model, and how they should be represented.