Everything so far has been unsupervised: data points with no answers attached. From here the course turns to supervised learning, where every training point comes with a label, the answer we want to predict. This chapter starts with the simplest supervised problem and the most widely used model of all: predicting a number with a straight line.
The supervised set-up
The training data is a set of pairs
The features describe an example; the label is the answer.
- In regression the label is a real number: . A house's floor area, age and distance to the station () and its price ().
- In classification the label is a category: for two classes, or one of classes. Modules 7 to 10 cover classification.
The goal is a function , learned from the training pairs, that predicts the label of a new well. As in Module 1, success means doing well on data the model has not seen.
Choosing a family and a loss
Searching over all possible functions is hopeless (and any function that simply memorises the training pairs would fit them perfectly). We restrict the search to a family, and the simplest useful family is the linear functions:
The weights are the parameters to learn. To include an intercept (a value when every feature is zero), append a constant feature ; its weight plays the role of the intercept, so we can keep writing .
To compare weight vectors we need a loss, a measure of how wrong the predictions are. The standard choice is the squared error, summed over the training set:
Squaring makes every error count positively, punishes large errors far more than small ones, and (as we will see) produces a smooth bowl-shaped function with a single minimum. Finding the that minimises it is least squares.
Solving it
Stack the training points as the columns of (as we did for PCA) and the labels into . The predictions for all points at once are , so
Its gradient is
Setting it to zero gives the normal equations
and, when is invertible,
is a convex quadratic (its Hessian is positive semi-definite), so this stationary point is the global minimum. The matrix is , the same matrix that appeared, divided by , as the covariance matrix in PCA.
A small example
Fit to three points: . With the constant feature, each :
The line is . Its predictions miss the labels by , and no other line has a smaller sum of squared misses ().
Non-linear features, still linear regression
"Linear" refers to the weights, not to the shape of the curve. Replace by any fixed set of features, such as , and the model is still linear in . The same formula fits it, with the feature matrix in place of . Polynomial regression, regression on logarithms or on products of features: all are linear regression on transformed features, the same feature-map idea we met in kernel PCA.
Fitting the training data is not the goal
With features , a polynomial can pass through all training points exactly: zero training error. But between the points such a curve swings wildly, and its predictions on new points are poor. This is overfitting: the model has fitted the noise in the training labels rather than the underlying pattern. A model that is too rigid has the opposite problem, underfitting: a straight line through a wavy pattern is wrong everywhere.
What we really want is low generalisation error, the expected error on new data from the same source. Training error is an optimistic estimate of it, increasingly so as the model becomes more flexible. Later chapters give the tools to control this: held-out validation data (cross-validation), and penalties on the size of the weights (ridge and lasso).