Sub topic 9
The kernel trick: nonlinear boundaries without explicit features
This chapter assumes you know the soft-margin SVM and have seen the dual problem, where training depends on dual variables α. It also assumes you are comfortable with dot products and NumPy.
The problem with a linear boundary
Some data cannot be separated by a hyperplane in its original space. One fix is to map each point x to a richer feature vector φ(x), such as all pairwise products of features, and fit a linear SVM there. The cost is that φ(x) can have very many dimensions, or infinitely many.
Dot products are all the dual needs
In the dual problem, the training points appear only inside dot products xi · xj. The fitted decision function has the same property:
f(x) = Σ αi yi (x_i · x) + b
The sum runs over support vectors, because αi is zero for every other point. Replace each dot product with φ(xi) · φ(x) and the algorithm is unchanged. It now works in the mapped space.
A kernel function computes that mapped dot product directly:
k(x, z) = φ(x) · φ(z)
You never build φ(x). You only evaluate k. This is the kernel trick.
A worked check
Take the degree-2 polynomial kernel k(x, z) = (x · z)² on two-dimensional inputs. Its feature map is φ(x) = (x₁², √2·x₁x₂, x₂²). The code below checks that both routes give the same number.
import numpy as np
def phi(x):
return np.array([x[0] ** 2, np.sqrt(2.0) * x[0] * x[1], x[1] ** 2])
def poly2_kernel(x, z):
return (x @ z) ** 2
x = np.array([1.0, 2.0])
z = np.array([3.0, 0.5])
print("explicit features:", round(float(phi(x) @ phi(z)), 6))
print("kernel evaluation:", round(float(poly2_kernel(x, z)), 6))Both lines print 16.0. The kernel needed one dot product in two dimensions and one square. The explicit route built two three-dimensional vectors. With many input features and higher degrees, that gap becomes very large. The RBF kernel corresponds to an infinite-dimensional φ, so only the kernel route is possible.
What makes a valid kernel
A function k is a valid kernel if it is symmetric and its kernel matrix K, where K[i, j] = k(xi, xj), is positive semi-definite for any set of points (Mercer's condition). This guarantees that some φ exists and that the dual stays a convex problem. An arbitrary similarity score, such as a hand-made string distance, usually fails this test, and the solver may not converge to a meaningful answer.
Common errors
- Treating the kernel as free. Training works with an n × n kernel matrix, so memory and time grow quickly with the number of samples. Solvers often cache part of it.
- Expecting readable weights. With a nonlinear kernel, w lives in feature space and
coef_is not available. You inspect the dual coefficients and support vectors instead. - Skipping feature scaling. Kernels such as RBF depend on distances, so a feature with a large range dominates k.
Exercise
A kernel SVM is trained on 50,000 samples. Why does the kernel trick avoid computing φ(x), and what cost remains that grows with the number of samples?
Show answer
The dual objective and the decision function use only dot products, and k(x, z) returns φ(x) · φ(z) without building φ. What remains is the kernel matrix, which has one entry per pair of samples (50,000 × 50,000 here). Evaluating or caching it makes time and memory grow roughly quadratically with n.