Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab: Linear Regression with one feature

Content

  1. Summary of Linear Regression

  2. Linear Regression with one feature

  3. Linear Regression with multiple features

Summary of Linear Regression

Upfront:

  1. Loss Function is Mean Square Error

  2. Optimizer is Gradient Descent

Linear Regression Model:

fw,b(x(i))=wx(i)+bf_{w, b}(x^{(i)}) = wx^{(i)} + b

Cost Function:

J(w,b)=12m∑i=1m(fw,b(x(i))−y(i))2J(w, b) = \frac{1}{2m} \sum_{i=1}^{m} (f_{w,b}(x^{(i)}) - y^{(i)})^2

Gradient Descent:

repeat until convergence {w=w−α∂∂wJ(w,b)b=b−α∂∂bJ(w,b)}\begin{aligned} & \text{repeat until convergence } \{ \\ & w = w - \alpha \frac{\partial}{\partial w} J(w, b) \\ & b = b - \alpha \frac{\partial}{\partial b} J(w, b) \\ & \} \end{aligned}

If we calculate the derivatives:

repeat until convergence {w=w−α1m∑i=1m(fw,b(x(i))−y(i))x(i)b=b−α1m∑i=1m(fw,b(x(i))−y(i))}\begin{aligned} & \text{repeat until convergence } \{ \\ & w = w - \alpha \frac{1}{m}\sum_{i=1}^{m} (f_{w,b}(x^{(i)}) - y^{(i)}) x^{(i)} \\ & b = b - \alpha \frac{1}{m}\sum_{i=1}^{m} (f_{w,b}(x^{(i)}) - y^{(i)}) \\ & \} \end{aligned}

Algorithm

While not max_steps:
    1.Find J(w, b)
    2.Find new Weight, and Bias using Gradient Descent

    if new J(w, b) and old J(w, b) has no or very less different
        break 
<__main__.LinearRegression at 0x20ec7681220>
LR1
weight: 3.994886365117163
bias: 1.0184618444746052
predicted: 24.987780035177583
LR2
weight: 3.9765256673068543
bias: 1.0846450935092746
predicted: 24.9437990973504
LR3
weight: 2.7567402642210097
bias: 0.7617635343126392
predicted: 17.302205119638696
LR4
weight: 0.0
bias: 0.0
predicted: 0.0
<__main__.LinearRegression at 0x20ec7681220>
<Figure size 1500x400 with 4 Axes>