ML Notes
Deep dive into SVM classification covering maximum margin, kernel trick, soft margin, RBF kernel, C and gamma parameters, and complete sklearn implementation.
Support Vector Machines represent one of the most elegant ideas in machine learning. While other algorithms find any boundary that separates classes, SVM finds the BEST boundary — the one that maximizes the margin between classes. This mathematical rigor makes SVMs particularly powerful for high-dimensional data and remains the algorithm of choice in many specialized domains like text classification, bioinformatics, and image recognition.
The Core Intuition: Maximum Margin
Imagine you have two groups of points on a plane, and you need to draw a line separating them. There are infinitely many lines that could work. But which line is best? SVM argues that the best line is the one that sits as far as possible from both groups — maximizing the "margin" (the distance from the boundary to the nearest data points on each side).
Why does maximum margin matter? Because a boundary with a wide margin is more robust to new data. If the boundary barely grazes some training points, even slight variation in new data could cause misclassification. A wide margin provides a safety buffer.
Mathematical Foundation
The decision boundary in SVM is a hyperplane defined by:
w · x + b = 0
Where:
w = weight vector (normal to the hyperplane)
x = input feature vector
b = bias term
Classification rule:
If w · x + b > 0 → class +1
If w · x + b < 0 → class -1The margin is the distance between the two parallel hyperplanes w · x + b = +1 and w · x + b = -1. This distance equals 2/||w||. To maximize the margin, we minimize ||w||² subject to the constraint that all training points are correctly classified.
The data points that lie exactly on the margin boundaries are called support vectors — they "support" the hyperplane's position. A remarkable property of SVMs is that only these support vectors matter for defining the boundary. You could remove all other training points without changing the model.
The Kernel Trick: Non-Linear Classification
Real-world data is rarely linearly separable. SVM solves this through the kernel trick — mapping data to a higher-dimensional space where a linear separator exists, WITHOUT explicitly computing the transformation.
How Kernels Work
Consider data arranged in a circle (class 1 inside, class 0 outside). No straight line can separate them. But if we add a new feature z = x² + y², the inner points have low z values and outer points have high z values — now a plane at some z threshold separates them perfectly.
The genius of the kernel trick is that we never actually compute this high-dimensional mapping. Instead, we use kernel functions that compute dot products in the transformed space directly:
| Linear | K(x, y) = x · y |
| Polynomial | K(x, y) = (γ × x · y + r)^d |
| RBF (Gaussian) | K(x, y) = exp(-γ × ||x - y||²) |
| Sigmoid | K(x, y) = tanh(γ × x · y + r) |
The RBF (Radial Basis Function) kernel is the most popular because it can model complex non-linear boundaries and has only one parameter (gamma) to tune.
Soft Margin: Handling Overlapping Classes
In practice, classes often overlap — no boundary can perfectly separate all training points. Soft margin SVM allows some misclassifications by introducing slack variables and a penalty parameter C:
- Large C: Heavily penalizes misclassification → narrow margin, fits training data closely (risk of overfitting)
- Small C: Tolerates misclassification → wide margin, smoother boundary (risk of underfitting)
The C parameter directly controls the bias-variance tradeoff. Finding the right C through cross-validation is crucial for good SVM performance.
Complete Implementation
Understanding Gamma in RBF Kernel
The gamma parameter controls how far the influence of a single training example reaches:
- High gamma: Each training point has very local influence → complex, wiggly boundary that closely follows training points (overfitting risk)
- Low gamma: Each training point has far-reaching influence → smooth, simple boundary (underfitting risk)
Think of gamma as controlling the "flexibility" of the boundary. High gamma creates a boundary that wraps tightly around each training point cluster, while low gamma creates broad, gentle curves.
# Visualize effect of gamma (conceptual)
# gamma=0.001: Very smooth boundary, might underfit
# gamma=0.01: Moderate curvature, usually good starting point
# gamma=1.0: Very complex boundary, likely overfitting
# gamma=100: Each point creates its own island — extreme overfittingMulti-Class SVM
SVM is inherently binary, but handles multi-class through decomposition:
- One-vs-One (OvO): Train K×(K-1)/2 classifiers, one for each pair of classes. Each classifier votes, and the class with most votes wins. This is sklearn's default for SVC.
- One-vs-Rest (OvR): Train K classifiers, each separating one class from all others. Predict the class with highest confidence score.
When to Use SVM
SVMs excel with high-dimensional data (text classification with thousands of features), small to medium datasets (under 100,000 samples), clear margin separation between classes, and when you need strong theoretical guarantees. They struggle with very large datasets (training is O(n²) to O(n³)), noisy data with heavily overlapping classes, and when you need probability estimates (SVMs naturally output only class labels — probability calibration is an approximation).
Practical Tips
Always scale features before SVM. Start with RBF kernel. Use GridSearchCV to jointly tune C and gamma. If training is too slow, try LinearSVC for linear kernels (much faster) or subsample your data. SVM with RBF kernel and proper tuning often matches or beats ensemble methods on smaller datasets.
Key Takeaways
SVM finds the maximum margin hyperplane between classes, using the kernel trick to handle non-linear boundaries without explicitly mapping to higher dimensions. The C parameter controls the tradeoff between margin width and classification accuracy, while gamma controls boundary complexity for RBF kernels. SVMs are mathematically elegant, theoretically grounded, and practically powerful — especially for high-dimensional problems. Understanding SVMs deeply prepares you for kernel methods throughout machine learning and the concept of optimization with constraints.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Support Vector Machine (SVM).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, classification, support, vector, support vector machine (svm)
Related Machine Learning Topics