Skip to content

Instantly share code, notes, and snippets.

@harshildarji
Last active December 3, 2018 19:09
Show Gist options
  • Select an option

  • Save harshildarji/55d111d39e7a971b42558b0eef377b3d to your computer and use it in GitHub Desktop.

Select an option

Save harshildarji/55d111d39e7a971b42558b0eef377b3d to your computer and use it in GitHub Desktop.
'''
The table below gives the age (in year) and mileage (in KMs) of four used cars:
WARTBURG MOSKVICH LADA TRABI
Age: 5 7 15 28
Mileage: 30530 90000 159899 270564
1. Determine the weights w0 and w1 for a simple linear regression to predict mileage from age.
2. Use the model from (1) to predict the mileage for a 15-year old car.
'''
X = [5, 7, 15, 28]
Y = [30530, 90000, 159899, 270564]
x_mean = sum(X) / 4
y_mean = sum(Y) / 4
#print('x_mean: {}; y_mean: {}'.format(x_mean, y_mean))
cov = var = 0
for i in range(4):
cov += ((Y[i] - y_mean) * (X[i] - x_mean))
var += (X[i] - x_mean) ** 2
w1 = cov / var
w0 = y_mean - (w1 * x_mean)
print('w0: {:.3f}; w1: {:.3f}'.format(w0, w1))
mil = w0 + (w1 * 15)
print('Mileage for a 15-year old car is {:.3f} km'.format(mil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment