← All AI/ML articles

Linear regression, visualised.

Drag the points. Bend the line. Then open the hood: see exactly how a computer measures “wrong” and walks downhill to the best line.

1. Play with the line2. See the error3. Watch it learn
Click empty space to add a point · drag a point to move it · double-click a point to remove it.
MSE (error)0
R² (fit)0
Points0
Behind the scenes · 1

How wrong is the line?

For every point, the error is the gap between the line’s prediction (ŷ) and the real value (y). We square each gap so negatives don’t cancel and big misses hurt more, then average them. That average is the mean squared error (MSE), the number the computer tries to shrink.


MSE = (1/n) · Σ (ŷ − y)². Switch on Show squares above to literally see each squared error as a box. The line that makes the total box area smallest is the best fit.

Behind the scenes · 2

The loss landscape, and walking downhill

Every possible line is one point on this map: slope m across, intercept b up. Bright means low error, dark means high. The best line sits at the bottom of the bowl. Drag the white dot to pick any line yourself, or let gradient descent roll it downhill.


Cyan ring = the best line. White trail = gradient descent’s path.
Loss over steps. It should fall and flatten out.

Next update — before taking the step


The gradient tells which way is uphill. Each step moves the opposite way, by a distance set by the learning rate η. Too small is slow; too big overshoots and the loss explodes. Try it in the challenges below.

Behind the scenes · 3

The shortcut: a formula

For a straight line there is no need to walk: the lowest point of the bowl can be calculated directly. This is called ordinary least squares. Gradient descent matters because bigger models have no such formula.


The mathematics

The mathematics behind linear regression

Everything on this page comes from four ideas. Every number below updates live with the line and points you are playing with above.

A. The line: slope and intercept


Cyan dot = intercept. Blue step = run of 1. Pink step = rise (the slope).
ŷ = m·x + b
  • Slope m is how much ŷ changes when x increases by 1 (rise ÷ run). Positive m tilts the line up, negative tilts it down, and m = 0 is a flat line.
  • Intercept b is where the line crosses the vertical axis, the prediction when x = 0.

Move the sliders in the lab above and watch the triangle and numbers change.

B. The prediction error

ei = ŷi − yi

Prediction minus reality, for one point. Positive means the line is too high, negative means too low.

ei² = (ŷi − yi)²

Squaring removes the sign and punishes big misses more. This is the box you see with Show squares.

C. Mean squared error, with a worked example

MSE = 1/n · Σi=1n (ŷi − yi)²

Below, the formula is applied by hand to the first few of your points using your current m and b.


D. R²: how much does the line explain?

R² = 1 − SSres/SStot    where   SSres = Σ(yi − ŷi)²  ,   SStot = Σ(yi − &ybar;)²
How to read it
  • SSres is the error your line leaves. SStot is the error of the laziest model, always predicting the mean &ybar;.
  • R² = 1: the line hits every point.
  • R² = 0: no better than predicting the mean.
  • R² < 0: worse than the mean. This is possible when you pick the line by hand.
  • R² = 0.70 means the line explains about 70% of the variation in y.

E. How the model learns

“Learning” means choosing the m and b that make the MSE as small as possible:


(m*, b*) = argminm,b   MSE(m, b)

Walk downhill (gradient descent)
∂MSE/∂m = (2/n) · Σ (ŷ−y)·x ∂MSE/∂b = (2/n) · Σ (ŷ−y) m ← m − η · ∂MSE/∂m b ← b − η · ∂MSE/∂b

Repeat until the gradient is about zero. η is the learning rate.

Jump straight there (least squares)
m* = Σ(x−x̄)(y−&ybar;) ÷ Σ(x−x̄)² b* = &ybar; − m*·x̄

Setting both gradients to zero and solving gives this exact answer.

Experiment

Break it on purpose

One bad outlier

Add a point that fights the trend and watch the best line, and the squared box, get dragged.

Learning rate too high

Set η to 0.18 and run gradient descent. Watch the steps overshoot and the loss blow up.

Very noisy data

Turn the noise up. The best line still exists, but R² drops: the line explains less.

Check yourself

Three quick questions

Key takeaways

  • The model is just a line: ŷ = m·x + b. Two numbers to learn.
  • Learning means finding the m and b with the smallest mean squared error.
  • Gradient descent finds them by repeatedly stepping downhill on the loss landscape.
  • Least squares reaches the same answer in one calculation, but only for simple models.
  • Outliers matter a lot because errors are squared.
← Back to all articles