Gradient

Courses

∇\nabla∇The maths behind AI0/72
π\piπMaths0/84
⋆\star⋆AI for everyone0/10
Γ\GammaΓGame theory0/49
py\texttt{py}pyPython for AI0/15
y^\hat{y}y^​Build your own AI0/15
All courses

Tuition

My tuitionOlympiad trackCompetitions

Practice

ProjectsAsk a questionTrain on a GPUExplorationsMaths StoriesUnsolved problemsPlaygroundScience labsMock testsOlympiad

Reference

GlossaryBlogDiscoveries
Blog › Explore in 3D

25 November 2026 · 4 min readInteractive 3D

A hand writing formulas on a blackboard with chalk
Photo by Vitaly Gariev on Unsplash

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.

Press “Fit the plane” and watch it tilt into place. The thin lines are its errors.

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

  1. Start with a random plane.
  2. Work out which way to tilt and lift it to shrink the total squared error.
  3. Move it a little in that direction.
  4. 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.

  1. Mean hours = 2, mean score = 61.67
  2. m = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² = (11.67 + 0 + 13.33) ÷ 2 = 12.5
  3. c = ȳ − m x̄ = 61.67 − 25 = 36.67
  4. 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

  1. Q1. Line y = 2x + 1: predict y for x = 4. Answer: 9
  2. Q2. Points (1, 3), (2, 5), (3, 7): slope? 2, and intercept 1
  3. Q3. Actual 10, predicted 8: squared error = 4
  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

  • Lesson: fitting a line
  • Glossary: loss
  • Noise in data
  • What is supervised learning?
  • What is weight decay?
  • What is k-means clustering? Finding groups
  • Where is clustering used in real life?

Read and listen in

Indian languages

World languages

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.

More articles

  • What is noise in data? Why models miss points
  • What is k-means clustering? Finding groups
  • Where is clustering used in real life?