Support Vector Machines
Maximum-margin hyperplane, kernel trick, soft margin
You are classifying loan applicants as default or no-default. You have a linearly separable training set — there exists some line that perfectly separates the two classes. Logistic regression will find one such line, whichever one minimises cross-entropy loss. But there are infinitely many separating lines. Which one do you want?
SVMs say: the one that is furthest from all training points. If you draw the two parallel boundary lines that touch the nearest points from each class, the space between them is the margin. A wider margin means more room for error — new test points that fall near the boundary are more likely to land on the correct side. Maximize the margin and you minimize the worst-case generalization error. This is the structural risk minimization principle.
Only a subset of training points define the boundary. The points that sit exactly on the margin edges — the closest points to the boundary — are the support vectors. Move any other training point and the boundary does not change at all. This sparsity is a structural property of the solution: the entire decision boundary is determined by a small minority of training examples.
Real data is not linearly separable. The soft-margin extension introduces slack variables $ξ_i \geq 0$: allow some points to violate the margin, but penalise each violation with cost $C$. Large $C$: tight margin, few violations, the model tries to classify everything correctly, prone to overfitting. Small $C$: wide margin, allows more violations, smoother boundary, better generalisation. C is the bias-variance dial.
The kernel trick makes non-linear boundaries possible without changing the algorithm. The dual form of the SVM only needs dot products $x_i^T x_j$ between training points — it never needs the feature vectors explicitly. Replace each dot product with a kernel function $k(x_i, x_j) = φ(x_i)^Tφ(x_j)$ for some mapping $φ$. The RBF kernel $k(x, x') = \exp(-γ |x - x'|^2)$ corresponds to an infinite-dimensional feature space. You never compute $φ(x)$ — you only compute $k(x_i, x_j)$, which is cheap. The SVM finds a linear separator in the infinite-dimensional space, which appears non-linear back in the original space.
NOT this. Most people think "SVMs are about the kernel." The kernel is how you apply maximum margin to non-linear boundaries — but maximum margin is the core idea, and the kernel is just a tool. Many practitioners can explain RBF kernels but cannot explain why maximum margin generalises well (structural risk minimization — the margin controls the VC dimension of the classifier). Without the why, you cannot diagnose when SVMs fail or explain their behavior to a stakeholder.
The hard limit: SVMs stall at $n > 50\text{K}$. The kernel matrix $K$ where $K_{ij} = k(x_i, x_j)$ requires $O(n^2)$ memory — 80GB for $n = 100\text{K}$ in float64. Training time is $O(n^2)$ to $O(n^3)$. For large datasets, use gradient boosting or linear models with SGD.
Key points
- Use kernel SVMs when n < 50K, the feature space is moderate-dimensional, and you have reason to believe the data is separable with a wide margin (e.g., clean binary classification with low noise). SVMs excel on small, clean datasets with well-defined boundaries — classic use cases include bioinformatics, text classification with TF-IDF features (linear SVM), and image patches. For n > 50K, switch to sklearn's LinearSVC (liblinear solver, O(nd) time) or SGDClassifier with hinge loss. Always StandardScaler before any SVM — the RBF kernel uses Euclidean distance and an unscaled feature with range [0, 1000] will dominate a feature with range [0, 1] regardless of predictive value.
- The production trap: tuning C and γ separately instead of jointly, and forgetting to scale features. C and γ interact: C=10, γ=0.01 produces a very different boundary than C=10, γ=10. A coarse grid search that sweeps C with fixed γ will miss the optimum. Always use a 2D grid on log scale: C ∈ {0.01, 0.1, 1, 10, 100}, γ ∈ {0.001, 0.01, 0.1, 1, 10}. The interaction means you need 25 combinations minimum, not 5 + 5. Missing feature scaling is the single most common reason for SVM underperformance — a single unscaled feature can make the kernel compute pure noise.
- The diagnostic: count the support vectors. Too many (> 50% of training data) means C is too large or γ is too small — the boundary is effectively ignoring the margin constraint. Too few may mean underfitting. A well-calibrated SVM typically has 5–30% of training examples as support vectors. Run svm.n_support_ after fitting. If it is near n, reduce C or increase γ to enforce a wider margin. Check test accuracy on a held-out set and compare to a logistic regression baseline — if they are within 1–2%, the kernel is not buying you anything and logistic regression is the simpler, faster choice.
SVMs maximise the margin — the gap between classes — and only the points on the margin edge (support vectors) determine the boundary; the kernel trick substitutes dot products with kernel evaluations to get non-linear boundaries without computing the feature map.
Recap
- SVM = maximise the margin, the gap between classes.
- Only support vectors (points on the margin edge) determine the boundary.
- Kernel trick: swap dot products for kernel evaluations → non-linear boundaries without computing the feature map.
- Soft margin (C) trades margin width for training errors.
- Reach for kernel SVM when n < 50K, moderate dimension, clean separable data — and always scale features.
- Tune C and γ jointly; count support vectors — >50% of data means C too large or γ too small.
Check your understanding
Q1. Your SVM with RBF kernel underfits the training data. Select the two adjustments that would actually help, each with its risk.
- `A) Increase C (fewer margin violations, tighter fit). Risk: the boundary turns wiggly and stops generalising past the training set — classic overfitting.`
- `B) Increase γ (a tighter, more localised RBF kernel). Risk: at high γ the model memorises training points inside tiny "bubbles," again overfitting.`
- `C) Switch from RBF to a degree-5 polynomial kernel, since higher-degree polynomial kernels always add more raw capacity with no numerical downside at all.`
- `D) Reduce C sharply (e.g. C=0.01), since underfitting with RBF always means C is too high, penalising slack variables far more than needed here.`
Q2. Explain the kernel trick. Why does it work, and what mathematical condition must a function k(x,x') satisfy to be a valid kernel?
- `A) It replaces each point xᵢ with φ(xᵢ) before the SVM runs; the condition is that φ be a bijection, guaranteeing the mapped problem has an equivalent solution.`
- `B) The dual SVM only needs dot products xᵢᵀxⱼ, so any k(x,x')=φ(x)ᵀφ(x') can substitute. Condition: k must be symmetric and positive semi-definite (Mercer's theorem).`
- `C) It approximates the feature-space inner product via a Taylor expansion of k(x,x'); the condition is that this series converges uniformly across the training set.`
- `D) It computes similarity k(xᵢ,xⱼ) directly from raw inputs; the condition is that k be a monotone decreasing function of the distance between the two points.`
Q3. SVMs and logistic regression both find a linear separator. In what situations would you prefer one over the other?
- `A) Prefer SVM for small, high-dimensional, well-separated data; prefer LR for calibrated probabilities, large n, or interpretability.`
- `B) Always prefer logistic regression: it learns the same weight vector as a linear SVM when separable, and additionally gives calibrated probabilities outright.`
- `C) Prefer SVM whenever class imbalance is present, since its margin criterion is fully independent of class frequency while LR's loss biases toward the majority.`
- `D) Prefer logistic regression for all practical applications now; every SVM advantage can be replicated with feature engineering, so SVMs are only academic today.`
Try it interactively
ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.
Open ML Systems Lab →