Kernels & the Kernel Trick
Source: Unit 2 §12
The problem: non-linearly separable data
Recall the observation that closed the SVM page: the dual involves only scalar quantities plus the dot product . That makes it cheap - as long as a dot product of two vectors is all we ever need.
But what if the data is not linearly separable at all?
The idea is to add another dimension until the data becomes separable. Which dimension, though, and how many? More dimensions means more computation, and real data already has on the order of 1000 dimensions - separating it might need scaling by another factor of 1000.
Transforming to higher dimensions
A 1-D example. Points on a line that no threshold separates. Map each one to a 2-D feature vector and the picture changes completely.
So there exists a set of dimensions - a feature map - that separates the data. The catch is that computing explicitly in high dimensions is expensive.
The kernel trick
Because the dual only ever needs the dot product , we can compute that dot product directly with a kernel function, without ever computing in the high-dimensional space at all:
The modified dual simply replaces with :
For any kernel to be valid, it must be expressible as a dot product in some feature space. That is Mercer's condition, and it is the whole licence for substituting into the dual.
Polynomial kernel
The worked demonstration: compute directly, then map both vectors into the higher-dimensional space and take the dot product there. Both give the same value. One dot product plus a constant, squared, replaces an expensive high-dimensional mapping.
- Take and . The cheap route is .
- Expand it: .
- The expensive route maps each vector with and takes the 6-dimensional dot product.
- That dot product expands to exactly the same six terms. So for the kernel implicitly includes all pairwise feature products - and we never build .
Gaussian / RBF kernel
- The most popular kernel - effectively synonymous with SVM, and the default choice.
- It has the same shape as a Gaussian, but it is not a PDF: it is not normalised.
- It measures similarity between and . Identical points give , the maximum; different points give a value in , and the farther apart they are, the smaller it gets.
- It depends only on the radial distance between the points, independent of their absolute position - hence Radial Basis Function.
- It effectively maps into infinite dimensions.
The hyperparameters C and γ
| Parameter | Controls | High value | Low value |
|---|---|---|---|
| γ (gamma) | Reach or width of a single training point's influence (RBF) | High variance, low precision - wiggly, tight boundary, overfits | Low variance, high precision - smoother boundary |
| C | Trade-off between margin width and the misclassification penalty | Hard margin - punishes errors heavily, risks overfitting | Soft margin - tolerates some misclassification, generalises more |
Tuning C and γ is how you balance bias against variance in an SVM - underfitting against overfitting. Small C and small γ give a smoother, more general boundary that may underfit; large C and large γ give a wigglier, tighter boundary that may overfit. With the right C and γ, SVMs work like a charm.