SVMs & Kernel Trick Explained
Support Vector Machines: optimal hyperplane, support vectors, kernel trick, and 4 kernel types with gamma tuning.
What Is a Support Vector Machine?
An SVM is a supervised learning algorithm that identifies the optimal hyperplane separating different classes in the feature space. Unlike other classifiers that just find any boundary that works, an SVM finds the one with the maximum margin — the widest gap between the boundary and the closest data points on either side.
Those closest points are called support vectors. They're the only data points that actually influence the boundary — everything else is irrelevant to the decision surface.
The Problem: Non-Linearly Separable Data
A straight hyperplane works fine when classes are cleanly separable. But real data is rarely that cooperative. When classes form rings, spirals, or overlapping clusters, no straight line can separate them.
The Solution: The Kernel Trick
Instead of drawing a curved boundary in low-dimensional space, the kernel trick projects the data into a higher-dimensional feature space where a straight hyperplane can separate the classes.
The key insight: you never actually compute the coordinates in that high-dimensional space. Kernels compute the dot products in the transformed space directly from the original inputs — giving you the power of high-dimensional separation at low computational cost.
The 4 Kernel Types
1 Linear Kernel
No transformation — computes a standard dot product in the original feature space. Use when data is already linearly separable. Fast, interpretable, no hyperparameters to tune.
2 Polynomial Kernel
Maps data into a polynomial feature space. With degree 2, it considers not just original features but also their squares and cross-products (x², y², xy). Captures feature interactions that a linear kernel misses.
3 RBF — Radial Basis Function Kernel
The default kernel in sklearn's SVM. Computes similarity between points using Euclidean distance — points closer together get higher similarity scores. Maps data into infinite-dimensional space, making it extremely flexible for complex boundaries.
Tuning Gamma (γ) in RBF
Gamma controls the influence radius of each training point — how far a single example reaches.
High γ
Each point influences only its immediate neighborhood. The boundary hugs the training data tightly, capturing fine-grained detail. Risk: overfitting.
Low γ
Each point influences a wide area. The boundary is smooth and generalized. Risk: underfitting when data has complex structure.
4 Sigmoid Kernel
Uses the hyperbolic tangent function — similar to the activation function in neural networks. Produces S-shaped decision boundaries. Less commonly used than RBF but useful when the data exhibits neural-network-like behavior.
Kernel Comparison at a Glance
Key Takeaways
- SVM goal: find the hyperplane that maximizes the margin between classes — not just any separating boundary.
- Support vectors are the only training points that matter — the rest can be removed without changing the boundary.
- Kernel trick: maps data to higher dimensions implicitly, enabling linear separation of non-linear data at low compute cost.
- RBF is the default in sklearn — flexible for most problems; tune γ and C via cross-validation.
- High γ → overfitting. Low γ → underfitting. GridSearchCV over γ and C is the standard approach.
For the applied side — training an SVC on the Iris dataset and tuning C, gamma, and kernels with real scores — see Support Vector Machine (SVM).
Frequently Asked Questions
What is a Support Vector Machine?
An SVM finds the optimal hyperplane that separates different classes in the feature space, maximizing the margin between the closest data points (support vectors) of each class.
What is the kernel trick in SVM?
The kernel trick maps non-linearly separable data into a higher-dimensional feature space where it becomes linearly separable — without ever explicitly computing the coordinates in that high-dimensional space. This makes SVMs efficient even with complex, curved decision boundaries.
What are the different SVM kernels?
The four main kernels: (1) Linear — standard dot product, for linearly separable data; (2) Polynomial — captures squares and cross-products of features; (3) RBF — default in sklearn, Euclidean distance similarity; (4) Sigmoid — mimics neural network activation.
What does gamma do in RBF SVM?
Gamma controls influence radius per training point. High γ: tight, complex boundaries that capture fine detail — risks overfitting. Low γ: smooth, simple boundaries — risks underfitting.
When should you use SVM over other classifiers?
SVMs excel in high-dimensional spaces, when features outnumber samples, and when a clear margin exists. Strong for text classification, image recognition, bioinformatics. They scale poorly on very large datasets — consider gradient-boosted trees or neural networks then.