Linear Regression is usually the first Machine Learning algorithm because:
- It is simple
- It explains the core idea of ML:
👉 Learn a pattern from data - It introduces:
- Model
- Loss function
- Optimization
- Prediction
Almost everything in ML builds on this idea.
Imagine you want to predict house prices.
You notice:
- Bigger houses → Higher price
- Smaller houses → Lower price
You want a straight line that best fits the data.
That line helps answer:
“If the house size is 1200 sq.ft, what should the price be?”
The equation of a straight line:
y = mx + b
In ML terms:
y = w·x + b
Where:
x→ input (feature)y→ output (prediction)w→ weight (slope)b→ bias (intercept)
wcontrols how steep the line isbcontrols where the line starts
The goal of training:
Find the best w and b so the line fits the data well.
::contentReference[oaicite:0]{index=0}
- Dots → real data
- Line → model prediction
- The closer the line is to all points → better model
Suppose:
w = 50
b = 10
For:
x = 20
Prediction:
y = 50 × 20 + 10 = 1010
This is how the model makes predictions.
Prediction is never perfect.
Error for one data point:
error = predicted_y − actual_y
Example:
actual_y = 950
predicted_y = 1010
error = 60
But errors can be positive or negative, so we square them.
Mean Squared Error (MSE):
MSE = (1/n) Σ (y_pred − y_actual)²
Why square?
- Removes negative sign
- Penalizes large mistakes
Training means:
Adjust
wandb
so that MSE is minimum
This is the core idea of Machine Learning.
- Start with random values
- Measure loss
- Adjust slightly
- Repeat
This process is called Gradient Descent.
::contentReference[oaicite:1]{index=1}
Imagine a valley:
- Top → High loss
- Bottom → Minimum loss
Gradient Descent is like:
Taking small steps downhill until you reach the bottom.
import numpy as np
# house size
X = np.array([500, 800, 1000, 1200, 1500])
# price
y = np.array([50, 80, 100, 120, 150])w = 0.0
b = 0.0
learning_rate = 0.0001for epoch in range(1000):
y_pred = w * X + b
error = y_pred - y
dw = (2/len(X)) * np.sum(error * X)
db = (2/len(X)) * np.sum(error)
w -= learning_rate * dw
b -= learning_rate * dbhouse_size = 1300
predicted_price = w * house_size + b
print(predicted_price)- Before training → random line
- After training → best-fit line
-
Linear Regression learns a straight line
-
It uses:
- Model:
y = wx + b - Loss: Mean Squared Error
- Optimization: Gradient Descent
- Model:
-
Training = minimizing error
Machine Learning = Guess → Measure Error → Improve Guess → Repeat
After Linear Regression:
- Multiple Linear Regression
- Polynomial Regression
- Logistic Regression
- Neural Networks (same idea, more layers)


