Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Motivation: the radial basis function

Scatter plot of the exercise data: a dense round cloud of points in the middle, surrounded by a broad ring of points at a distance of roughly 8 to 10 units. Every point has the same colour — no straight line separates the two groups.

A geometric transformation

Loading...
Loading...

Data transformed in this way can now be clustered with 2-means!

k-means in the transformed space

Three three-dimensional views of the same cloud of points from different angles, after the feature map has lifted the data into the height — the height is the squared distance from the origin. The inner clump lies blue and flat on the floor at a height near zero, the ring forms a green shell floating far above it, between 70 and 110. In the third view, almost straight down from above, the two appear as concentric circles again. At this height the two groups can be separated by a horizontal plane, in the plane below they cannot.

We can also run only the k-means computation in this larger space (with the radial feature x2+y2x^2+y^2), then carry the labels back and show them in our ‘old’ two-dimensional plot:

The same data as before, now coloured by the labels k-means assigned: the inner clump green, the outer ring blue. After the lift into the third dimension k-means separates the two groups cleanly, even though in the plane they are nested within one another.

What we actually needed

The k-means algorithm measures distances all the time. It is therefore invariant under translation and rotation. And if we do not transform our data at all, but merely measure distances with the notion of distance from the transformed space, that could, depending on the transformation, be less expensive to compute.

Other algorithms, too – k-nearest neighbours and support vector machines, for instance (neither of which we have discussed) – rest on nothing but measurements of distance.

Describing data by distances

The kernel trick is now to describe the data xix_i not by the symmetric matrix d(xi,xj)d(x_i, x_j) but by some other function κ(xi,xj)\kappa(x_i, x_j) that plays the same role as a measurement of distance in a transformed coordinate system.

The Euclidean distance is

d(a,b)=∥b−a∥=⟨b−a,b−a⟩=(b−a)T(b−a)d(a,b) = \lVert b - a \rVert = \sqrt{\langle b-a , b-a \rangle} = \sqrt{(b-a)^T (b-a)}

If one applies a linear transformation MM to the data, what one computes is

d(Ma,Mb)2=∥Mb−Ma∥2=∥M(b−a)∥2=⟨M(b−a),M(b−a)⟩=(b−a)TMTM(b−a)d(Ma, Mb)^2 = \lVert Mb - Ma \rVert^2 = \lVert M(b - a) \rVert^2 = \langle M(b-a), M(b-a) \rangle = (b-a)^T M^T M (b-a)

so that for a measurement of distance in the transformed space we do not actually have to compute MaMa and MbMb at all – ‘only’ apply MTMM^TM, rather often. With luck, that is the easier job.

Often the inner product ⟨a,b⟩\langle a, b\rangle itself is a good notion of distance too, or ⟨Ma,Mb⟩=bTMTMa\langle Ma, Mb\rangle = b^T M^TM a.

An aside, by way of explanation: we can also compute

∥b−a∥2=⟨b−a,b−a⟩=⟨b,b⟩−2⟨a,b⟩+⟨a,a⟩=∥a∥2+∥b∥2−2⟨a,b⟩{\lVert b-a \rVert}^2 = \langle b-a, b-a\rangle = \langle b,b\rangle - 2\langle a, b\rangle + \langle a, a\rangle = {\lVert a \rVert}^2 + {\lVert b \rVert}^2 -2\langle a, b\rangle

which for normalised vectors a,ba,b (that is, ∥a∥=1{\lVert a \rVert} = 1 and ∥b∥=1{\lVert b \rVert} = 1) shows a close relationship between ⟨a,b⟩=∥a∥∥b∥cos⁡(∡(a,b))=cos⁡(∡(a,b))\langle a, b\rangle = {\lVert a \rVert}{\lVert b \rVert}\cos(\measuredangle(a,b)) = \cos(\measuredangle(a,b)) and d(a,b)=21−⟨a,b⟩d(a,b) = \sqrt{2}\sqrt{1-\langle a, b\rangle}.

One now abstracts away from the linear situation and allows an arbitrary map into a vector space with an inner product.

Kernels

With a kernel one need not know the space FF and the map ϕ\phi at all, because for symmetric positive definite functions κ ⁣:X×X→R\kappa \colon \mathcal{X} \times \mathcal{X} \to \mathbb{R} there is always a construction of a ϕ ⁣:X→F\phi \colon \mathcal{X} \to F that represents κ\kappa as a kernel (Mercer’s theorem).

One also speaks of a Mercer kernel; more generally, functions κ\kappa that are not necessarily positive definite or symmetric are also called kernel functions.

One speaks of the kernel method in general when an algorithm is reformulated so that it works only with the inner products of the feature vectors, and these are then replaced by a kernel function.

Gaussian kernels

Note that we need not compare (x−μ)(x-\mu) with (y−μ)(y-\mu) here, because the two have the same difference x−yx-y. The usual normalisation drops out as well, because we are dealing not with probabilities but with a notion of distance.

array([[1.00000000e+00, 4.13904520e-58, 2.16272228e-01, ..., 1.22811834e-23, 2.13172511e-14, 5.18538063e-21], [4.13904520e-58, 1.00000000e+00, 1.21371818e-57, ..., 4.35762550e-19, 2.06913343e-19, 2.23830019e-21], [2.16272228e-01, 1.21371818e-57, 1.00000000e+00, ..., 1.19980400e-19, 2.05065431e-12, 4.27055216e-17], ..., [1.22811834e-23, 4.35762550e-19, 1.19980400e-19, ..., 1.00000000e+00, 1.64084897e-02, 7.84350348e-01], [2.13172511e-14, 2.06913343e-19, 2.05065431e-12, ..., 1.64084897e-02, 1.00000000e+00, 3.60038828e-02], [5.18538063e-21, 2.23830019e-21, 4.27055216e-17, ..., 7.84350348e-01, 3.60038828e-02, 1.00000000e+00]], shape=(4000, 4000))

But what does one do with the kernel?

Linear regression with ridge regularisation, for instance:

The same arrangement of ring and clump, coloured by the prediction of a kernel ridge regression. The ring is uniformly dark, the inner clump light, and at the edge of the clump a few points take an intermediate colour — there the prediction is undecided.

Kernel PCA

Principal component analysis computes the eigenvectors of the covariance matrix. One can also transform the data nonlinearly first – or else use the correspondingly transformed covariance matrix.

Two scatter plots side by side. On the left, an ordinary principal component analysis of the data: ring and clump still lie within one another, the PCA has separated nothing. On the right, the same sample after a kernel PCA: the inner class has collapsed into a narrow line at the far left, the outer one spreads to the right as a crescent — in this representation the two classes can be separated by a vertical line.

Strictly speaking, kernel PCA first increases the number of dimensions (with the nonlinear feature map), then transforms them linearly onto principal components, so that unimportant dimensions can be thrown away again. The first principal component in the feature space no longer corresponds to a straight line in the original observation space but to a nonlinear curve.