Synaplume

Core Algorithms

Linear Regression

50 min

The "hello world" that never retires

Linear regression predicts a number as a weighted sum of features:

y^=w1x1+w2x2++wnxn+b=wx+b\hat{y} = w_1 x_1 + w_2 x_2 + \cdots + w_n x_n + b = \mathbf{w} \cdot \mathbf{x} + b

House price = (weight for size)·size + (weight for age)·age + … + base price. It's over 200 years old (Gauss used it to locate an asteroid), yet it remains the most deployed model in the world: fast, stable, and every weight is readable — wsize=3200w_{\text{size}} = 3200 literally means "each extra m² adds $3,200, all else equal".

What does "best line" mean?

Define wrongness, then minimize it. The standard choice is mean squared error (MSE):

L(w,b)=1ni=1n(y^iyi)2L(\mathbf{w}, b) = \frac{1}{n} \sum_{i=1}^{n} \left( \hat{y}_i - y_i \right)^2

Why squared? It punishes big misses disproportionately, it's smooth (differentiable everywhere — the calculus path explains why that matters), and it corresponds to assuming Gaussian noise (the statistics connection). Plot MSE against the parameters and you get a perfect bowl — convex, one global minimum, no traps.

Finding the minimum

Two routes to the bottom of the bowl:

  1. The normal equation — solve directly with linear algebra: w=(XTX)1XTy\mathbf{w} = (X^TX)^{-1}X^T\mathbf{y}. Exact, elegant, but slow for many features.
  2. Gradient descent — the iterative descent from the math path. For MSE the gradient has a beautifully interpretable form: each weight's update is proportional to error × its feature value. Wrong by a lot on big-featured examples → big correction.

Route 2 is the same procedure that trains trillion-parameter language models. Linear regression is where you watch it work in a landscape simple enough to fully understand.

Beyond straight lines (without leaving linear-land)

"Linear" refers to linearity in the weights — you can feed in transformed features: x2x^2, logx\log x, x1x2x_1 x_2. Fitting y^=w1x+w2x2+b\hat{y} = w_1 x + w_2 x^2 + b gives a curve, trained exactly like before. This is polynomial regression, and it raises a fateful question: higher-degree polynomials fit training data ever better… but wiggle absurdly between points. Sound dangerous? It is — that's overfitting, and it gets its own lesson soon.

Reading the diagnostics

After fitting, professionals check:

  • R² ("R-squared") — fraction of the label's variance the model explains (1.0 = perfect, 0 = no better than predicting the mean).
  • Residual plots — plot errors vs. predictions; visible patterns mean the model is missing structure (maybe the relationship isn't linear).

Key takeaways

  • Linear regression: y^=wx+b\hat{y} = \mathbf{w}\cdot\mathbf{x} + b, trained by minimizing MSE — a convex bowl with one minimum.
  • Its weights are directly interpretable — a huge practical virtue.
  • Feature transforms give curves; flexibility invites overfitting (next chapters).

Try it yourself — live code

Output

Press Run to execute the code (it runs in your browser, in a sandbox).

Check your understanding

0/3 answered
  1. 1.Linear regression's standard training objective is minimizing…

  2. 2.A weight wsize=3200w_{size} = 3200 in a house-price model means…

  3. 3.R² = 0 means the model…

Share:

0% of Machine Learning completed

Up nextLogistic Regression & Classification