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 multiple 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 {wn=wn−α∂∂wnJ(w,b)b=b−α∂∂bJ(w,b)}\begin{aligned} & \text{repeat until convergence } \{ \\ & w_n = w_n - \alpha \frac{\partial}{\partial w_n} J(w, b) \\ & b = b - \alpha \frac{\partial}{\partial b} J(w, b) \\ & \} \end{aligned}

If we calculate the derivatives:

repeat until convergence {wn=wn−α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_n = w_n - \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 
<Figure size 1000x700 with 1 Axes>
<__main__.LinearRegression at 0x2202f6ee150>
LR1
mean: [-0.299   0.1769] std: [2.7437294 2.8673457]
Weights: [12.40141217 -7.51510148]
Bias: 1.1730256436710773
Prediction: 4.165163846319295
LR2
mean: [-0.299   0.1769] std: [2.7437294 2.8673457]
Weights: [12.39573878 -7.5106585 ]
Bias: 1.1726056602733947
Prediction: 4.16436449310532
LR3
mean: [-0.299   0.1769] std: [2.7437294 2.8673457]
Weights: [ 7.6980177  -4.52180807]
Bias: 0.7416254203248862
Prediction: 2.739843669937235
LR4
mean: [-0.299   0.1769] std: [2.7437294 2.8673457]
Weights: [0. 0.]
Bias: 0.0
Prediction: 0.0
<Figure size 1500x400 with 4 Axes>