This document introduces machine learning concepts and provides a step-by-step guide to creating a machine learning model with TensorFlow. It begins with an overview of machine learning and formulating hypotheses. Then it shows how to load data, create a simple linear regression model manually, and train it with gradient descent. Next, it demonstrates how to simplify the process using TensorFlow Keras to build and train neural network models. It concludes by discussing feature engineering techniques like bucketizing features to improve model performance.
34. # we need a new way of getting data into the model
def df_to_dataset(df, columns, shuffle=True, batch_size=64):
df = df.copy()
labels = df.pop('medv')
features_df = df[columns]
ds = tf.data.Dataset.from_tensor_slices( (dict(features_df), labels) )
if shuffle:
ds = ds.shuffle(buffer_size=len(df))
ds = ds.batch(batch_size)
return ds
35. from sklearn.model_selection import train_test_split
train, val = train_test_split(df, test_size=0.1)
train_ds = df_to_dataset(train, columns)
val_ds = df_to_dataset(val, columns)
36. feature_columns = []
# numeric columns
for _col in columns:
feature_columns.append(tf.feature_column.numeric_column(_col))
# bucketize number of rooms
rm_buckets =
tf.feature_column.bucketized_column(tf.feature_column.numeric_column('rm
'), boundaries=[1, 2, 3, 4, 5, 6, 7, 8, 9])