D = sio.loadmat("baseball_data.mat")
# X represents the total number of wins per team
X = D["X"].ravel()
# Y represents the total payroll per team
Y = D["Y"].ravel()
# The number of rows of matrix A m=X.size #by sorting the value of X, I realized that the minimum value in X is 672 and the maximum is 956, so I made the plotting range for x-axis from 650 to 1000
# n = 0
A=np.ones((m,1))
Q, R = la.qr(A) y=Q.transpose().dot(Y) x1=la.solve(R[0:1],y[0:1]) x_index= np.linspace(650, 1000, 256, endpoint=True) y_index=x1*np.ones((256,1)) plt.plot(X, Y,'o') plt.plot(x_index, y_index,label = 'fitted polynomial by QR factorization (n=0)') plt.xlabel('Wins') plt.ylabel('Payroll') plt.legend() plt.show()
# n = 1
A=np.ones((m,2))
A[:,1] = X
Q, R = la.qr(A) y=Q.transpose().dot(Y) x2=la.solve(R[0:2,0:2],y[0:2]) x_index= np.linspace(650, 1000, 256, endpoint=True) y_index=x2[0]+x2[1]*x_index plt.plot(X, Y,'o') plt.plot(x_index, y_index,label = 'fitted polynomial by QR factorization (n=1)') plt.xlabel('Wins') plt.ylabel('Payroll') plt.legend() plt.show()
# n =2
A = np.ones((m,3))
A[:,1] = X
A[:,2] = X**2
Q,R = la.qr(A) y=Q.transpose().dot(Y) x3 = la.solve(R[0:3,0:3],y[0:3]) x_index = np.linspace(650,1000,256,endpoint = True) y_index=x3[0]+x3[1]*x_index +x3[2]*((x_index)**2) plt.plot(X, Y,'o') plt.plot(x_index, y_index,label = 'fitted polynomial by QR factorization (n=2)') plt.xlabel('Wins') plt.ylabel('Payroll') plt.legend() plt.show()
# n = 3
A = np.ones((m,4))
A[:,1] = X
A[:,2] = X**2
A[:,3] = X**3
Q,R = la.qr(A)
y=Q.transpose().dot(Y)
x4 = la.solve(R[0:4,0:4],y[0:4])
x_index = np.linspace(650,1000,256,endpoint = True)
y_index=x4[0]+x4[1]*x_index +x4[2]*((x_index)**2)+ x4[3]*((x_index)**3)
plt.plot(X, Y,'o') plt.plot(x_index, y_index,label = 'fitted polynomial by QR