In [1]:
from IPython.display import Image, SVG
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import keras
from keras.datasets import boston_housing
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.utils.vis_utils import model_to_dot
In [2]:
# Boston Housing Dataset
# For details, see:
# https://medium.com/@haydar_ai/learning-data-science-day-9-linear-regression-on-boston-housing-dataset-cd62a80775ef
(x_train, y_train), (x_test, y_test) = boston_housing.load_data()
In [3]:
# 404 training observations with 13 features
x_train.shape
Out[3]:
In [4]:
# 102 test observations with 13 features
x_test.shape
Out[4]:
In [5]:
num_features = np.prod(x_train.shape[1:])
num_features
Out[5]:
In [6]:
# The training and test targets are housing prices in Boston
(y_train[:5], y_test[:5])
Out[6]:
In [7]:
# All the logic to build a linear regression model
model = Sequential()
model.add(Dense(1, input_dim=num_features, activation='linear'))
In [8]:
model.summary()
In [9]:
SVG(model_to_dot(model).create(prog='dot', format='svg'))
Out[9]:
In [10]:
# Trains the model, iterating on the training data in batches of 1 in 10 epochs.
model.compile(optimizer='rmsprop', loss='mse', metrics=["mae"])
model.fit(x_train, y_train, batch_size=1, epochs=10, verbose=1)
Out[10]:
In [11]:
# RMSE is about 8.5 and MAE is about 6.5.
mse, mae = model.evaluate(x_test, y_test, verbose=False)
rmse = np.sqrt(mse)
mse, rmse, mae
Out[11]:
In [12]:
# Predict the first few houses in the test set
model.predict(x_test[:3, :])
Out[12]:
In [13]:
y_test[:3]
Out[13]:
In [ ]: