Linear Regression
Linear regression as the backbone of older ML models: decision boundaries of the form wx + b, and how the fit is found.
- regression
Linear and Logistic regression are the backbone of almost all older ML models. Any model with its decision boundary of the form:
(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.
| 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:
| 1 | 2 | 4 | 6 | 8 | 114 |
| 1 | 4 | 6 | 8 | 10 | 168 |
| 1 | 9 | 11 | 13 | 15 | 343 |
Then initialize a weight vector , often with zeros:
| 0 | 0 | 0 | 0 | 0 |
Feature and Label Matrices:#
Features (X):
| 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:
Where:
- is the actual label,
- is the predicted label,
Optimizer: Gradient Descent#
Update rule:
For MSE, this becomes:
You repeat this for every ( ) in the weight matrix until convergence.
Visualizing the Loss Surface#
In the case of two weights , , the loss surface 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)#
| 10 | 5 | 7 | 2 | 8 |
Verifying Predictions#
First training example:
| 1 | 2 | 4 | 6 | 8 |
Predicted:
Second training example:
| 1 | 4 | 6 | 8 | 10 |
Predicted:
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:
Check out LR.py for the linear regression code and SGD.py for gradient descent!