vrishank

← Blog

AI / ML

Linear Regression

Linear regression as the backbone of older ML models: decision boundaries of the form wx + b, and how the fit is found.

· 2 min read

  • regression

Linear and Logistic regression are the backbone of almost all older ML models. Any model with its decision boundary of the form:

Y=WTX+BY=W^TX+B

(where (Y) is the label, (X) is the feature matrix, (W) is the weight matrix, and (B) is the bias, a scalar value) is called a Linear Model.

Let me quickly walk you through the key differences between Linear Regression and Logistic Regression.

Linear Regression Logistic Regression
Used to predict a value (label) when given corresponding features. Used to classify between two clusters given features. It gives a boolean output.
Error is measured using Mean Square Error (MSE). Error is measured using Binary Cross Entropy (maximizing negative log likelihood).

Linear Regression#

Here's how a computer solves a Linear Regression problem:#

You begin with a table of features (X) and labels (Y). Suppose there are M training examples and N-1 features.

X1X_{1} X2X_2 X3X_3 X4X_4 YY
2 4 6 8 114
4 6 8 10 168
9 11 13 15 343

Here, (M = 3) and (N-1 = 4.

To include a bias, add an extra column filled with 1s to your feature matrix:

X0X_0 X1X_1 X2X_2 X3X_3 X4X_4 YY
1 2 4 6 8 114
1 4 6 8 10 168
1 9 11 13 15 343

Then initialize a weight vector WW, often with zeros:

Bias/W0Bias/W_0 W1W_1 W2W_2 W3W_3 W4W_4
0 0 0 0 0

Feature and Label Matrices:#

Features (X):

X1X_1 X2X_2 X3X_3 X4X_4
2 4 6 8
4 6 8 10
9 11 13 15

Labels (Y):

Y


114


168


343



Error Function: Mean Square Error#

We use the Mean Square Error (MSE) as our loss function and Gradient Descent to optimize:

MSE=1m∑i=1m(Yi−Yi^)2\text{MSE}=\frac{1}{m}\sum_{i=1}^{m}(Y_i-\hat{Y_i})^2

Where:

  • YiY_i is the actual label,
  • Yi^=hθ(Xi)\hat{Y_i}=h_\theta(X_i) is the predicted label,
  • hθ(Xi)=Xi⋅Wh_\theta(X_i)=X_i\cdot W

Optimizer: Gradient Descent#

Update rule:

θj:=θj−α⋅∂J(θ)∂θj\theta_j:=\theta_j-\alpha\cdot\frac{\partial J(\theta)}{\partial\theta_j}

For MSE, this becomes:

θj:=θj−α⋅∑i=1m(hθ(Xi)−Yi)⋅Xij\theta_j:=\theta_j-\alpha\cdot\sum_{i=1}^{m}(h_\theta(X_i)-Y_i)\cdot X_{ij}

You repeat this for every ( θj\theta_j ) in the weight matrix until convergence.


Visualizing the Loss Surface#

In the case of two weights θ1\theta_1, θ2\theta_2, the loss surface J(θ)J(\theta) is a convex bowl-shaped curve with a single global minimum. This is why gradient descent works so effectively.

Contour Plot:


Final Weights (after optimization)#

Bias/W0Bias/W_0 W1W_1 W2W_2 W3W_3 W4W_4
10 5 7 2 8

Verifying Predictions#

First training example:

X0X_0 X1X_1 X2X_2 X3X_3 X4X_4
1 2 4 6 8

Predicted:

Y=X⋅WT=[1 2 4 6 8]⋅[10 5 7 2 8]T=114Y=X\cdot W^T=[1\ 2\ 4\ 6\ 8]\cdot[10\ 5\ 7\ 2\ 8]^T=114

Second training example:

X0X_0 X1X_1 X2X_2 X3X_3 X4X_4
1 4 6 8 10

Predicted:

Y=[1 4 6 8 10]⋅[10 5 7 2 8]T=168Y=[1\ 4\ 6\ 8\ 10]\cdot[10\ 5\ 7\ 2\ 8]^T=168

The predictions match the actual labels - exactly what we want!


Further Reading#

I’ve implemented Linear Regression and Gradient Descent from scratch using NumPy in my repo:

GitHub - ImVrishank/from_scratch_implementations: This is a repo of me implementing basic concepts of ML

Check out LR.py for the linear regression code and SGD.py for gradient descent!