In python, cannot use the import command! Write functions about def mode(L), def
ID: 3711228 • Letter: I
Question
In python, cannot use the import command! Write functions about def mode(L), def Variance(L), def sample variance, def standard deviation, def standard deviation from sample, def sum of product, def the error function of x, def the regression(L,M): this one is to find the usual line of best fit using linear regression. In python, cannot use the import command! Write functions about def mode(L), def Variance(L), def sample variance, def standard deviation, def standard deviation from sample, def sum of product, def the error function of x, def the regression(L,M): this one is to find the usual line of best fit using linear regression. Write functions about def mode(L), def Variance(L), def sample variance, def standard deviation, def standard deviation from sample, def sum of product, def the error function of x, def the regression(L,M): this one is to find the usual line of best fit using linear regression.Explanation / Answer
As said, we can not use the defined functions. we need to write our own code for each of the function. Lets write each function one by one.
def mode(L):
def Variance(L):
average = averageOfSamples(L)
variance = 0
for l in L:
variance = variance + (average - l) ** 2
return variance/len(L)
# we need supporting functions for the same like sum and average
#calculate the sum
def sumOfSamples(L):
total = 0
for l in L:
total += l
return total
#Take the average by adding up each sample
def averageOfSamples(L):
sum_of_samples = sumOfSamples(L)
average = sum_of_samples / float(len(L))
return average
def samples_std_deviation(L):
return variance ** 0.5
variance = samples_std_deviation(L)
def SumOfProducts(L, M):
return sum( [L[i][0]*M[i] for i in range(len(M))] )
Given below is the complete code that we want for linear regression. Above are just reference functions. Here, we will use root mean squared error for the error calculation
def rmse_metric(actual, predicted):
sum_error = 0.0
for i in range(len(actual)):
sum_error = sum_error + ((predicted[i] - actual[i]) ** 2)
mean_error = sum_error / float(len(actual))
return sqrt(mean_error)
def mean(values):
return sum(values) / float(len(values))
def covariance(x, mean_x, y, mean_y):
covar = 0.0
for i in range(len(x)):
covar = covar + (x[i] - mean_x) * (y[i] - mean_y)
return covar
def variance(values, mean):
return sum([(x-mean)**2 for x in values])
def coefficients(dataset):
x = [row[0] for row in dataset]
y = [row[1] for row in dataset]
x_mean, y_mean = mean(x), mean(y)
b1 = covariance(x, x_mean, y, y_mean) / variance(x, x_mean)
b0 = y_mean - b1 * x_mean
return [b0, b1]
# Simple linear regression algorithm
def simple_linear_regression(train, test):
predictions = list()
b0, b1 = coefficients(train)
for row in test:
yhat = b0 + b1 * row[0]
predictions.append(yhat)
return predictions
def mode(L):
max(set(list), key=list.count)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.