Classical machine learning
Kernels and Nonlinear Decision Boundaries
Understand the kernel trick, linear, polynomial, RBF, and domain kernels, hyperparameter interactions, support, and computational limits.
By the end you can
- Explain how a kernel represents inner products in another feature space
- Compare linear, polynomial, RBF, and domain-specific similarity assumptions
- Diagnose gamma, scaling, support, and kernel-validity risks
- Design a joint regularization, kernel, and computational evaluation workflow
Example
A linear separator can become nonlinear after the representation changes
Kernel methods compare examples. The comparison runs through an implicit or explicit feature space. Each of the comparisons below has been built, published and costed by somebody; none of them is a shape that merely could exist.
- Concentric circles: no line separates the raw coordinates, but radial similarity can distinguish inner from outer regions. The function that does it is exp(-gamma||x_i - x_j||^2). It is one of the four basic kernels in the LIBSVM guide.
- Periodic signals: a periodic kernel can encode similarity that repeats over time rather than decaying monotonically.
- Text strings: specialized kernels can compare subsequences without constructing every possible feature explicitly. Lodhi and colleagues at Royal Holloway defined one in 2002. Its feature space is all subsequences of length k, contiguous or not, weighted by an exponentially decaying factor of their span. Writing that space out is hopeless, and their abstract says why: “A direct computation of this feature vector would involve a prohibitive amount of computation even for modest values of k, since the dimension of the feature space grows exponentially with k.” The inner product is instead computed by dynamic programming in O(n|s||t|) time, where n is the subsequence length and |s|, |t| the document lengths.
- Polynomial interactions: a polynomial kernel represents combinations of input features up to a chosen degree. The LIBSVM guide writes it as (gamma x_i^T x_j + r)^d, so the degree d, the scale gamma and the offset r are all choices you make and later have to defend.
- Failure case: an expressive kernel can fit small noisy datasets while providing little support for deployment regions.
The kernel trick replaces inner products, not all computation
Many algorithms can be written using pairwise inner products between examples. A valid kernel computes the inner product that would occur in another feature space without constructing every transformed coordinate.
The algorithm still needs a kernel matrix or repeated similarity evaluations. Implicit features do not imply free scaling.
The trick arrived with its costs already tabulated. Boser, Guyon and Vapnik introduced it at COLT 1992, training ten separators on handwritten digits, one per class. The database held 7,300 training and 2,000 test images at 16 × 16 pixels. The kernels were polynomials of order q. Their table lists the dimension N of the corresponding feature space at each order. It is 256 at q = 1, about 3 × 10^4 at q = 2, and 8 × 10^7 at q = 3. At q = 4 it is 4 × 10^9, and at q = 5 it is 1 × 10^12. Across that same range the average number of supporting patterns per separator falls from 97 to 89, 79, 72 and 69. The test error goes 10.5%, 5.8%, 5.2%, 4.9%, 5.2%. A feature space of about 10^12 dimensions is bought without storing a single extra coordinate. What the model keeps is patterns: “The solution is expressed as a linear combination of supporting patterns.”
Figure
Comparison
Common kernels encode different similarity priors
Hyperparameters determine how local, smooth, or interaction-heavy the boundary becomes. The people who wrote the library these kernels ship in also wrote down which one to start with. They gave reasons in print rather than taste.
The linear kernel is the ordinary dot product, x_i^T x_j. It is equivalent to a linear separator in the supplied features. It is efficient for sparse high-dimensional data, and it offers no nonlinear rescue without engineered features. It is also not a separate country from the RBF kernel. The guide's first reason for making RBF the default is that the linear kernel is a special case of it (Keerthi and Lin 2003). The linear fit remains the baseline you run before anything richer. But it is a corner of the RBF family rather than an alternative to it.
The RBF kernel, exp(-gamma||x_i - x_j||^2), creates local smooth influence. Gamma sets the width of the neighbourhood, and the kernel is sensitive to scaling. The guide's verdict is one sentence: “In general, the RBF kernel is a reasonable first choice.” Part of the reason is arithmetic hygiene. RBF values obey 0 < K_ij <= 1, “in contrast to polynomial kernels of which kernel values may go to infinity (gamma x_i^T x_j + r > 1) or zero (gamma x_i^T x_j + r < 1) while the degree is large”.
That sentence is what “can grow extreme values” means for the polynomial kernel, (gamma x_i^T x_j + r)^d. The degree controls complexity. The scale and the offset matter. The structure it imposes is global interaction rather than local influence. The guide's other objection is cheaper to state: the polynomial kernel has more hyperparameters to search than the RBF kernel does, so it costs more grid to reach the same place.
A domain kernel encodes similarity for strings, graphs, sequences or sets, and can preserve specialized invariances that no generic kernel knows about. Two bills come with it. The first is mathematical validity, and the cautionary case is not exotic. Of the fourth basic kernel in its own list the guide records that “the sigmoid kernel is not valid (i.e. not the inner product of two vectors) under some parameters (Vapnik, 1995)”. That kernel ships with the library. The second bill is expense, and the string-kernel authors measured their own. Their Reuters data set held “approximately 9600 training examples and 3200 test examples with an average length of approximately 2300 characters”. At that size they judged the kernel “is too expensive to apply on large text collections”, which is why their paper introduces an approximation. A domain kernel must match the task evidence. It must also still be affordable at the size the task actually arrives in.
Linear kernel
Uses the ordinary dot product.
- Equivalent to a linear separator in the supplied features
- Efficient for sparse high-dimensional data
- No nonlinear rescue without engineered features
- Useful baseline before richer kernels
Polynomial kernel
Represents feature interactions up to a degree.
- Degree controls complexity
- Scaling and offset matter
- Can grow extreme values
- Global interaction structure
RBF kernel
Uses exponentially decaying squared distance.
- Creates local smooth influence
- Gamma controls neighborhood width
- Sensitive to scaling
- Flexible but can overfit
Domain kernel
Encodes similarity for strings, graphs, sequences, or sets.
- Can preserve specialized invariances
- Requires mathematical validity
- May be expensive
- Must match the task evidence
Visual
RBF gamma changes the radius of influence
The same training examples can create a broad smooth boundary or a collection of tiny local islands. Which one you get at the extremes is a proved result rather than a plotting impression. Keerthi and Lin proved it in Neural Computation in 2003, writing the kernel as K(x, x') = exp(-||x - x'||^2 / 2 sigma^2).
Small gamma is large sigma^2, and similarity decays slowly, creating broad influence and smoother boundaries. Pushed far enough it stops being smooth and starts being empty. Sigma^2 tending to infinity with C fixed is one of the three routes they identify to severe underfitting, in which the entire data space is assigned to the majority class. The other two are sigma^2 fixed with C tending to 0, and sigma^2 tending to 0 with C fixed small. Scikit-learn 1.9.0 states the same limit in engineering terms: with very small gamma “The resulting model will behave similarly to a linear model”.
Moderate gamma is the region where local structure appears while neighbouring cases still interact, and it is the region a search exists to find. The published grids show how wide the hunt has to be. The scikit-learn example searches C over np.logspace(-2, 10, 13) and gamma over np.logspace(-9, 3, 13).
Large gamma is sigma^2 tending to 0. Similarity decays quickly, and with C fixed sufficiently large this is severe overfitting in the paper's precise sense: small regions around the minority-class training examples are classified as that class, and the rest of the space as the majority class. The library says the same thing to its users. “If gamma is too large, the radius of the area of influence of the support vectors only includes the support vector itself and no amount of regularization with C will be able to prevent overfitting.” The islands are not a rendering artifact. They are the model.
The violation penalty and the kernel width are therefore not two independent dials, and Keerthi and Lin tie them together exactly. When sigma^2 tends to infinity with C = C_tilde * sigma^2, the classifier converges to the linear SVM with penalty C_tilde. Along that one path through the pair, the nonlinear model is the linear model. That is why C and gamma have to be searched together rather than one after the other.
- 01
Small gamma
Similarity decays slowly, creating broad influence and smoother boundaries.
- 02
Moderate gamma
Local structure appears while neighboring cases still interact.
- 03
Large gamma
Similarity decays quickly, allowing very local and potentially brittle regions.
- 04
C interaction
Violation penalty and kernel width jointly control fit complexity.
Not every similarity function is a valid kernel
A kernel used in standard SVM or kernel-ridge theory should generate a positive semidefinite Gram matrix under the required conditions, and an intuitive similarity can violate that property and break the geometry or optimizer assumptions.
Use established kernels. Otherwise verify the mathematical conditions of a custom design.
The condition is old enough to have a citation. It is current enough to have a shipping counterexample. It descends from a paper on functions of positive and negative type that James Mercer published through the Royal Society in 1909. The counterexample is installed on most machines that run an SVM. LIBSVM's kernel type 3 is the sigmoid, tanh(gamma*u'*v + coef0). Scikit-learn exposes the same function as the “sigmoid” kernel, with r specified by coef0. Hsuan-Tien Lin and Chih-Jen Lin studied it at National Taiwan University. With the sigmoid, they record, “the kernel matrix may not be positive semi-definite (PSD)”. It is only “conditionally positive definite (CPD) in certain parameters and thus are valid kernels there”. And “existing software may have difficulties such as endless loops when using non-PSD kernels”. A similarity function can be popular, implemented, and still outside the theory that justifies the solver.
This is the same kernel the practical guide flags while listing its four basic choices. That is worth noticing about the amber card in the comparison above. The validity requirement on a domain kernel is not a formality invented for exotic designs. It is a condition the fourth stock kernel in the standard library already fails under some parameters.
Steps
Tune a nonlinear kernel without searching blindly
Kernel hyperparameters interact strongly with preprocessing and regularization. The sequence below is not a preference. It is close to the procedure the LIBSVM guide recommends — scale the data, use the RBF kernel, grid-search C and gamma by cross-validation — and the guide publishes what each step is worth on one data set.
That data set is astroparticle physics: 3,089 training instances, 4,000 testing instances, 4 features, 2 classes. LIBSVM's default parameters on the raw data give 66.925% test accuracy. Linearly scaling each attribute to [-1, +1], changing nothing else, gives 96.15%. Scaling plus the grid search, which selects C = 2 and gamma = 2 at a five-fold cross-validation rate of 96.8922%, gives 96.875%. Table 1 records the same task at 75.2% accuracy “by users” against 96.9% “by our procedure”. The gap between a competent user and the written procedure on identical data is that table.
1. Fit a linear baseline, to determine whether the supplied features already separate the task.
2. Scale inside folds. The 66.925%-to-96.15% jump is this step and only this step. Doing it inside the folds is what keeps distance-based kernel parameters comparable without letting the held-out rows inform the scaling.
3. Search C and kernel settings jointly, on logarithmic ranges, under valid nested evaluation. “We found that trying exponentially growing sequences of C and γ is a practical method to identify good parameters”, the guide reports. Its example sequences are C = 2^-5, 2^-3, ..., 2^15 and gamma = 2^-15, 2^-13, ..., 2^3, with a coarse grid first and a finer grid on the better region.
4. Map support: inspect distances, support vectors and sparse regions.
5. Stress extrapolation: test points beyond observed feature support and under shift.
6. Measure computation: record Gram-matrix memory, training time and prediction support-vector count. The quantity to write down for the first of those is the standard cost of kernel training, O(m^2) space in the training set size m.
1. Fit a linear baseline
Determine whether supplied features already separate the task.
2. Scale inside folds
Make distance-based kernel parameters comparable.
3. Search C and kernel settings jointly
Use logarithmic ranges and valid nested evaluation.
4. Map support
Inspect distances, support vectors, and sparse regions.
5. Stress extrapolation
Test points beyond observed feature support and under shift.
6. Measure computation
Record Gram-matrix memory, training time, and prediction support-vector count.
Analogy
Comparing objects through a specialized measuring instrument
A device compares two materials by resonance rather than by color or weight. A learning algorithm can use those pairwise readings directly. Each material behaves as if it had been described in a hidden coordinate system.
Kernel values must obey mathematical consistency, and a useful pairwise comparison can still scale poorly across millions of objects. Both halves of that sentence have already been priced in this lesson. The consistency was priced by a kernel that ships in the standard library and fails the condition under some parameters. The scaling was priced by a table of row counts past which the readings cannot all be taken.
A kernel is a disciplined way to define similarity for an algorithm that operates through inner products.
Key idea
An RBF boundary can look persuasive where no data exists
Smooth contour plots fill the entire plane, including regions unsupported by training examples, and the classifier still returns a score because the kernel combines similarities everywhere.
Overlay training density. Add out-of-support rules before treating the colored surface as evidence.
Keerthi and Lin describe what that colour actually is away from the data, and it is not a weakened version of the local answer. In their severe-overfitting regime, sigma^2 tending to 0 with C fixed sufficiently large, small regions around the minority-class training examples are classified as that class and the rest of the space as the majority class. In their severe-underfitting cases — sigma^2 fixed with C tending to 0, sigma^2 tending to 0 with C fixed small, sigma^2 tending to infinity with C fixed — the entire data space is assigned to the majority class. Far from any support vector the score is not a reading of the region. It is the default the kernel decays to. A plotting library will render that default in the same confident colour it uses where the data is dense.
A continuous decision function is not continuous empirical support.
Kernel methods trade feature dimension for sample dependence
The kernel trick can avoid an enormous explicit feature vector. Standard kernel training often still depends on pairwise relations among samples. Memory and training cost can therefore grow rapidly with the number of rows.
Approximation methods, linearized features, or different model families may be necessary at scale.
That bill was measured early, and the modern library still documents it. In 1998 John Platt published sequential minimal optimization at Microsoft Research, and the abstract states the band. “SMO scales somewhere between linear and quadratic in the training set size for various test problems, while the standard chunking SVM algorithm scales somewhere between linear and cubic in the training set size.” His test case was the UCI adult income task: 32,562 training examples encoded as 123 binary attributes. His log-log fits give SMO ~N^1.9 against chunking ~N^3.1 for a linear SVM. For a Gaussian one they give ~N^2.1 against ~N^2.9. Scikit-learn 1.9.0 puts its own libsvm-based solver in a band that begins where Platt's measurements ended. It “scales between O(n_features × n_samples^2) and O(n_features × n_samples^3) depending on how efficiently the libsvm cache is used in practice”. It also names the size at which exact kernel training stops being practical. “The fit time scales at least quadratically with the number of samples and may be impractical beyond tens of thousands of samples.”
A second, independent group states the exponents and the sizes together. Tsang, Kwok and Cheung, at the Hong Kong University of Science and Technology, open their 2005 core vector machine paper by stating the standard cost of kernel training: O(m^3) time and O(m^2) space in the training set size m. “It is thus computationally infeasible on very large data sets.” Their Table 1 gives the sizes at which that becomes the binding constraint: checkerboard 1,000,000 patterns over 2 attributes; forest cover type 522,911 over 54; extended USPS digits 266,079 over 676; extended MIT face 889,986 over 361; KDDCUP-99 intrusion detection 4,898,431 over 127; UCI adult 32,561 over 123. An O(m^2) object over 4,898,431 rows is not slow, it is absent.
“Approximation methods may be necessary at scale” has a measured price. Rahimi and Recht put one on it in 2007. “Unfortunately, methods that operate on the kernel matrix (Gram matrix) of the data scale poorly with the size of the training dataset.” Their answer was to replace the Gram matrix with a fixed random feature map, and their Table 1 prices it. On the UCI Adult classification task, 32,000 instances over 123 dims — the same task Platt used — random Fourier features with D = 500 give 14.9% test error in 9 seconds, against 15.1% in 7 minutes for the exact SVM (SVMlight). On Forest Cover, 522,000 instances over 54 dims, random binning features with P = 50 give 2.2% in 25 minutes, against 2.2% in 44 hours for the exact SVM (libSVM). The identical error rate, at 44 hours against 25 minutes. The same table lists CPU (6,500 instances, 21 dims), Census (18,000 instances, 119 dims) and KDDCUP99 (4,900,000 instances, 127 dims).
Implicit high-dimensional representation can still require an explicit large sample-by-sample computation.
Kernel flexibility makes interpretation a boundary analysis problem
Individual raw-feature coefficients may not exist in an accessible form, so interpretation shifts toward support vectors, sensitivity, local perturbations, and comparison with simpler representations.
Do not infer a universal feature effect from a boundary whose behavior depends on all pairwise similarities.
There is a theorem behind that shift. The generalized representer theorem, published in 2001, extends Wahba's classical result “to a larger class of regularizers and empirical risk terms”. Its authors state the consequence directly. “The result shows that a wide range of problems have optimal solutions that live in the finite dimensional span of the training examples mapped into feature space, thus enabling us to carry out kernel algorithms independent of the (potentially infinite) dimensionality of the feature space.” The same property that makes the computation possible is what removes the per-feature coefficient. The fitted function is an expansion over training examples. So the objects available for inspection are those examples and their weights. Their number is a figure you have already seen fall, from 97 to 69 per separator across nine and a half orders of magnitude of implicit dimension.
Nonlinear similarity expands expressiveness while weakening simple coefficient narratives.
Key takeaways
- The kernel trick evaluates inner products in an implicit feature space without materializing every transformed coordinate: Boser, Guyon and Vapnik bought about 10^12 dimensions at q = 5 while storing 69 supporting patterns per separator.
- Linear, polynomial, RBF, and domain kernels encode different priors about similarity and boundary shape, and the library authors' own ranking is explicit — “In general, the RBF kernel is a reasonable first choice.”
- RBF gamma controls the radius of local influence and interacts strongly with the soft-margin penalty; Keerthi and Lin prove that with sigma^2 tending to infinity and C = C_tilde * sigma^2 the model converges to the linear SVM with penalty C_tilde.
- Distance-based kernels require fold-aware scaling and realistic support checks: on one astroparticle-physics data set the same solver gives 66.925% raw, 96.15% scaled to [-1, +1], and 96.875% scaled plus grid search.
- An intuitive similarity is not necessarily a mathematically valid positive-semidefinite kernel — the sigmoid ships as LIBSVM kernel type 3 and is only conditionally positive definite in certain parameters.
- Kernel methods can avoid explicit feature expansion while still costing O(m^3) time and O(m^2) space in the number of rows, which is why random binning features cut Forest Cover from 44 hours to 25 minutes at the same 2.2% error.