Kernel ridge regression

On this page 7

Kernel ridge regression (KRR) is a nonparametric regression method that applies ridge regression in a high-dimensional feature space defined implicitly by a kernel function, which lets it fit smooth nonlinear relationships while keeping a closed-form solution. In machine learning, and in artificial intelligence more broadly, it is a standard kernel method that sits between linear models and Gaussian processes.

The method minimizes squared error plus an L2 penalty on the function’s norm in a reproducing kernel Hilbert space. By the representer theorem, the optimal function is a weighted sum of kernels centered on the training points, so training reduces to solving one linear system of size n by n, where n is the number of training samples.

How kernel ridge regression works

Kernel ridge regression works by replacing every inner product in ordinary ridge regression with a kernel evaluation k(x, x’). The training data produce an n by n Gram matrix K, whose entries measure similarity between every pair of training points.

The dual coefficients are obtained from alpha = (K + lambda I)^-1 y, where lambda is the regularization strength and y is the vector of targets. A prediction for a new point x is then f(x) = sum_i alpha_i k(x_i, x), a weighted combination of similarities to all training samples. Adding lambda to the diagonal makes the matrix well conditioned and controls how closely the function follows the data.

Because the feature map is never computed explicitly, KRR can use kernels whose feature spaces are infinite-dimensional, such as the Gaussian radial basis function. This is the same kernel trick used by support vector machines and kernel PCA.

The table below lists the main elements of the model and their roles.

ElementNotationRole
Kernel functionk(x, x’)Defines similarity and the function class
Gram matrixK, size n by nPairwise similarities of training points
Regularization parameterlambdaBalances fit against smoothness
Dual coefficientsalphaWeights of each training point
Predictionf(x) = k(x)^T alphaWeighted sum of kernel similarities
Building blocks of kernel ridge regression

Choosing kernels and hyperparameters

The choice of kernel determines which functions KRR can represent, and the hyperparameters determine how flexible that representation is. The radial basis function kernel is the common default because it can approximate any continuous function given enough data.

KernelKey parameterSuitable for
LinearNoneEquivalent to ordinary ridge regression
PolynomialDegree, offsetFeature interactions of known order
Gaussian RBFLength scale or gammaSmooth general nonlinear functions
LaplacianGammaFunctions with sharper local changes
MaternSmoothness nu, length scalePhysical and spatial data
Common kernels used with KRR

Two hyperparameters usually matter most: lambda and the kernel width. A small lambda with a narrow kernel memorizes noise, while a large lambda with a wide kernel produces an almost flat function. Both are normally tuned together with k-fold cross-validation on a logarithmic grid, for example lambda from 1e-6 to 10. KRR also offers an efficient leave-one-out estimate computed from the same matrix inverse, which makes tuning cheaper than with many other models.

Because RBF and similar kernels depend on distances, feature scaling is essential. Unscaled inputs let one feature with a large numeric range dominate the similarity measure.

How KRR relates to neighboring methods

Kernel ridge regression shares its mathematical core with several better-known methods and differs from them mostly in loss function and interpretation. The predictive mean of a Gaussian process with noise variance equal to lambda is identical to the KRR prediction, but the Gaussian process also provides uncertainty estimates.

MethodLoss functionSolutionMain difference
Kernel ridge regressionSquared errorClosed formDense coefficients, fast to tune
Ridge regressionSquared errorClosed formLinear in the original features
Support vector regressionEpsilon-insensitiveQuadratic programSparse support vectors, robust to outliers
Gaussian process regressionGaussian likelihoodClosed formAdds predictive variance
Gradient boostingConfigurableIterative treesScales to large tabular data
KRR compared with related regression methods

Compared with support vector regression, KRR usually trains faster on medium datasets because it needs one linear solve rather than an optimization, but it predicts more slowly, since every training point contributes to every prediction.

Computational cost and scaling

The main practical constraint of KRR is its cubic training cost in the number of samples. Solving the n by n system takes on the order of n cubed operations and storing the Gram matrix requires n squared memory, so exact KRR becomes impractical beyond roughly tens of thousands of samples on a typical workstation.

Several approximations reduce this cost while keeping most of the accuracy, as the following table shows.

StrategyIdeaApproximate cost
Exact solveCholesky factorization of K plus lambda In cubed time, n squared memory
Nystrom approximationLow-rank kernel from m landmark pointsn times m squared
Random Fourier featuresExplicit random feature mapn times D squared
Iterative solversConjugate gradient with preconditioningSeveral passes over K
Divide and conquerAverage models trained on data subsetsDepends on partition count
Scaling strategies for large datasets

Limitations and common mistakes

Beyond cost, KRR has several practical weaknesses that experienced practitioners plan for. The squared loss makes it sensitive to outliers, the model is hard to interpret beyond inspecting kernel similarities, and the plain version gives no uncertainty estimate.

  • Skipping feature scaling, which distorts distance-based kernels.
  • Tuning lambda and kernel width separately instead of jointly.
  • Extrapolating far from the training data, where RBF predictions decay toward the mean.
  • Ignoring numerical conditioning when lambda is very small, which causes unstable coefficients.

In plain words

Kernel ridge regression predicts a number for a new case by looking at similar past cases. Each past case gets a weight. The prediction is a weighted mix of what happened before.

A kernel is simply a rule for measuring how similar two cases are. The ridge part keeps the weights modest. This stops the model from chasing every random bump in the data. The result is a smooth curve that follows the real pattern, even when that pattern is curved rather than straight.

A real-world example

A materials research group wants to predict the formation energy of new alloys from a set of composition and structure descriptors. Each laboratory or simulation result is expensive, so the team has only about 3,000 labeled compounds. They standardize the descriptors, choose a Gaussian RBF kernel, and tune lambda and the kernel width with five-fold cross-validation on a logarithmic grid.

Training takes a few seconds, and the tuned model predicts formation energy with errors small enough to rank candidate alloys before running costly simulations. The researchers use the model to screen thousands of hypothetical compositions and send only the most promising few dozen to full simulation. The screening step cuts simulation time substantially, and because the data set is small, the cubic cost of exact KRR never becomes a problem.