Skip to content
AI-grafen
CBuilderClassical machine learning· about 40 min· fundamentals that rarely change· verified 2026-09-20· EN

Linear regression: fitting a straight line to data

Be able to fit a straight line to data points, explain what 'error' (loss) means, and understand that training means minimising the error.

Prerequisites

Intuition

You have measured how many cups of coffee the café sells at different temperatures: (15 °C, 20 cups), (20 °C, 35 cups), (25 °C, 50 cups). It looks like a line. If you find the line, you can predict sales at 30 °C.

A model here is simply sales = k · temperature + m. "Training the model" means: find k and m so the line fits the points as well as possible.

Formal

How well does a line fit? Calculate the error for each point: actual value − line's prediction. Square the errors (so positive and negative values do not cancel out, and large errors are penalised heavily) and take the average. This is called MSE, mean squared error, or loss.

With k = 3, m = −25 on the coffee points:

tempactualpredictionerrorerror²
15202000
20353500
25505000

Loss = 0 — perfect. With real data, it is never zero; you seek the minimum. How to find the best k and m automatically is the next node: gradient descent.

Mastery means

  • Calculates the prediction and error for a given line and data point
  • Explains why errors are squared

Sign in to do the exercises and build your mastery up.

Sources

All the sources and licences