Kernel PCA came from one observation: if an algorithm needs only dot products between data points, a kernel can replace them and the algorithm becomes non-linear for free. Linear regression passes the same test, and the result, kernel regression, can fit curves of almost any shape while still solving a linear system.
The weights live in the span of the data
The least-squares solution satisfies the normal equations . Look at the right-hand side: is a combination of the training points. A short argument shows that the solution can always be taken as such a combination too. Split any weight vector into a part inside the span of the training points and a part orthogonal to all of them: . The orthogonal part has for every training point, so it changes no training prediction and cannot reduce the loss; dropping it loses nothing. Hence there is an optimal solution of the form
This is the regression version of the fact we used in kernel PCA, that the principal components are combinations of the data points, and it is a simple case of a general result called the representer theorem.
Everything in terms of dot products
Substitute .
Predictions on the training set are , where is the matrix of dot products, . The loss becomes
which is minimised when , so (if is invertible) .
A prediction for a new point is
Again only dot products appear: between the new point and each training point.
Swapping in a kernel
Replace every dot product by a kernel , which computes a dot product in some feature space:
This is linear regression in the feature space of , carried out without ever computing . With a polynomial kernel of degree it fits polynomials of degree ; with the RBF kernel it fits smooth curves of essentially any shape.
The prediction has an appealing reading. Each training point places a bump centred on itself, and the prediction at is the sum of all the bumps there. With the RBF kernel, nearby training points dominate and distant ones contribute almost nothing: the model predicts by similarity to the examples it has seen.
Making it work in practice: add a ridge
Solving exactly makes the curve pass through every training point, noise included, which is overfitting in its purest form. With the RBF kernel, is also often nearly singular (two close points give two nearly identical rows), so is numerically unstable. The standard remedy adds a small multiple of the identity:
This is kernel ridge regression. The term makes the system well conditioned and stops the fit from chasing every noisy label; the next chapters show that it corresponds to a penalty on the size of the weights (equivalently, a prior belief that they are small). Choosing , and the kernel's own parameters such as the RBF width , is done by validation.
The cost of kernels
Kernel regression trades the system of ordinary least squares for an one: about operations to solve and numbers to store. Prediction needs every training point (all kernel values for each new ), unlike linear regression, which only needs . That is fine for thousands of points and painful for millions. Approximations such as random features or using a subset of points as centres keep kernel methods practical at scale. Support vector machines (Module 10) solve the prediction-cost problem differently: their solution keeps only a few training points.