text
stringlengths
0
4.99k
train_ds = dataframe_to_dataset(train_dataframe)
val_ds = dataframe_to_dataset(val_dataframe)
Each Dataset yields a tuple (input, target) where input is a dictionary of features and target is the value 0 or 1:
for x, y in train_ds.take(1):
print(\"Input:\", x)
print(\"Target:\", y)
Input: {'age': <tf.Tensor: shape=(), dtype=int64, numpy=62>, 'sex': <tf.Tensor: shape=(), dtype=int64, numpy=0>, 'cp': <tf.Tensor: shape=(), dtype=int64, numpy=4>, 'trestbps': <tf.Tensor: shape=(), dtype=int64, numpy=160>, 'chol': <tf.Tensor: shape=(), dtype=int64, numpy=164>, 'fbs': <tf.Tensor: shape=(), dtype=int64, numpy=0>, 'restecg': <tf.Tensor: shape=(), dtype=int64, numpy=2>, 'thalach': <tf.Tensor: shape=(), dtype=int64, numpy=145>, 'exang': <tf.Tensor: shape=(), dtype=int64, numpy=0>, 'oldpeak': <tf.Tensor: shape=(), dtype=float64, numpy=6.2>, 'slope': <tf.Tensor: shape=(), dtype=int64, numpy=3>, 'ca': <tf.Tensor: shape=(), dtype=int64, numpy=3>, 'thal': <tf.Tensor: shape=(), dtype=string, numpy=b'reversible'>}
Target: tf.Tensor(1, shape=(), dtype=int64)
Let's batch the datasets:
train_ds = train_ds.batch(32)
val_ds = val_ds.batch(32)
Feature preprocessing with Keras layers
The following features are categorical features encoded as integers:
sex
cp
fbs
restecg
exang
ca
We will encode these features using one-hot encoding. We have two options here:
Use CategoryEncoding(), which requires knowing the range of input values and will error on input outside the range.
Use IntegerLookup() which will build a lookup table for inputs and reserve an output index for unkown input values.
For this example, we want a simple solution that will handle out of range inputs at inference, so we will use IntegerLookup().
We also have a categorical feature encoded as a string: thal. We will create an index of all possible features and encode output using the StringLookup() layer.
Finally, the following feature are continuous numerical features:
age
trestbps
chol
thalach
oldpeak
slope
For each of these features, we will use a Normalization() layer to make sure the mean of each feature is 0 and its standard deviation is 1.
Below, we define 3 utility functions to do the operations:
encode_numerical_feature to apply featurewise normalization to numerical features.
encode_string_categorical_feature to first turn string inputs into integer indices, then one-hot encode these integer indices.
encode_integer_categorical_feature to one-hot encode integer categorical features.
from tensorflow.keras.layers import IntegerLookup
from tensorflow.keras.layers import Normalization
from tensorflow.keras.layers import StringLookup
def encode_numerical_feature(feature, name, dataset):
# Create a Normalization layer for our feature
normalizer = Normalization()
# Prepare a Dataset that only yields our feature
feature_ds = dataset.map(lambda x, y: x[name])
feature_ds = feature_ds.map(lambda x: tf.expand_dims(x, -1))
# Learn the statistics of the data
normalizer.adapt(feature_ds)
# Normalize the input feature
encoded_feature = normalizer(feature)
return encoded_feature
def encode_categorical_feature(feature, name, dataset, is_string):
lookup_class = StringLookup if is_string else IntegerLookup
# Create a lookup layer which will turn strings into integer indices
lookup = lookup_class(output_mode=\"binary\")
# Prepare a Dataset that only yields our feature
feature_ds = dataset.map(lambda x, y: x[name])
feature_ds = feature_ds.map(lambda x: tf.expand_dims(x, -1))
# Learn the set of possible string values and assign them a fixed integer index
lookup.adapt(feature_ds)
# Turn the string input into integer indices
encoded_feature = lookup(feature)
return encoded_feature
Build a model
With this done, we can create our end-to-end model:
# Categorical features encoded as integers
sex = keras.Input(shape=(1,), name=\"sex\", dtype=\"int64\")
cp = keras.Input(shape=(1,), name=\"cp\", dtype=\"int64\")
fbs = keras.Input(shape=(1,), name=\"fbs\", dtype=\"int64\")
restecg = keras.Input(shape=(1,), name=\"restecg\", dtype=\"int64\")
exang = keras.Input(shape=(1,), name=\"exang\", dtype=\"int64\")
ca = keras.Input(shape=(1,), name=\"ca\", dtype=\"int64\")
# Categorical feature encoded as string
thal = keras.Input(shape=(1,), name=\"thal\", dtype=\"string\")
# Numerical features
age = keras.Input(shape=(1,), name=\"age\")
trestbps = keras.Input(shape=(1,), name=\"trestbps\")
chol = keras.Input(shape=(1,), name=\"chol\")