◆ Algo3DVerse AI/ML
02 · Regression
AI/ML Visual Lesson 02

Linear
Regression

Find the best-fit line through noisy data by minimising prediction error — gradient descent in action, step by step.

Linear regression fits a straight line (or plane) to data by finding parameters that minimise the total squared error. Gradient descent does it automatically — nudging the line a little in the downhill direction of the error surface on every step, until it settles at the best fit.

Gradient descent in 3D

Yellow dots are training samples. The blue line starts at a random position and iterates toward the best fit. Residuals show the prediction error for each point.

Drag to rotate · scroll to zoom
EPOCH 0
MSE    —
SLOPE —
BIAS    —
Gradient Descent iterate the line toward the best fit

How it works

Three ideas drive every linear regression: the model, the cost function, and the optimiser.

📐

The Model

ŷ = mx + b

The model is just a line: slope m times the input, plus a bias b. With multiple inputs (features) it becomes a plane or hyperplane — same idea, more dimensions.

ŷ = w₁x₁ + w₂x₂ + … + b
📉

Cost Function (MSE)

mean squared error

We measure fit by squaring the distance from each data point to the line and averaging them. Squaring penalises large errors more, and gives a smooth surface to optimise.

MSE = (1/n) Σ (yᵢ − ŷᵢ)²
⛰️

Gradient Descent

optimisation · learning rate

Compute the slope of the error surface (the gradient), take a small step in the opposite direction. Repeat until the error stops falling. The step size is the learning rate.

w ← w − α · ∂L/∂w
🎯

Residuals

prediction errors

The vertical distance from each data point to the fitted line. Residuals should look random — if they have a pattern, the model is missing something the data contains.

eᵢ = yᵢ − ŷᵢ