import numpy as np
import matplotlib.pyplot as plt
# generating equispaced points
x = np.linspace(0,1, 6)
betaTrue = np.asarray([1,1])
Xtilde = (np.vstack((np.ones((1, len(x))), x.reshape(-1,1).T))).T
t = np.matmul(Xtilde,betaTrue.reshape(-1,1))
# adding gaussian noise
tnoisy = t + np.random.normal(0,0.15,len(t)).reshape(-1,1)
plt.scatter(Xtilde[:,1], tnoisy, c='r')
plt.show()
maxIter = 10
eta = 0.1
k = 0
beta = np.random.normal(1,.1, (2,1))
# gradient descent iterations
for k in np.arange(maxIter):
grad = -np.matmul(Xtilde.T,tnoisy) + np.matmul(np.matmul(Xtilde.T, Xtilde), beta)
beta = beta - eta*grad
xtest = np.linspace(0,1,10)
Xtilde_test = (np.vstack((np.ones((1, len(xtest))), xtest.reshape(-1,1).T))).T
t_test = np.matmul(Xtilde_test,betaTrue.reshape(-1,1))
plt.plot(xtest, t_test)
plt.scatter(Xtilde[:,1], tnoisy, c='r')
predictions = np.matmul(Xtilde_test,beta.reshape(-1,1))
plt.plot(xtest, predictions)
plt.show()
# same with points distributed along a parabola
x = np.linspace(-2,1,20)
t = x**2 + x + 1
plt.plot(x, t)
xi = np.linspace(-2,1,10)
epsilon = np.random.normal(0,.1,len(xi))
ti = xi**2 + xi + 1 + epsilon
plt.scatter(xi, ti, c= 'r')
plt.show()
xi1 = xi
xi2 = xi**2
Xtilde = np.vstack((np.ones((1, len(xi))), xi1.reshape(-1,1).T))
Xtilde = np.vstack((Xtilde, xi2.reshape(-1,1).T)).T
Xtilde
array([[ 1. , -2. , 4. ], [ 1. , -1.66666667, 2.77777778], [ 1. , -1.33333333, 1.77777778], [ 1. , -1. , 1. ], [ 1. , -0.66666667, 0.44444444], [ 1. , -0.33333333, 0.11111111], [ 1. , 0. , 0. ], [ 1. , 0.33333333, 0.11111111], [ 1. , 0.66666667, 0.44444444], [ 1. , 1. , 1. ]])
maxIter = 10
eta = 0.01
k = 0
beta = np.random.normal(1,.1, (3,1))
# solving the linear regression problem through gradient descent on
# the features x_1 = xi, x_2 = xi^2
for k in np.arange(maxIter):
grad = -np.matmul(Xtilde.T,ti.reshape(-1,1)).reshape(-1,1) + np.matmul(np.matmul(Xtilde.T, Xtilde), beta).reshape(-1,1)
beta = beta - eta*grad
array([[0.94017014], [1.04532901], [1.02221728]])
# displaying the solution by first generating the features xi and xi^2 for eq
xtest = np.linspace(-2,1,20)
xtest1 = xtest
xtest2 = xtest**2
Xtilde = np.vstack((np.ones((1, len(xtest2))), xtest1.reshape(-1,1).T))
Xtilde = np.vstack((Xtilde, xtest2.reshape(-1,1).T)).T
prediction = np.matmul(Xtilde,beta.reshape(-1,1))
plt.plot(xtest1.reshape(-1,1), prediction)
plt.scatter(xi, ti, c= 'r')
plt.show()