Answered step by step
Verified Expert Solution
Question
1 Approved Answer
from math import sqrt from matplotlib import pyplot as plot from random import seed from random import randrange from csv import reader # Step 2
from math import sqrt
from matplotlib import pyplot as plot
from random import seed
from random import randrange
from csv import reader
# Step : Load the csv file
def loadcsvfilename skipFalse:
dataset list
with openfilenamer as file:
csvreader readerfile
if skip:
nextcsvreader, None
for row in csvreader:
dataset.appendrow
return dataset
# Step : Convert any string column to a float column
def stringcolumntofloatdataset column:
for row in dataset:
rowcolumn floatrowcolumnstrip
# Step : Calculate the mean value of a list of numbers
def meanvalues:
return sumvalues floatlenvalues
# Step : Calculate a regularisation value for the parameter
def regularisationparameter lambdavalue:
return lambdavalue parameter
def variancevalues mean:
return sumx mean for x in values
def covariancex xmean, y ymean:
covar
for i in rangelenx:
covar xi xmeanyi ymean
return covar
# Step : Calculate least squares between x and y
def leastSquaresdataset:
x row for row in dataset
y row for row in dataset
xmean meanx
ymean meany
b covariancex xmean, y ymean variancex xmean
b ymean b xmean
return b b
# Step : Calculate root mean squared error
def rootmeansquareerroractual predicted:
sumerror
for i in rangelenactual:
predictionerror predictedi actuali
sumerror predictionerror
meanerror sumerror floatlenactual
return sqrtmeanerror
# Step : Make predictions
def simplelinearregressiontrain test:
predictions list
b b leastSquarestrain
for row in test:
yhat b b row
predictions.appendyhat
return predictions
# Step : Split the data into training and test sets
def traintestsplitdataset split:
train list
test listdataset
trainsize split lendataset
while lentrain trainsize:
index randrangelentest
train.appendtestpopindex
return train, test
# Seed the random value
seed
# Load and prepare data
filename 'fertilityrateworkerpercent.csv
dataset loadcsvfilename skipTrue
for i in rangelendataset:
stringcolumntofloatdataset i
# Evaluate algorithm
split
rmse evaluatesimplelinearregressiondataset split
printRoot Mean Square Error: f rmse
# Visualise the dataset
def visualisedatasetdataset:
testset list
for row in dataset:
rowcopy listrow
rowcopy None
testset.appendrowcopy
sizes, prices
for i in rangelendataset:
sizes.appenddataseti
prices.appenddataseti
plot.figure
plot.plotsizes prices, x
plot.xlabelFertility rate'
plot.ylabelWorker percent'
plot.grid
plot.tightlayout
plot.show
visualisedatasetdataset
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started