You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 KiB
34 KiB
None
<html lang="en">
<head>
</head>
</html>
In [12]:
# data for the OLS
import numpy as np
import matplotlib.pyplot as plt
theta0 = 2.6486
theta1 = 4.98
m = 50
disp = 4
x = np.linspace(0,5,m)
y = theta0+theta1*x+disp*np.random.rand(m)
In [13]:
plt.plot(x,y, '.k')
plt.show()
In [18]:
xSum = 0
ySum = 0
for i in range(m):
#print(x[i])
xSum = xSum + x[i]
ySum = ySum + y[i]
pass
xMean = xSum/m
yMean = ySum/m
print(xMean)
print(yMean)
In [20]:
num = 0
den = 0
for i in range(m):
num = num + x[i]*(yMean-y[i])
den = den + x[i]*(xMean-x[i])
pass
theta1H = num/den
theta0H = yMean-theta1H*xMean
print("theta 0: ", theta0H)
print("theta 1: ", theta1H)
In [21]:
yH = theta0H+theta1H*x
plt.plot(x, yH, ':r', label="Model")
plt.plot(x, y, '.k', label="Data")
plt.legend()
plt.show()