Chapter 2: Linear Models for Regression and Classification#
Suppose you want to predict tomorrow’s temperature, or guess whether a customer will click on an ad. Behind many of these predictions are two simple but powerful ideas: drawing a straight line through data, and bending that line to handle yes-or-no answers. This chapter builds those ideas from the ground up, and shows that they both come from one elegant principle: picking the model that makes the observed data most likely.
The Big Picture#
Linear models are often the first tool you reach for in supervised learning, and they remain benchmarks decades later because they are fast, interpretable, and surprisingly capable. In this chapter we start with linear regression for predicting numbers. Then we meet logistic regression for predicting classes. We will see that both can be understood using maximum likelihood estimation (MLE), which simply asks: “What parameter values would make our data most likely?” By the end, you will know how these models work and why they share the same mathematical foundation.
Linear Regression – The Simple Starting Point#
Imagine you have a scatterplot of house sizes (
Linear regression: A supervised learning model that predicts a continuous target
as a straight-line function of one or more input features .
For a single feature, the line is written:
Here
where
The job of training is to choose
Why squares? One reason is geometry: minimizing RSS gives the line that is closest to the points in a vertical direction. Another reason, more probabilistic, is that if we assume the errors (the vertical scatter) follow a normal distribution with constant variance, then minimizing RSS is exactly the same as making the data most likely. We will make that precise in a later section.
For a single feature, the optimal slope and intercept have closed-form solutions:
where
This is the normal equation. Here
Interpreting the coefficients is a great thing about linear models. If
📝 Section Recap: Linear regression fits a straight line (or hyperplane) to data by minimizing the sum of squared residuals, and its coefficients are easy to interpret as the expected change in the target per unit change in a feature.
Logistic Regression – From Probabilities to Decisions#
Now suppose you want to predict whether an email is spam (
At the heart of the model is the logistic function (also called the sigmoid):
Its shape is an S-curve: for large negative
Logistic regression: A linear model for binary classification that estimates the probability of the positive class by applying the logistic (sigmoid) function to a linear combination of the features.
Why the logistic function in particular? One clean answer comes from the log-odds. The odds of an event with probability
Logistic regression models the log-odds as a linear function of the inputs:
Solving for
To make a hard classification, we typically set a threshold – often 0.5 – and predict the class with the higher probability. The decision boundary where the two classes are equally likely occurs where the linear score equals zero:
Now, how do we find good coefficients? Unlike linear regression with squared error, there is no closed-form formula. Instead we turn to the principle that unites this chapter: maximum likelihood estimation.
📝 Section Recap: Logistic regression models the probability of a class by applying the sigmoid to a linear score; the log-odds transform makes the model linear and gives coefficients an odds-ratio interpretation.
Maximum Likelihood Estimation – A Unified View#
Both linear regression and logistic regression can be learned by asking the same fundamental question: Which parameter values make the observed data most likely? This is maximum likelihood estimation (MLE).
Maximum likelihood estimation (MLE): A method for learning model parameters by maximizing the probability (likelihood) of the observed data under the model’s assumed distribution.
We start by imagining a story for how the data were generated. The story defines a likelihood function
Consider linear regression first. A natural assumption is that the target
Under this model, each
Because the observations are assumed independent, the joint likelihood is the product. Taking the log (and keeping only the terms that depend on
Maximizing
Now turn to logistic regression. Here each
If
This log-likelihood is the negative of a quantity known as binary cross-entropy loss. Maximizing it pushes
So both models, though they look different, are learned using the same idea: maximize the likelihood. The only difference is the assumed data distribution: normal for regression, Bernoulli for classification. This insight lets you create new models by simply swapping the distribution; for example, a Poisson distribution for count data gives Poisson regression.
We often add a regularization term to prevent overfitting, but that is a story for another time. For now, remember that learning these linear models is about asking: given what we assume about the noise, what parameters make our training data the most likely outcome?
📝 Section Recap: Maximum likelihood estimation provides a single framework for learning both linear regression (via Gaussian likelihood) and logistic regression (via Bernoulli likelihood), turning loss functions into natural consequences of probabilistic assumptions.
Summary#
Linear regression and logistic regression may seem different – one predicts numbers, the other probabilities – but they share a deep connection. Both build on a linear combination of features, and both are trained by maximum likelihood estimation: pick the parameters that make the data most likely under a reasonable probability model. Understanding this connection gives you a template for designing and interpreting many other supervised learning algorithms: choose a distribution for the target, write down the likelihood, and then optimize. The next time you see a new model, try asking: what probability story is it telling?
| Key idea | What it means (plain English) | Why it matters |
|---|---|---|
| Linear regression | A model that predicts a continuous number as a weighted sum of input features plus a constant. | It is simple, interpretable, and often the first model to try; the coefficients directly tell you the expected change in the target per unit change in a feature. |
| Least squares / RSS | The sum of squared differences between actual and predicted values; minimized to fit the regression line. | Minimizing squares follows naturally from assuming normally distributed errors, and it gives a unique, easy-to-compute solution. |
| Logistic (sigmoid) function | An S-shaped curve |
It turns a linear score into a valid probability estimate for binary classification. |
| Log-odds (logit) transform | The natural log of the odds |
Modelling log-odds as a linear function makes the relationship between features and probabilities easy to interpret, and gives coefficients a meaningful odds-ratio interpretation. |
| Logistic regression | A linear model for binary outcomes that applies the sigmoid to a linear combination of inputs to estimate class probabilities. | It provides reliable probabilities, a clear decision boundary, and is a workhorse for many classification tasks like spam detection or medical diagnoses. |
| Maximum likelihood estimation (MLE) | A general method that finds the parameter values that maximize the probability (likelihood) of the observed data under a chosen probabilistic model. | It unifies the training of many models: linear regression’s least squares and logistic regression’s cross-entropy loss both come from MLE with different data distributions. |
| Normal / Gaussian likelihood | The assumption that target values are normally distributed around the model’s prediction. | Leads directly to the familiar sum-of-squared-errors loss for linear regression, and justifies why we care about minimizing squared differences. |
| Bernoulli likelihood | The assumption that each binary outcome follows a biased coin flip with a parameter |
Leads to the binary cross-entropy loss for logistic regression, which encourages the predicted probabilities to match the true labels. |