application_vnd.jupyter.stderr_output_1.png
from keras.layers import Input, Dense

from keras.models import Model

from math import sqrt

from sklearn import datasets

from sklearn.metrics import  mean_squared_error

from sklearn.metrics import r2_score

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

import statsmodels.api as sm

boston = datasets.load_boston()

bos = pd.DataFrame(boston.data, columns=boston.feature_names)
target = pd.DataFrame(boston.target, columns=['MEDV'])
bos['MEDV'] = target['MEDV']

correlation_matrix = bos.corr().round(2)

X = bos[['RM', 'LSTAT']]
Y = target['MEDV']
model = sm.OLS(Y, X).fit()
predictions = model.predict(X)
model.summary()

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33, random_state=5)

scalar = StandardScaler()
X_train = scalar.fit_transform(X_train)
X_test = scalar.transform(X_test)

input_data = Input(shape=(2,))
firstlayer = Dense(2, activation='relu', name='input')(input_data)
ff = Dense(2, activation='relu', name='ff')(firstlayer)
secondlayer = Dense(1, activation='linear', name='prices')(ff)
MLPRegModel = Model(inputs=input_data, outputs=secondlayer)
MLPRegModel.compile(loss='mse', optimizer='rmsprop')
MLPRegModel.fit(X_train, Y_train, epochs=250, batch_size=10)

predictions = MLPRegModel.predict(X_test)
'""\n#this is for remainder purpose\nseed=3 best for adam\nin adam batch size=1 best result\nMSE: 21.7102 epoch-500 batch 2 adam\nMSE: 22.2704 epoch-250 batch 2 adam\n\nwithout tensorflow seeding\nMSE: 21.3172 rmsprop "\nMSE: 20.3754 rmsprop with ff layer\nMSE: 20.758  rmsprop epoch- 500 batch 1\nMSE: 20.4079 rmsprop epoch- 500 batch 2\nMSE: 20.3896 rmsprop epoch- 150 batch 2\nMSE: 20.4025 rmsprop epoch- 150 batch 1\n\nafter tensorflow seed\nMSE: 20.425  rmsprop epoch- 250 batch 2\nMSE: 20.3996 rmsprop epoch- 250 batch 3\nMSE: 20.3558 rmsprop epoch- 250 batch 10\nMSE: 20.4105 rmsprop epoch- 500 batch 10\n\n#original way to calculate  mse\n\npred=pd.DataFrame(predictions)\npred.columns=["MEDV"]\npred["MEDV"]=pred.MEDV.astype(float)\nprint(pred.head())\nytest=pd.DataFrame(Y_test)\nprint(ytest.head())\npred.index=ytest.index\npred["diff"]=pred.loc[:,"MEDV"] - ytest.loc[:,"MEDV"]\nprint(pred.head())\nmse = (pred["diff"] ** 2).mean()\nprint(\'MSE: {}\'.format(round(mse, 4)))\n\n'

print('R2 score: {}'.format(round(r2_score(Y_test, predictions), 4)))
print('MSE: {}'.format(round(mean_squared_error(Y_test, predictions), 4)))
print('RMSE: {}'.format(round(sqrt(mean_squared_error(Y_test, predictions)), 4)))