Classical ML

The Kernel Trick

Projecting non linearly separable data into higher dimensional spaces without computing explicit feature vectors.

🔴 advanced5 min readsupervised
The Kernel Trick allows linear algorithms like Support Vector Machines to operate in high dimensional non linear feature spaces. Instead of explicitly transforming inputs phi(x) into high dimensional space, Kernel Functions compute inner dot products K(x, z) = <phi(x), phi(z)> directly in raw low dimensional input space. Popular kernels include Polynomial Kernel and Radial Basis Function (RBF / Gaussian) Kernel, which implicitly projects data into infinite dimensional Hilbert space.

The Non-Linear Separation Problem

What if data points cannot be separated by a straight line in 2D space?

Consider two concentric rings of data points:

  2D NON-LINEAR DATA (Concentric Circles):       3D TRANSFORMED DATA (Paraboloid z = x1^2 + x2^2):
     ▲                                                ▲ Feature Z
     │   (o) (o)                                      │    (o) (o)  <-- Higher Elevation!
     │ (o) (x) (o)   ──► Add Feature z = x1^2+x2^2 ──►│   ──────────  <-- LINEAR SEPARATING PLANE!
     │   (o) (o)                                      │    (x) (x)  <-- Lower Elevation!
  ───┴─────────────► Feature X1                       └─────────────► Feature X1

By mapping 2D inputs $(x_1, x_2)$ into a higher 3D space $\phi(x) = (x_1, x_2, x_1^2 + x_2^2)$, a simple flat linear plane can separate the classes!

However, calculating explicit high-dimensional feature transformations $\phi(x)$ for 1000 features creates millions of dimensions, causing memory and compute to explode.

The Kernel Trick Breakthrough

Notice that the dual formulation of Support Vector Machines relies only on inner dot products between sample pairs:

$$W(\alpha) = \sum_{i=1}^N \alpha_i - \frac{1}{2} \sum_{i=1}^N \sum_{j=1}^N \alpha_i \alpha_j y_i y_j \mathbf{\langle \phi(x_i), \phi(x_j) \rangle}$$

The Kernel Trick: Replace the expensive explicit inner product $\langle \phi(x), \phi(z) \rangle$ with a simple Kernel Function $K(x, z)$ evaluated directly in raw low-dimensional input space!

$$K(x, z) = \langle \phi(x), \phi(z) \rangle$$

You get all the power of high dimensional non linear decision boundaries at the computational cost of low dimensional input math!

Popular Kernel Functions

┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ 1. LINEAR KERNEL         │ 2. POLYNOMIAL KERNEL     │ 3. RBF / GAUSSIAN KERNEL │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ K(x, z) = x^T z          │ K(x, z) = (x^T z + c)^d  │ K(x, z) = exp(-γ ||x-z||²)│
│ Standard linear boundary.│ Computes interaction     │ Implicit Infinite        │
│ Best for text data.      │ terms up to degree d.    │ Dimensional Space!       │
└──────────────────────────┴──────────────────────────┴──────────────────────────┘

Radial Basis Function (RBF) Kernel

The RBF (Gaussian) Kernel is the most widely used kernel in SVMs:

$$K(x, z) = \exp\left( -\gamma |x - z|^2 \right)$$

Using Taylor series expansion of $e^x$, the RBF kernel mathematically corresponds to an Infinite Dimensional Feature Space!

Say this out loud

The Kernel Trick allows algorithms like SVM to learn non linear decision boundaries without computing explicit high dimensional feature vectors. A Kernel Function K(x, z) calculates inner dot products directly in low dimensional input space. The RBF Gaussian kernel implicitly projects data into an infinite dimensional space, controlled by parameter gamma.

Followups to expect

  1. What is Mercer Theorem? A mathematical theorem stating that any symmetric, positive semi-definite function $K(x, z)$ is a valid kernel function corresponding to an inner product in some Hilbert feature space.
  2. Why are Kernel SVMs rarely used on datasets with 1 Million rows? Evaluating $K(x_i, x_j)$ requires computing an $N \times N$ Gram matrix, which takes $O(N^2)$ memory and $O(N^3)$ compute time. Use Linear SVMs or Gradient Boosting for large datasets.

Check yourself

Question 1 of 3

What core computational bottleneck does the Kernel Trick eliminate when mapping data into high dimensional feature spaces?

More in Classical ML

See all →
Bias–Variance Tradeoff4 minOverfitting vs Underfitting3 minLinear Regression4 min