Skip to content

Linear Regression

Predicts a number by fitting the straight line (or flat plane) that keeps the squared prediction errors as small as possible.

Alsoordinary least squares · OLS · least squares regression

Fig. / How it works
SHRINK THE SQUARED ERRORS
Role · kind
Models and algorithmsModel
Level
Beginner

Legendre (1805) and Gauss (1809), method of least squares

Imagine sticking a ruler through a cloud of dots so it passes as close to all of them as it can. Once the ruler is placed, you can read off a guess for any new dot.

Where it fits

+All relationships as a list

See it on the full map

How it works

Linear regression assumes the thing you want to predict changes in a straight line as the inputs change. With one input (say floor area) the model is a line; with two inputs it is a flat plane; with many inputs it is a “hyperplane”, but the idea is the same.

Each prediction is a weighted sum: multiply every feature by its weight, add them up, add a constant (the bias). Training means finding the weights that make predictions as close as possible to the true values in the training data.

“As close as possible” needs a definition, and that is the loss function. Linear regression uses mean squared error: for each point, take the vertical gap between the true value and the line (the residual), square it, and average. Squaring makes all gaps positive and punishes big misses much more than small ones.

There are two ways to find the best weights:

  1. Solve it directly. For squared error there is an exact formula, so you can compute the answer in one step.
  2. Walk downhill. Start with random weights and repeatedly nudge them in the direction that lowers the error. That is gradient descent, the same idea that later trains huge neural networks.
The math

With features x∈Rdx \in \mathbb{R}^d, weights ww and bias bb, the prediction is

y^=w⊤x+b\hat{y} = w^\top x + b

Over nn training points the mean squared error is

MSE(w,b)=1n∑i=1n(yi−w⊤xi−b)2\text{MSE}(w, b) = \frac{1}{n} \sum_{i=1}^{n} \left(y_i - w^\top x_i - b\right)^2

Stacking the inputs in a matrix XX (with a column of ones for the bias), the minimizer is the normal equation

w^=(X⊤X)−1X⊤y\hat{w} = \left(X^\top X\right)^{-1} X^\top y

which exists when the columns of XX are linearly independent.

Once trained, the weights are easy to read: a weight of 2,000 on “floor area in m²” means each extra square meter adds about 2,000 to the predicted price, holding the other features fixed.

Everyday use cases

  • Case 01

    House price estimates

    Predict a sale price from floor area, number of rooms and neighborhood, the classic first regression problem.

  • Case 02

    Sales forecasting

    Estimate next month's sales from advertising spend, season and price changes.

  • Case 03

    Fuel use

    Predict a car's fuel consumption from its weight and engine size.

Famous examples

Strengths, weaknesses and when to use it

Strengths

  • Fast to train, even on millions of rows.
  • Fully interpretable, each weight says how much the prediction moves per unit of a feature.
  • A strong, honest baseline to beat before trying anything fancier.

Weaknesses

  • Can only capture straight-line relationships unless you engineer features (squares, interactions).
  • Sensitive to outliers, because errors are squared.
  • Correlated features make individual weights unstable and hard to read.

When to use itWhen the target is a number, the relationship is roughly linear, and you care about explaining the prediction as much as making it.

Compare with

Check yourself

  1. Q01

    What does linear regression minimize when it fits a line?

    Show answer

    B: The sum of squared vertical distances between points and the line Least squares picks the line with the smallest sum (or mean) of squared residuals.

  2. Q02

    Which task is a good fit for linear regression?

    Show answer

    C: Predicting an apartment's rent from its size Rent is a continuous number, and size relates to it roughly linearly.

  3. Q03

    Why can a single extreme outlier pull the line a lot?

    Show answer

    A: Because errors are squared, so large errors dominate the loss

Go deeper