text_plain_output_1.png
import keras as K

import numpy as np

import pandas as pd

import tensorflow as tf

np.random.seed(4)
tf.set_random_seed(13)

dfBoston = pd.read_csv('../input/boston_mm_tab.csv', header=None)
dfBoston
X = dfBoston[np.arange(0, 13)]
y = dfBoston[13]

tf.logging.set_verbosity(tf.logging.ERROR)
init = K.initializers.RandomUniform(seed=1)
simple_sgd = K.optimizers.SGD(lr=0.01)
model = K.models.Sequential()
model.add(K.layers.Dense(units=10, input_dim=13, kernel_initializer=init, activation='tanh'))
model.add(K.layers.Dense(units=10, kernel_initializer=init, activation='tanh'))
model.add(K.layers.Dense(units=1, kernel_initializer=init, activation=None))
model.compile(loss='mean_squared_error', optimizer=simple_sgd, metrics=['mse'])

batch_size = 8
max_epochs = 500
h = model.fit(X_train, y_train, batch_size=batch_size, epochs=max_epochs, shuffle=True, verbose=1)

y_pred = model.predict(X_train)
y_d = np.array(y_train).reshape(-1, 1)
results = abs(y_pred - y_d) < np.abs(0.15 * y_d)
results
acc = np.sum(results) / len(results)
y_pred = model.predict(X_test)
y_d = np.array(y_test).reshape(-1, 1)
results = abs(y_pred - y_d) < np.abs(0.15 * y_d)
results
acc = np.sum(results) / len(results)

eval = model.evaluate(X_train, y_train, verbose=0)
print('Erro médio do conjunto de treinamento {0:.4f}'.format(eval[0]))
eval = model.evaluate(X_test, y_test, verbose=0)
print('Erro médio do conjunto de teste {0:.4f}'.format(eval[0]))