25 November 2026 · 4 min readInteractive 3D
What is linear regression? Fitting a plane in 3D
With one input you fit a line. With two, you fit a flat plane through a cloud of points. Watch it learn.
Linear regression is the oldest trick in machine learning: find the straight line that best follows some data. Give it two inputs instead of one, say the size and the age of a house, and the line becomes a flat plane floating in 3D.
What “best” means
Each thin line joins a real data point to the plane’s guess for it. That gap is the error. Square every error, so big misses count much more than small ones, and add them up. The best plane is the one that makes that total as small as possible. This is called least squares.
How the plane learns
- Start with a random plane.
- Work out which way to tilt and lift it to shrink the total squared error.
- Move it a little in that direction.
- Repeat until it stops improving.
That is gradient descent again, with only three numbers to learn: one tilt for each input, plus a height. A neural network does the same with millions of numbers.
Turn up the noise. The plane still finds the trend, but the errors grow. No straight model can fit pure noise, and it should not try.
A worked example with a line
Three students studied 1, 2 and 3 hours and scored 50, 60 and 75. Find the best line score = m × hours + c.
- Mean hours = 2, mean score = 61.67
- m = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² = (11.67 + 0 + 13.33) ÷ 2 = 12.5
- c = ȳ − m x̄ = 61.67 − 25 = 36.67
- Prediction for 4 hours: 12.5 × 4 + 36.67 ≈ 86.7
With two inputs, say hours studied and hours slept, the same method fits a flat plane through points in 3D instead of a line through points on paper.
Reading the answer
The slope m = 12.5 says each extra hour of study is linked with about 12.5 more marks in this data. It does not prove that studying caused the marks; other things may have changed too. And predictions far outside the data, like 20 hours of study, are not trustworthy.
Measuring how good the fit is
The usual measure is the mean squared error: the average of the squared gaps between each point and the line. Another is R², the share of the variation in scores that the line explains, from 0 (none) to 1 (all of it).
Linear regression is often the first model to try. If it works well, you may not need anything more complicated.
More than one input
With inputs x₁ and x₂, the model is y = w₁x₁ + w₂x₂ + c. House price might be 4,000 × area + 2,00,000 × bedrooms + 5,00,000. Each weight shows the change in price for one more unit of that input, with the others held fixed.
Glossary
- Residual: actual value minus predicted value.
- Intercept: the prediction when all inputs are zero.
- R²: share of variation explained by the model.
- Extrapolation: predicting outside the range of the data.
Try these yourself
- Q1. Line y = 2x + 1: predict y for x = 4. Answer: 9
- Q2. Points (1, 3), (2, 5), (3, 7): slope? 2, and intercept 1
- Q3. Actual 10, predicted 8: squared error = 4
- Q4. Errors 1, −2, 1: mean squared error = (1 + 4 + 1) ÷ 3 = 2
Where it came from
The method of least squares was published by Legendre in 1805 and used by Gauss to predict where the dwarf planet Ceres would reappear in the sky. The word regression came later from Francis Galton, who studied the heights of parents and children.
In short
Linear regression fits the line or plane with the smallest total squared error. It is quick, easy to explain and often the right first model, as long as you remember it shows patterns, not causes.
Keep going
Questions people ask
What is linear regression used for?
Predicting a number from one or more inputs with a straight line or flat plane, such as house prices from size, or marks from hours studied.
What does “least squares” mean?
Choosing the line that makes the sum of squared vertical gaps between the points and the line as small as possible.
Does a good regression line prove cause and effect?
No. It shows a pattern in the data. Cause needs a controlled experiment or careful reasoning.