Huarui Zhou
import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import inv
x = np.linspace(0,20,100)
y = [np.sin(i) for i in x]
plt.scatter(x,y)
plt.show()
def y_pred(x,beta_list):
value = 0
for i in range(len(beta_list)):
value += beta_list[i]*(x**i)
return value
#generate the polynomial regression function of degree = deg (positive integer)
def Poly_reg(x,y,deg):
n = len(x)
X = []
x0 = [1 for i in range(n)]
X.append(x0)
for d in range(deg):
xd = [i**(d+1) for i in x]
X.append(xd)
X = np.transpose(np.array(X))
Y = np.array(y)
Xt = np.transpose(X)
XtX = np.matmul(Xt,X)
XtXi = inv(XtX)
XtXiXt = np.matmul(XtXi,Xt)
beta = np.matmul(XtXiXt,Y)
beta = np.transpose(beta)
y_list = [y_pred(i,beta) for i in x]
plt.plot(x,y_list,color = 'red',label = 'Regression curve')
plt.scatter(x,y,s = 20)
plt.legend(loc = 'upper right')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
return beta
Poly_reg(x,y,9)
array([-3.60746962e-02, 5.60114854e-01, 1.49460816e+00, -1.50741907e+00, 5.14307455e-01, -8.72912546e-02, 8.23217625e-03, -4.38788154e-04, 1.23740082e-05, -1.43538332e-07])