Support Vector Machines
Support vector machines, margins, kernels, similarity features, and related optimization ideas.
A support vector machine (SVM) is capable for linear/nonlinear classification and regression. They are only good for small to medium-sized nonlinear datasets and do not scale well to large datasets.
Large margin classification - the idea of choosing the widest possible “street” between classes so the classifier is more robust on new data.
Remark. Remark SVMs are sensitive to feature scales.

Hard margin classification - the idea of imposing that all instances must be on the correct side. This only works if data is linearly separable and this is sensitive to outliers. Soft margin classification - the balance between large margin classification and hard margin classification.
When creating SVM model, there is a hyperparameter C where increasing C makes the street smaller and reducing makes the street larger.
Remark. >If your SVM model is overfitting, you can try regularizing it by reducing
C.
Although linear SVM classifiers are efficient and often work surprisingly well, many datasets are not even close to being linearly separable.
The kernel trick makes it possible to get the same result as if you had added many polynomial features, even with a very high degree, without actually having to add them.
Similarity Features
Similarity features are new features created by measuring how close each data point is to selected reference points called landmarks.
Instead of representing an instance only by its original feature value, we represent it by its similarity to one or more landmarks.
For example, suppose we have a one-dimensional point and a landmark . We can measure their similarity using the Gaussian RBF similarity function:
where:
- is the data point
- is the landmark
- is the squared distance between them
- controls how fast similarity decreases as distance increases
The output is always between and :
- close to means the point is very similar or close to the landmark
- close to means the point is far from the landmark
The key idea is that similarity features can transform a nonlinear dataset into a higher-dimensional feature space where it may become linearly separable. This allows a linear model, such as a linear SVM, to solve problems that were not linearly separable in the original feature space.
| Class | Time Complexity | Out-of-Core Support | Scaling Required | Kernel Trick |
|---|---|---|---|---|
LinearSVC | No | Yes | No | |
SVC | to | No | Yes | Yes |
SGDClassifier | Yes | Yes | No |
Support Vector Regression (SVR)
Support Vector Regression (SVR) is an extension of Support Vector Machines used for regression tasks. Instead of trying to separate classes with the widest possible margin, SVR tries to fit a function that keeps as many training instances as possible within a margin of width around the predicted regression line, while limiting the number and size of margin violations.
Formally, SVR seeks a function such that most predictions satisfy
where defines the width of the margin, also called the -insensitive tube. Errors inside this tube are ignored, meaning that training instances within the margin do not affect the model’s predictions. Only instances outside the tube contribute to the loss and become support vectors.
A smaller value of creates a narrower margin, which usually increases the number of support vectors and makes the model more sensitive to the training data. A larger value of creates a wider margin, allowing more instances to fall inside the tube and producing a simpler, smoother model.
Theorem. Distance Between Parallel Hyperplanes Let with , and let . Consider the parallel hyperplanes and . Then the distance between them is
Proof. Let , so . Since is normal to both hyperplanes, the shortest path from to is in the direction of the unit normal vector . Thus, a point on can be written as , where is the distance traveled. Since , we have . Substituting gives . Hence, . Since and , we get . Therefore, . Taking the absolute value gives . □
Definition. Hard-Margin Linear SVM Objective The hard-margin linear SVM classifier finds the weight vector and bias term that maximize the margin while ensuring that every training instance is correctly classified and lies outside the margin. This can be written as the constrained optimization problem:
subject to
Here, is the weight vector, is the bias term, is the -th training instance, and is the transformed class label, where for the negative class and for the positive class.
The constraint ensures that all training instances are correctly classified and lie outside the margin. For positive instances, this means . For negative instances, this means .
We minimize instead of directly maximizing the margin because the margin width is . Thus, maximizing the margin is equivalent to minimizing . The expression is preferred because it is differentiable and easier to optimize. The factor is included for convenience, since its derivative is simply .
Definition. Soft-Margin Linear SVM Objective The soft-margin linear SVM introduces a slack variable for each training instance. The value of measures how much the -th instance is allowed to violate the margin. The objective is to keep the margin as wide as possible while allowing some margin violations.
The soft-margin linear SVM objective is written as:
subject to
Here, encourages a wider margin, while penalizes margin violations. The hyperparameter controls the trade-off between these two goals. A larger penalizes margin violations more strongly, leading to fewer violations but possibly a narrower margin. A smaller allows more violations, giving a wider and more regularized margin.
Both hard-margin and soft-margin SVMs are convex quadratic optimization problems with linear constraints, also called quadratic programming (QP) problems.
The Dual Problem
Definition. The Dual Problem Given a constrained optimization problem called the primal problem, it is often possible to express a closely related problem called the dual problem. Under certain conditions, such as those satisfied by SVM optimization, the primal and dual problems have the same solution. This means we can solve either the primal or the dual formulation.
For the linear SVM, the dual objective can be written as:
subject to
Here, are the dual variables, are the transformed labels where , and is the dot product between two training instances.
Once the optimal dual variables are found, we can recover the primal solution using:
and
where is the number of support vectors.
The dual problem is often faster to solve than the primal problem when the number of training instances is smaller than the number of features. More importantly, the dual formulation makes the kernel trick possible, because the data only appears through dot products .
Theorem. Mercer's Theorem Let be a compact subset of , and let be a continuous, symmetric function. If is positive semidefinite, meaning that for any finite set and any real coefficients , we have
then there exists a Hilbert space and a feature map such that
In SVMs, this means that if a function satisfies Mercer's conditions, then it can be used as a valid kernel function. The kernel computes an inner product in some transformed feature space without explicitly computing the transformation . This is what makes the kernel trick possible.
For example, the Gaussian RBF kernel is given by
This kernel corresponds to an inner product in a very high-dimensional, even infinite-dimensional, feature space. Mercer's theorem guarantees that such a feature map exists, even if we do not explicitly write it down.
Note. > Some kernels used in practice, such as the sigmoid kernel, do not always satisfy all of Mercer's conditions, but they may still work well empirically.