Support Vector Machines
The algorithm that finds the widest possible boundary between classes. Margins, support vectors, the kernel trick, and when SVMs still beat neural networks.
Logistic regression draws any boundary that separates the classes. SVM draws the best boundary — the one with the maximum safety margin.
Imagine Stripe's fraud detection system. You have thousands of transactions — some fraudulent, some legitimate. You train a logistic regression. It draws a line that separates them correctly on the training data. But there are infinitely many lines that separate them correctly. Which one should you choose?
Logistic regression picks whichever line happens to minimise the loss. It could be a line that sits dangerously close to some legitimate transactions — technically correct, but fragile. A new transaction that is only slightly different from the training data might fall on the wrong side.
Support Vector Machines take a different approach. Instead of just finding any separating line, they find the line (or hyperplane in higher dimensions) that maximises the distance to the nearest points of both classes. This maximum distance is called the margin. A wider margin means the boundary is more robust — new points have to be much further off before they get misclassified.
Imagine drawing a road between two rows of houses. You could draw the road anywhere between them — but the safest road is the one exactly in the middle, with equal distance to both rows. Any car staying on the road has the maximum buffer before hitting a house.
SVM finds that middle road — the decision boundary equidistant from both classes, giving the maximum safety margin to new data points. The houses closest to the road are the support vectors — they are the only training points that actually determine where the road goes.
The margin — what SVM maximises
The margin is the total width of the gap between the two classes at the decision boundary. It is measured as twice the distance from the boundary to the nearest point of each class. SVM finds the boundary that makes this margin as wide as possible.
The hyperplane that separates the two classes. A line in 2D, a plane in 3D, a hyperplane in higher dimensions. All points on one side are predicted as class +1, all points on the other as class -1.
The training points closest to the decision boundary. These are the only points that determine where the boundary is. Remove any other training point — the boundary stays the same. Remove a support vector — the boundary moves.
The total width of the gap between the two classes at the boundary. Equal to 2 / ||w|| where w is the weight vector of the boundary. SVM maximises this margin — a wider margin means a more robust classifier.
Hard margin vs soft margin — handling overlapping classes
The margin explained above — where no training point is allowed inside the margin gap — is called a hard margin. It only works when the two classes are perfectly separable with a straight line. Real data almost never is. Some fraudulent transactions look exactly like legitimate ones. Some legitimate transactions look suspicious.
Soft margin SVM allows some training points to fall inside the margin or even on the wrong side of the boundary — but penalises them. The parameter C controls this trade-off: high C means "penalise violations heavily, keep the margin tight" (closer to hard margin). Low C means "allow more violations, keep the margin wide" (more regularisation, better generalisation).
The kernel trick — separate non-linear data without computing high dimensions
What if the two classes cannot be separated by any straight line? In 2D, circles around the origin versus points outside the circle cannot be split with a line — no matter how you draw it. SVM's solution: project the data into a higher-dimensional space where a linear separator does exist.
The problem with projecting to higher dimensions is that it becomes computationally very expensive — projecting to 1,000 dimensions means working with 1,000-dimensional vectors. The kernel trick solves this beautifully: it computes the dot product in the high-dimensional space without ever explicitly going there. It uses a kernel function that takes two original vectors and returns the same number as if you had projected them first and then taken the dot product. All the power of high-dimensional separation, none of the cost.
Imagine two groups of ants on a table — one group in the centre, one group around the edges. You cannot draw a straight line between them. But if you lift the table into the air and fold it into a bowl shape, suddenly the centre ants are at the bottom and the edge ants are up high — and you can cut them apart with a flat knife.
The kernel function is like the bowl shape — it transforms the space so a linear separator works. The kernel trick means you never actually have to fold the table — you just compute as if you did.
SVR — Support Vector Regression
SVM has a regression variant called SVR (Support Vector Regression). Instead of maximising the margin between classes, SVR fits a tube around the data — predictions within the tube incur no penalty. Only points outside the tube (the support vectors for regression) contribute to the loss. The width of the tube is controlled by the parameter epsilon.
When SVMs win — and when to use something else
SVMs were the dominant algorithm in ML from the late 1990s until around 2012 when deep learning took over. They are no longer the default choice for large-scale problems, but they still genuinely win in specific situations that come up regularly in production.
Every common SVM error — explained and fixed
Five things people get wrong about SVMs
The classic SVM training problem — a quadratic program with linear constraints — really is convex, and for a fixed kernel and C, solvers genuinely guarantee the global optimum. But that "global optimum" is only global with respect to the choices you already handed the solver. Switch the kernel from linear to RBF, or change gamma, and you get an entirely different convex problem with a different, possibly worse, global optimum. Model quality is not automatically solved by convexity; you still have to search over kernel and hyperparameter choices, and a perfectly optimal fit for a badly chosen kernel can lose to a mediocre fit for a well-chosen one.
Nothing is ever projected into that space in memory. The kernel trick is an algebraic shortcut: it computes what the dot product between two points would be if they had been projected into a higher, even infinite, dimensional space, without ever performing or storing that projection. The RBF kernel formula is just a number computed directly from the original low-dimensional inputs — no infinite-dimensional vector is ever created. This is exactly why SVM with an RBF kernel is not more memory-hungry than one with a linear kernel: the computation happens entirely inside the kernel function, never in some conjured high-dimensional space.
C controls a real tradeoff, not a dial to crank up for better performance. A high C tells the optimiser to penalise margin violations heavily, producing a narrow margin that hugs the training data closely — this can raise training accuracy while hurting generalisation, since the boundary becomes sensitive to individual points, including outliers. A low C allows more violations in exchange for a wider, more robust margin. The best C is whichever value generalises best on held-out data, found through cross-validation — not the largest value that fits the training set most tightly.
Standard kernel SVM training scales roughly quadratically to cubically in the number of training samples, because it works with a kernel matrix comparing every pair of points. At fifty thousand rows that is already a matrix with billions of entries. Past roughly a hundred thousand rows, SVM training routinely becomes impractically slow or memory-heavy, in a way logistic regression or gradient boosting simply do not. Where SVM genuinely wins is on small to medium, high-dimensional datasets — it is not a safe general default the way it is sometimes treated, and reaching for it on a large dataset without checking runtime first is a common practical misstep.
Strictly, only the support vectors — the points closest to, or violating, the margin — determine the final position of the boundary. Every other training point could be removed entirely, the model retrained, and the boundary would land in exactly the same place. This is exactly why SVM predictions only require storing the support vectors, often a small fraction of the training set, rather than the whole dataset — a genuine memory advantage at inference time. It also means the model is somewhat blind to the bulk distribution of "obvious" points that sit far from the boundary, unlike a model such as logistic regression whose loss is shaped by every single point.
SVM — 5 questions interviewers actually ask
Some datasets cannot be separated by any straight line in their original feature space — circles nested inside a ring is the classic example. Projecting the data into a higher-dimensional space can make a linear separator exist there, but explicitly computing that projection for every point would be expensive, or in some cases impossible if the target space is infinite-dimensional. The kernel trick sidesteps this: it uses a kernel function that returns exactly the dot product two points would have had if you had projected them first, computed directly from the original inputs. You get all the separating power of the higher dimension without ever visiting it.
A hard margin SVM requires the two classes to be perfectly separable, with no training point allowed inside the margin or on the wrong side of the boundary — that only works on data that is cleanly separable, which real-world data rarely is. A soft margin SVM introduces slack variables that allow some violations, each penalised in the objective, with the C parameter controlling how harshly those violations are punished. In practice the soft margin formulation is the default: it degrades gracefully on noisy or overlapping classes instead of failing to find any solution at all, which is what a hard margin would do if the data is not perfectly separable.
SVM's entire notion of a margin is a distance concept — it is measured in Euclidean terms, and kernels like RBF are explicit functions of the distance between two points. A feature with a much larger numeric range than the others dominates that distance calculation and effectively decides the boundary on its own, ignoring the rest. A tree-based model like random forest or XGBoost instead splits on one feature at a time using a threshold — whether that threshold is 5 or 5,000 does not change which side of the split a point falls on, so the split logic is invariant to scale in a way SVM's geometry simply is not.
I would push back before committing to it. Kernel SVM training scales roughly quadratically to cubically in the number of rows, so two million rows means a kernel matrix computation that is likely infeasible in reasonable time or memory. I would suggest either LinearSVC, which uses a solver that scales closer to linearly for a linear-kernel approximation, SGDClassifier with a hinge loss for a stochastic approximation of the same boundary, or moving to gradient boosting or a neural network if the data genuinely needs non-linear separation at that scale. Full kernel SVM is realistically a small-to-medium-dataset tool.
I would start with a linear kernel if the number of features is very large relative to the number of samples, as with text data represented by TF-IDF, since a linear boundary in that many dimensions is often already expressive enough, and RBF's extra flexibility mainly risks overfitting while slowing training down. If a linear kernel clearly underperforms and the dataset is small to medium in size, I would try RBF next and tune gamma and C together through cross-validation, watching for a growing gap between training and validation accuracy as a signal that gamma is too high and the boundary is overfitting to individual points.
SVMs find the best boundary. The next algorithm finds the nearest neighbours.
SVM is a global algorithm — it uses the entire training set to find the optimal boundary, then only remembers the support vectors. K-Nearest Neighbours (KNN) is the opposite — it is a local algorithm that remembers every single training point and makes predictions purely based on what the closest neighbours look like. No training phase. No boundary. Just: "what do the k points nearest to this new point look like?"
The simplest possible ML algorithm — predict based on what your neighbours look like. Distance metrics, the curse of dimensionality, and when KNN actually works in production.
🎯 Key Takeaways
- ✓SVM does not just find any separating boundary — it finds the boundary with the maximum margin: the widest possible gap between the two classes. A wider margin means more robust predictions on new data.
- ✓Support vectors are the training points closest to the boundary. They are the only points that determine where the boundary is. All other training points can be removed without changing the boundary at all.
- ✓C is the most important hyperparameter. High C = narrow margin, few violations (risks overfitting). Low C = wide margin, more violations allowed (more regularisation). Start with C=1.0 and tune with cross-validation.
- ✓The kernel trick projects data into higher dimensions where a linear separator exists — without the computational cost of actually working in those dimensions. RBF (Gaussian) kernel is the default and works well on most non-linear problems.
- ✓ALWAYS scale features before SVM. It is one of the most scaling-sensitive algorithms in all of sklearn. An unscaled feature with large values completely dominates the distance calculations and makes the model ignore all other features.
- ✓SVMs do not scale to large datasets — training complexity is O(n²) to O(n³). For datasets above ~50k rows, use LinearSVC, XGBoost, or a neural network. SVMs genuinely win on small high-dimensional datasets like text classification and biological data.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.