Question: How to reduce my RMSE in my final code. My RMSE is 3 . 5 1 3 . Only step 2 - 9 is allowed

How to reduce my RMSE in my final code. My RMSE is 3.513. Only step 2-9 is allowed for editing.
Import the libraries.
from math import sqrt
from matplotlib import pyplot as plot
from random import seed
from random import randrange
from csv import reader
Step 2
Load the csv file.
def load_csv(filename, skip = False):
dataset = list()
with open(filename,'r') as file:
csv_reader = reader(file)
if skip:
next(csv_reader, None)
for row in csv_reader:
dataset.append(row)
return dataset
Step 3
Convert any string column to a float coulm.
def string_column_to_float(dataset, column):
for row in dataset:
# The strip() function remove white space
# then convert the data into a decimal number (float)
# and overwrite the original data
#CONVERTING STRING TO FLOAT
row[column]= float(row[column].strip())
###
Step 4
Calculate the mean value of a list of numbers.
def mean(values):
mean_results =0.0
#MEAN RESULT CALCULATION
mean_results = sum(values)/ float(len(values))
###
return mean_results
Step 5
Calculate a regularisation value for the parameter.
def regularisation(parameter, lambda_value=0.01):
#CALCULATION OF THE REHULARISED PARAMETER
regularised_parameter = parameter *(1- lambda_value)
###
return parameter
Step 6
Calculate least squares between x and y.
def leastSquares(dataset):
x = list()
y = list()
for row in dataset:
x.append(row[0])
for row in dataset:
y.append(row[1])
b0=0
b1=0
# using the formula to calculate the b1 and b0
numerator =0
denominator =0
x_mean = mean(x)
y_mean = mean(y)
#B1 SLOPE - CALCULATION
numerator = sum((x[i]- x_mean)*(y[i]- y_mean) for i in range (len(x)))
denominator = sum((x[i]- x_mean)**2 for i in range(len(x)))
#B0 SLOPE - CALCULATION
b0= y_mean - b1* x_mean
###
return [b0, b1]
Step 7
Calculate root mean squared error.
sum_error = sum((actual[i]- predicted[i])**2 for i in range(len(actual)))
mean_error = sum_error / float(len(actual))
rmse = sqrt(mean_error)
def root_mean_square_error(actual, predicted):
rmse =0.0
sum_error =0.0
###
sum_error = sum((actual[i]- predicted[i])**2 for i in range(len(actual)))
mean_error = sum_error / float(len(actual))
rmse = sqrt(mean_error)
###
return rmse
Step 8
Make Predictions.
predictions = list()
b0, b1= leastSquares(train)
# This function takes the training data to calculate the linear regression model parameters (b0 and b1)
# using the least squares method. Then it predicts the target values for the given test set.
# Each prediction is made using the formula yhat = b0+ b1* x, where b0 is the intercept, b1 is the slope,
# and x is the independent variable. The predictions are stored in a list and returned.
for row in test:
x = row[0]
yhat = b0+ b1* x
predictions.append(yhat)
def simple_linear_regression(train, test):
predictions = list()
b0, b1= leastSquares(train)
# This function takes the training data to calculate the linear regression model parameters (b0 and b1)
# using the least squares method. Then it predicts the target values for the given test set.
# Each prediction is made using the formula yhat = b0+ b1* x, where b0 is the intercept, b1 is the slope,
# and x is the independent variable. The predictions are stored in a list and returned.
for row in test:
x = row[0]
yhat = b0+ b1* x
predictions.append(yhat)
return predictions
Step 9
Split the data into training and test sets.
def train_test_split(dataset, split):
train = list()
train_size = split * len(dataset)
dataset_copy = list(dataset)
while len(train) train_size:
index = randrange(len(dataset_copy)) #0 to 99
train.append(dataset_copy.pop(index))
return train, dataset_copy
Step 10
Evaluate regression algorithm on training dataset.
def evaluate_simple_linear_regression(dataset, split=0):
test_set = list()
train, test = train_test_split(dataset, split)
for row in test:
row_copy = list(row)
row_copy[-1]= None
test_set.append(row_copy)
predicted = simple_linear_regression(train, test_set)
actual =[row[-1] for row in test]
rmse = root_mean_square_error(actual, predicted)
return rmse
Step 12
Seed the random value.
In [69]: seed(1)
How to reduce my RMSE in my final code. My RMSE

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!