Kernel Functions
A kernel tells you how alike two points are by returning their dot product in some big feature space, computed directly from the original coordinates without ever building that space.
Why Does This Exist?
Linear methods are fast but blind to curves: no straight line separates a ring from its center. Lifting points into a richer feature space fixes the blindness, yet the lifted coordinates can be infinite-dimensional, so building them is impossible. Kernel functions break the deadlock: algorithms like SVMs only ever need dot products between lifted points, and the kernel computes those from the raw inputs.
This page defines what makes a function a valid kernel and checks one on numbers. The algorithm that spends those dot products is the kernel trick; the space the lifting lands in is previewed in Hilbert spaces.
The one idea you need first
A positive definite matrix has all eigenvalues above zero, so every quadratic form through it stays nonnegative. Kernel validity is exactly this property demanded of every Gram matrix you can build. The matrix facts are in positive definite matrices.
Think of It Like This
A spice blend rated without tasting each spice
A chef rates how well two dishes pair without listing every molecule: one sniff of each finished plate gives a similarity score. The score behaves like a dot product of full ingredient lists, yet nobody wrote those lists down.
A kernel is that sniff. Two raw inputs go in, one similarity number comes out, and the number equals the inner product in a feature kitchen you never enter. Where it stops: not every similarity sniff qualifies. Only positive definite ones correspond to a real feature space; the rest silently produce nonsense geometry.
How It Actually Works
A function is a kernel when it is symmetric and positive definite: for every finite set , the Gram matrix is positive semidefinite ( for all ). In plain words: returns a similarity, and no collection of points can force those similarities into a geometrically impossible shape.
Three kernels carry nearly all of practice. Linear: . Polynomial: . Gaussian RBF: with width . Mercer's theorem says each valid kernel is genuinely a dot product in some Hilbert space, and the RBF's space is infinite-dimensional.
Worked example
Take the RBF kernel with on points and . Then , so . Diagonal entries are , giving Gram matrix:
Eigenvalues: and . Both positive, so is positive definite and this kernel behaves like a genuine dot product here. Small width pushes toward (near-orthogonal); large width pushes it toward (near-identical).
Code
Build the Gram matrix and check its eigenvalues stay positive, the validity test for any kernel.
import numpy as np
def rbf_gram(xs: np.ndarray, sigma: float = 1.0) -> np.ndarray: d2 = (xs[:, None] - xs[None, :]) ** 2 # pairwise squared distances return np.exp(-d2 / (2 * sigma**2)) # RBF similarity
xs = np.array([0.0, 1.0])print(rbf_gram(xs)) # -> [[1. 0.607] [0.607 1.]]print(np.linalg.eigvalsh(rbf_gram(xs))) # -> [0.393 1.607]Ones on the diagonal, off it, eigenvalues and : positive, exactly as computed by hand.
Watch Out For
Inventing a similarity that is not positive definite
Symptom: a custom kernel trains fine on small data then yields indefinite Gram matrices (negative eigenvalues like ) and the optimizer oscillates. Fix: eigencheck every new kernel on a few hundred points first; build custom kernels only from sums, products and scalings of known-valid ones.
Setting the RBF width by gut feeling
Symptom: tiny makes every Gram entry off-diagonal (memorization), huge makes them all (underfit). Fix: grid-search against validation error, or start from the median pairwise distance heuristic and tune from there.
The Quick Version
- A kernel returns the dot product in a feature space you never construct.
- Validity means every Gram matrix is positive semidefinite, checked through eigenvalues.
- Linear, polynomial and RBF kernels cover nearly all practice; RBF's feature space is infinite-dimensional.
- Points and with unit width score , with Gram eigenvalues and .
- Width controls similarity scale: small widths orthogonalize, large widths merge.