Regression on Boston Houses prices


In [2]:
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score, KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline


Using TensorFlow backend.

In [7]:
from sklearn.datasets import load_boston
boston = load_boston()
X = boston.data
y = boston.target

In [16]:
def base_model():
    model = Sequential()
    model.add(Dense(X.shape[1], input_dim=X.shape[1], kernel_initializer='normal', activation='relu'))
    model.add(Dense(1, kernel_initializer='normal'))
    model.compile(loss='mse', optimizer='adam')
    return model

In [20]:
np.random.seed(7)
model = KerasRegressor(build_fn=base_model, epochs=100, batch_size=5, verbose=0)
pipeline = Pipeline([
    ("scaler", StandardScaler()),
    ('reg', model)
])
kfold = KFold(n_splits=10, random_state=7)

results = cross_val_score(pipeline, X, y, cv=kfold)
print(results.mean(), results.std())


22.4661029618 24.4918239996

A smaller layer


In [1]:
def smaller_model():
    model = Sequential()
    model.add(Dense(int(X.shape[1]/2), input_dim=X.shape[1], kernel_initializer='normal', activation='relu' ))
    model.add(Dense(1, kernel_initializer='normal'))
    model.compile(loss='mse', optimizer='adam')
    return model

In [9]:
np.random.seed(7)
model = KerasRegressor(build_fn=smaller_model, epochs=100, batch_size=5, verbose=0)
pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('reg', model)
])
kfold = KFold(n_splits=10, random_state=7)
results = cross_val_score(pipeline, X, y, cv=kfold)
print(results.mean(), results.std())


26.6485708675 25.9873010933

A larger model


In [10]:
def larger_model():
    model = Sequential()
    model.add(Dense(X.shape[1] * 2, input_dim=X.shape[1], kernel_initializer='normal', activation='relu' ))
    model.add(Dense(1, kernel_initializer='normal'))
    model.compile(loss='mse', optimizer='adam')
    return model

In [11]:
model = KerasRegressor(build_fn=larger_model, epochs=100, batch_size=5, verbose=0)
pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('reg', model)
])
kfold = KFold(n_splits=10, random_state=7)
results = cross_val_score(pipeline, X, y, cv=kfold)
print(results.mean(), results.std())


21.5674971462 22.9977673183

A deeper model


In [17]:
def wider_model():
    model = Sequential()
    model.add(Dense(X.shape[1], input_dim=X.shape[1], kernel_initializer='normal', activation='relu' ))
    model.add(Dense(int(X.shape[1]/2), kernel_initializer='normal', activation='relu' ))
    model.add(Dense(1, kernel_initializer='normal'))
    model.compile(loss='mse', optimizer='adam')
    return model

In [18]:
model = KerasRegressor(build_fn=wider_model, epochs=100, batch_size=5, verbose=0)
pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('reg', model)
])
kfold = KFold(n_splits=10, random_state=7)
results = cross_val_score(pipeline, X, y, cv=kfold)
print(results.mean(), results.std())


21.2356700153 22.0222610472