The Kernel Trick
Some classifiers only ever need dot products between pairs of points. A kernel computes that number directly from the original features, so the huge expanded space never gets built.
Why Does This Exist?
Facilities logs indoor temperature, humidity, and whether anyone filed a comfort complaint that hour. Complaints come from too cold, too hot, too dry, and too muggy, so comfortable hours sit in a blob ringed by complaints. No straight cut separates that, and the model is not weak: a support vector machine draws one flat surface, and no flat surface has an inside and an outside.
The obvious repair works on paper. Invent squared features and a flat cut in that space does the job, because a circle in the original features is a plane in the squared ones. Then you count columns. Every degree-2 combination of 1,000 features is 501,501 columns. Degree 3 is 167,668,501. The space you want, where the boundary curves however the data curves, is infinite-dimensional and cannot be built at all.
One idea to hold: a dot product multiplies matching entries of two vectors and adds them up. Margin methods only ever ask for that one number per pair, which is the opening the trick walks through.
Think of It Like This
The courier mileage chart
A courier plans a round trip between forty towns with no map and no coordinates. She holds a chart of road distances between every pair, and that chart answers everything she needs: which route is shortest, which towns sit close, where the day breaks.
Hand her driving times instead. A mountain pass puts two nearby towns hours apart, a motorway makes two distant ones adjacent. She plans exactly as before, and her plan is correct in that stranger geometry, without anyone drawing it.
One catch. The chart must correspond to some real arrangement of towns, even one nobody can sketch. Invent numbers where A to B plus B to C is shorter than A to C and her plans turn to nonsense. The kernel is the chart, the classifier is the courier, and no map gets drawn.
Where it stops: the courier still pays for every pair in the chart. Forty towns are trivial. Thirty thousand logged hours mean 900 million pairs, which is where this family hits its ceiling.
How It Actually Works
Write out a trained maximum-margin classifier and feature vectors never appear alone. They appear only inside dot products between pairs: two training points, or one training point with the point being predicted. The algorithm never asks where a point sits in the expanded space. It asks for the dot product of two points there, one number per pair.
A kernel is a function that returns that number from the original features:
Here is the projection you never evaluate and is the dot product in the expanded space. The answer is identical, not approximate.
Worked example: squaring one dot product replaces six terms
Take and . Their ordinary dot product is . The degree-2 kernel squares it after adding 1:
The honest degree-2 expansion of a two-feature vector holds every squared term, every cross product with its coefficient, the scaled originals, and a constant. Dotting those two long vectors term by term gives . Same 36, six terms against one dot product. On 60 features the honest lift needs 1,891 columns per row and the kernel still needs one dot product. At 1,000 features the lift would need 501,501 columns.
RBF, gamma, and which functions are allowed
The kernel used most is the radial basis function:
Here is the squared distance between the points and is a positive number you pick. This kernel corresponds to an infinite-dimensional space for one distance and one exponential. Read as reach. With squared distance and , the value is . Small keeps values near 1 far apart, so each point influences a wide region and the boundary stays smooth. Large collapses values toward 0 except for immediate neighbours, so the boundary wraps tightly around individual rows. It pulls against the margin penalty, so the two get tuned together after feature scaling.
Not every invented function is legal. Mercer condition is the requirement: the matrix of kernel values over any set of points must be positive semi-definite, so no combination of points gets a negative squared length. Pass it and a feature space with these dot products provably exists. Fail it and the mileage chart describes towns that cannot exist.
The bill is an matrix, quadratic in rows. At 30,000 hours that is 900 million entries. Past a few tens of thousands of rows the kernel version is usually wrong even when it fits best, and k-nearest neighbours or trees take over the same nonlinear problems at linear cost.
Code
The point is the identity: one dot product squared equals the long lifted dot product.
import math
a: list[float] = [2.0, 1.0]b: list[float] = [1.0, 3.0]plain: float = a[0] * b[0] + a[1] * b[1]kernel: float = (plain + 1.0) ** 2
s2: float = math.sqrt(2.0)lift_a: list[float] = [4.0, 1.0, s2 * 2.0, s2 * 2.0, s2 * 1.0, 1.0]lift_b: list[float] = [1.0, 9.0, s2 * 3.0, s2 * 1.0, s2 * 3.0, 1.0]lifted: float = sum(x * y for x, y in zip(lift_a, lift_b))print(plain, kernel) # -> 5.0 36.0print(round(lifted, 6)) # -> 36.0print(round(math.exp(-0.1 * 16), 2)) # -> 0.2The printout matches the hand computation: plain product , kernel , lifted , and the RBF value . Change to and both sides move together to .
Watch Out For
Leaving gamma at a value tuned for other units
Gamma multiplies a squared distance, so it only means something relative to feature scales. Switch temperature to Kelvin and the same gamma is a different reach entirely.
Symptom: every kernel value sits near 1 and the model predicts one class everywhere, or every value sits near 0 except a point with itself and the model memorizes. Fix: apply feature scaling first, then search gamma on a log scale jointly with the margin penalty.
Reaching for a kernel on a large dataset
The quadratic matrix is not a footnote. At 30,000 rows the pair matrix holds 900 million entries, and prediction compares each new row against every stored support vector.
Symptom: a job that fit on a sample never finishes on the full table. Fix: past tens of thousands of rows prefer a linear model on a few hand-built interactions, which captures much of the curvature at linear cost, or a tree ensemble that finds interactions itself.
The Quick Version
- No flat surface separates a blob from the ring around it, and expanding features by hand explodes to 501,501 columns at 1,000 features.
- Margin methods only use dot products between pairs, so the kernel computes that number and the projection never happens.
- On and the plain product is 5, the kernel is 36, and the six-term lifted product is also 36.
- The RBF kernel behaves like reach through , with distance 16 at 0.1 scoring 0.20. Scale features first and tune gamma jointly.
- A legal kernel passes Mercer condition. The bill is quadratic in rows, which caps the family.