To develop a neural network regression model for the given dataset.
Neural networks consist of simple input/output units called neurons. In this article, we will see how neural networks can be applied to regression problems.
Regression helps in establishing a relationship between a dependent variable and one or more independent variables. Although neural networks are complex and computationally expensive, they are flexible and can dynamically pick the best type of regression, and if that is not enough, hidden layers can be added to improve prediction.
Build your training and test set from the dataset, here we are making the neural network two hidden layer with activation layer as relu and with their nodes in them. Now we will fit our dataset and then predict the value.
Loading the dataset
Split the dataset into training and testing
Create MinMaxScalar object, fit the model and transform the data.
Build the Neural Network Model and compile the model.
Train the model with the training data.
Plot the performance plot
Evaluate the model with the testing data.
# Name : Venkatesh E
# Register Number : 212221230119
### Importing Modules
from google.colab import auth
import gspread
from google.auth import default
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential as Seq
from tensorflow.keras.layers import Dense as Den
from tensorflow.keras.metrics import RootMeanSquaredError as rmse
### Authenticate & Create Dataframe using Data in Sheets
auth.authenticate_user()
creds, _ = default()
gc = gspread.authorize(creds)
sheet = gc.open('Dataset 1.0').sheet1
rows = sheet.get_all_values()
df = pd.DataFrame(rows[1:], columns=rows[0])
df = df.astype({'Input':'float'})
df = df.astype({'Output':'float'})
df.head()
### Assign X and Y values
x=df[['Input']].values
y=df[['Output']].values
x
y
### Normalize the values & Split the
x_train,x_test,y_train,y_test = train_test_split(x_n,y,test_size = 0.3,random_state = 1)
scaler=MinMaxScaler()
scaler.fit(xtrain)
xtrain1=scaler.transform(xtrain)
### Create a Neural Network & Train it
model_1_0=Sequential([
Dense(8,activation='relu'),
Dense(15,activation='relu'),
Dense(1)
])
ai.compile(optimizer = 'rmsprop',loss = 'mse')
model_1_0.fit(xtrain1,ytrain,epochs=3000)
### Plot the Loss
loss=pd.DataFrame(model_1_0.history.history)
loss.plot()
### Evaluate the model
xtest1=scaler.transform(xtest)
model_1_0.evaluate(xtest1,ytest)
### Predict for some value
xn1=[[4]]
xn1_1=scaler.transform(xn1)
model_1_0.predict(xn1_1)
Thus a neural network regression model for the given dataset is written and executed successfully