Tuesday, August 4, 2020

Building Linear Regressor using ANN

In this post , we will learn how to build a linear regressor using artificial neural networks. You can use KerasRegressor to achieve this.

A gentle journey from linear regression to neural networks | by ...
In this example, we are using the Boston house price dataset with 13 numerical for properties in Boston. The Python code for the same is shown here:

Import all the required packages as shown:

import numpy
import pandas
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
from sklearn.model_selection import KFold

Now, load our dataset which is saved in local directory.

dataframe = pandas.read_csv("/Usrrs/admin/data.csv", delim_whitespace=True, header=None)
dataset = dataframe.values

Now, divide the data into input and output variables i.e. X and Y:

X = dataset[:,0:13]
Y = dataset[:,13]

Since we use baseline neural networks, define the model:

def baseline_model():

Now, create the model as follows:

model_regressor = Sequential()
model_regressor.add(Dense(13, input_dim=13, kernel_initializer='normal', activation='relu'))
model_regressor.add(Dense(1, kernel_initializer='normal'))


Next, compile the model:

model_regressor.compile(loss='mean_squared_error', optimizer='adam')
return model_regressor


Now, fix the random seed for reproducibility as follows:

seed = 7
numpy.random.seed(seed)

The Keras wrapper object for use in scikit-learn as a regression estimator is called KerasRegressor. In this section, we shall evaluate this model with standardize data set.

estimator = KerasRegressor(build_fn=baseline_model, nb_epoch=100, batch_size=5, verbose=0)
kfold = KFold(n_splits=10, random_state=seed)
baseline_result = cross_val_score(estimator, X, Y, cv=kfold)
print("Baseline: %.2f (%.2f) MSE" % (Baseline_result.mean(),Baseline_result.std()))


The output of the code shown above would be the estimate of the model’s performance on the problem for unseen data. It will be the mean squared error, including the average and standard deviation across all 10 folds of the cross validation evaluation.


Share:

0 comments:

Post a Comment