x = [1.00, 1.13, 1.27, 1.41, 1.55, 1.68, 1.82, 1.96, 2.10, 2.24, 2.37, 2.51, 2.65,
2.79, 2.93, 3.06, 3.20, 3.34, 3.48, 3.62, 3.75, 3.89, 4.03,
4.17, 4.31, 4.44, 4.58, 4.72, 4.86, 5.00]
y= [13.26, 14.15, 13.86, 14.81, 15.68, 15.64, 15.58, 15.67, 16.08, 16.36, 16.14,
16.68, 16.00, 15.66, 16.05, 16.22, 16.17, 16.03, 16.69, 15.83, 16.21, 16.13,
16.36, 16.42, 16.92, 16.65, 16.94, 17.47, 18.07, 17.52]
x = np.array(x)
y = np.array(y)
A = np.vstack([x, np.ones(len(x))]).T
m, c = np.linalg.lstsq(A, y, rcond=None)[0]
plt.plot(x, y, 'ro', label='Original data', markersize=5)
plt.plot(x, m*x + c, 'b', label='Fitted line: {:.2f}x+ {:.2f}'.format(m,c))
plt.title("Least Squares Solution Ax=b")
plt.legend()
plt.show()
#Plotting the residual below
y_residual = y - (m*x+c) # residual = actual - predicted(OLS)
plt.plot(x,np.zeros(len(x)),color='b', label="Y=0 (shows accurate points)")
plt.scatter(x,y_residual,color='r',label="residuals")
plt.legend()
plt.show()