Implementing Simple Linear Regression¶

In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
# Considering some data {(x1,y1), (x2,y2), ..., (xn,yn)}
data = np.array([[0.2,0.3], [0.32,0.23], [0.39,0.35], [0.46, 0.42], [0.51, 0.39], [0.6, 0.47], [0.64, 0.51], [0.75, 0.46]])

print('Data =\n', data)

x = data[:,0]

print('\nx values: ', x)

y = data[:,1]

print('\ny values: ', y, '\n')

plt.figure(dpi=120)

plt.scatter(x,y)

plt.show()
Data =
 [[0.2  0.3 ]
 [0.32 0.23]
 [0.39 0.35]
 [0.46 0.42]
 [0.51 0.39]
 [0.6  0.47]
 [0.64 0.51]
 [0.75 0.46]]

x values:  [0.2  0.32 0.39 0.46 0.51 0.6  0.64 0.75]

y values:  [0.3  0.23 0.35 0.42 0.39 0.47 0.51 0.46] 


Calculating $\hat{\beta_0}$ and $\hat{\beta_1}$¶

In [3]:
beta1 = ((x * y).sum() - y.mean() * x.sum()) / ( (x**2).sum() - x.mean() * x.sum() )

beta0 = y.mean() - beta1 * x.mean()

plt.figure(dpi=120)

plt.scatter(x,y)

est_y = beta0 + beta1 * x  ## calcluate estimated y using beta_0 and beta_1

plt.plot(x, est_y, c='r')

plt.show()