Step 1 of 6
Supervised learning & linear models
Regression and classification from first principles: fit a line, measure error, add regularization. The template every model follows.
16 min read
Every supervised learning algorithm follows the same template: take labeled data, fit a function that maps inputs to outputs, and evaluate how well it generalizes to data it hasn't seen. The differences are in what function you fit and how you measure "well."
The supervised learning setup#
You have a dataset of (input, output) pairs: features X and targets y. The goal is to learn a function f̂ such that f̂(x) ≈ y for new, unseen inputs. Two flavors:
- Regression: the target is continuous (predict a price, a temperature, a score)
- Classification: the target is categorical (predict spam vs. not spam, which digit 0-9)
The learning problem: find the function f̂ that minimizes expected loss over the data distribution.
The loss function L measures how bad a prediction is. For regression, mean squared error (MSE) is standard. For classification, cross-entropy loss. The choice of loss shapes everything the model learns.
Linear regression - the first model#
Start with the simplest possible function: a line (or hyperplane in higher dimensions).
Linear regression: predict y as a weighted sum of features plus a bias term.
The closed-form solution (ordinary least squares) sets the gradient of MSE to zero:
The normal equation: the optimal weights in one shot. Works when XᵀX is invertible.
This is pure linear algebra - the prediction is the projection of y onto the column space of X. When XᵀX is nearly singular (multicollinearity), the inverse amplifies noise. That's where regularization comes in.
- Multicollinearity
- When input features are highly correlated, making it hard to isolate each one's effect - the weight estimates become unstable.
- Regularization
- Adding a penalty to the loss function that discourages large or complex weights, preventing the model from memorizing noise.
Regularization - preventing overfitting#
Regularization adds a penalty for complexity to the loss, discouraging the model from fitting noise:
| Method | Penalty | Effect |
|---|---|---|
| Ridge (L2) | λ‖w‖² | Shrinks all weights toward zero; keeps all features |
| Lasso (L1) | λ‖w‖₁ | Drives some weights to exactly zero; automatic feature selection |
| Elastic Net | α‖w‖₁ + (1−α)‖w‖² | Combines both: sparse solutions with grouped correlated features |
Ridge regression: add λ‖w‖² to the loss. The solution adds λI to XᵀX, guaranteeing invertibility.
In deep learning: L2 regularization becomes weight decay, and dropout acts as an implicit regularizer by randomly zeroing activations during training.
- Weight decay
- The deep-learning name for L2 regularization - shrinks weights each step by a small fraction, keeping them from growing too large.
- Dropout
- During training, randomly sets a fraction of neuron outputs to zero each forward pass, forcing the network to spread knowledge across neurons.
- Sigmoid
- The S-shaped function σ(x) = 1/(1+e⁻ˣ) that squashes any real number to the range (0, 1) - used for probabilities.
- Cross-entropy
- A loss function that measures how far predicted probabilities are from the true labels - the standard loss for classification.
Logistic regression - classification#
For binary classification, you need probabilities, not raw numbers. Logistic regression passes the linear output through the sigmoid function:
Logistic regression: the sigmoid squashes the linear output to a probability between 0 and 1.
The loss is binary cross-entropy: −[y log(p) + (1−y) log(1−p)]. This is convex - gradient descent is guaranteed to find the global minimum.
For multiple classes, logistic regression extends to softmax regression: the linear outputs (logits) pass through softmax, which produces a probability distribution over all classes.
The loss landscape#
For linear and logistic regression, the loss is convex - one global minimum, no saddle points, no local minima traps. Gradient descent always converges (with a reasonable learning rate). This is the exception, not the rule: neural network losses are highly non-convex.
- 01Choose a model family (linear, polynomial, tree, neural net)
- 02Choose a loss function (MSE, cross-entropy, hinge)
- 03Choose a regularizer (L1, L2, dropout, none)
- 04Optimize: find parameters that minimize regularized loss
- 05Evaluate on held-out data
Gradient descent for linear models#
When the normal equation is too expensive (large datasets) or doesn't exist (logistic regression), use gradient descent. For MSE loss on linear regression:
The gradient of MSE with respect to weights. Each step moves weights to reduce the average prediction error.
This is the template every optimizer follows - the only difference between training a linear model and a neural network is the complexity of computing ∇L (backpropagation handles the latter).
Where it shows up in ML#
Linear layers are linear regression
Every dense layer in a neural network is a linear model (Wx + b) followed by a nonlinearity - the entire network is stacked linear models with activations between them.
Cross-entropy is the standard loss
Binary cross-entropy for two classes, categorical cross-entropy for many - the same loss that logistic regression uses, now applied to the softmax output of a neural network.
Regularization transfers directly
L2 → weight decay. L1 → sparsity. Dropout → randomly zeroing activations. Early stopping → regularization by limiting training time.
The normal equation appears everywhere
Least-squares solutions, ridge regression, kernel methods, and Gaussian processes all solve variants of (XᵀX + λI)⁻¹Xᵀy.