Back to data science

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.

Screenshot 2026-04-12 at 11.17.07 PM

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 xx and a landmark \ell. We can measure their similarity using the Gaussian RBF similarity function:

ϕγ(x,)=exp(γx2) \phi_\gamma(x, \ell) = \exp(-\gamma \|x - \ell\|^2)

where:

  • xx is the data point
  • \ell is the landmark
  • x2\|x - \ell\|^2 is the squared distance between them
  • γ\gamma controls how fast similarity decreases as distance increases

The output is always between 00 and 11:

  • close to 11 means the point is very similar or close to the landmark
  • close to 00 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.

ClassTime ComplexityOut-of-Core SupportScaling RequiredKernel Trick
LinearSVCO(m×n)O(m \times n)NoYesNo
SVCO(m2×n)O(m^2 \times n) to O(m3×n)O(m^3 \times n)NoYesYes
SGDClassifierO(m×n)O(m \times n)YesYesNo

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 ϵ\epsilon around the predicted regression line, while limiting the number and size of margin violations.

Formally, SVR seeks a function f(x)f(x) such that most predictions satisfy

yif(xi)ϵ|y_i - f(x_i)| \leq \epsilon

where ϵ\epsilon defines the width of the margin, also called the ϵ\epsilon-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 ϵ\epsilon 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 ϵ\epsilon creates a wider margin, allowing more instances to fall inside the tube and producing a simpler, smoother model.

Theorem. Distance Between Parallel Hyperplanes Let wRn\mathbf{w} \in \mathbb{R}^n with w0\mathbf{w} \neq \mathbf{0}, and let b,c1,c2Rb,c_1,c_2 \in \mathbb{R}. Consider the parallel hyperplanes H1={x:wTx+b=c1}H_1=\{\mathbf{x}:\mathbf{w}^T\mathbf{x}+b=c_1\} and H2={x:wTx+b=c2}H_2=\{\mathbf{x}:\mathbf{w}^T\mathbf{x}+b=c_2\}. Then the distance between them is

d(H1,H2)=c2c1w.d(H_1,H_2)=\frac{|c_2-c_1|}{\|\mathbf{w}\|}.

Proof. Let x1H1\mathbf{x}_1 \in H_1, so wTx1+b=c1\mathbf{w}^T\mathbf{x}_1+b=c_1. Since w\mathbf{w} is normal to both hyperplanes, the shortest path from H1H_1 to H2H_2 is in the direction of the unit normal vector ww\frac{\mathbf{w}}{\|\mathbf{w}\|}. Thus, a point on H2H_2 can be written as x2=x1+αww\mathbf{x}_2=\mathbf{x}_1+\alpha\frac{\mathbf{w}}{\|\mathbf{w}\|}, where α|\alpha| is the distance traveled. Since x2H2\mathbf{x}_2 \in H_2, we have wTx2+b=c2\mathbf{w}^T\mathbf{x}_2+b=c_2. Substituting x2\mathbf{x}_2 gives wT(x1+αww)+b=c2\mathbf{w}^T\left(\mathbf{x}_1+\alpha\frac{\mathbf{w}}{\|\mathbf{w}\|}\right)+b=c_2. Hence, wTx1+b+αwTww=c2\mathbf{w}^T\mathbf{x}_1+b+\alpha\frac{\mathbf{w}^T\mathbf{w}}{\|\mathbf{w}\|}=c_2. Since wTx1+b=c1\mathbf{w}^T\mathbf{x}_1+b=c_1 and wTw=w2\mathbf{w}^T\mathbf{w}=\|\mathbf{w}\|^2, we get c1+αw=c2c_1+\alpha\|\mathbf{w}\|=c_2. Therefore, α=c2c1w\alpha=\frac{c_2-c_1}{\|\mathbf{w}\|}. Taking the absolute value gives d(H1,H2)=α=c2c1wd(H_1,H_2)=|\alpha|=\frac{|c_2-c_1|}{\|\mathbf{w}\|}.

Definition. Hard-Margin Linear SVM Objective The hard-margin linear SVM classifier finds the weight vector w\mathbf{w} and bias term bb 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:

minw,b12wTw\min_{\mathbf{w},b}\frac{1}{2}\mathbf{w}^T\mathbf{w}

subject to

t(i)(wTx(i)+b)1for i=1,2,,m.t^{(i)}(\mathbf{w}^T\mathbf{x}^{(i)}+b)\geq 1 \quad \text{for } i=1,2,\dots,m.

Here, w\mathbf{w} is the weight vector, bb is the bias term, x(i)\mathbf{x}^{(i)} is the ii-th training instance, and t(i)t^{(i)} is the transformed class label, where t(i)=1t^{(i)}=-1 for the negative class and t(i)=1t^{(i)}=1 for the positive class.

The constraint t(i)(wTx(i)+b)1t^{(i)}(\mathbf{w}^T\mathbf{x}^{(i)}+b)\geq 1 ensures that all training instances are correctly classified and lie outside the margin. For positive instances, this means wTx(i)+b1\mathbf{w}^T\mathbf{x}^{(i)}+b\geq 1. For negative instances, this means wTx(i)+b1\mathbf{w}^T\mathbf{x}^{(i)}+b\leq -1.

We minimize 12wTw\frac{1}{2}\mathbf{w}^T\mathbf{w} instead of directly maximizing the margin because the margin width is 2w\frac{2}{\|\mathbf{w}\|}. Thus, maximizing the margin is equivalent to minimizing w\|\mathbf{w}\|. The expression 12wTw=12w2\frac{1}{2}\mathbf{w}^T\mathbf{w}=\frac{1}{2}\|\mathbf{w}\|^2 is preferred because it is differentiable and easier to optimize. The factor 12\frac{1}{2} is included for convenience, since its derivative is simply w\mathbf{w}.

Definition. Soft-Margin Linear SVM Objective The soft-margin linear SVM introduces a slack variable ζ(i)0\zeta^{(i)} \geq 0 for each training instance. The value of ζ(i)\zeta^{(i)} measures how much the ii-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:

minw,b,ζ12wTw+Ci=1mζ(i)\min_{\mathbf{w},b,\zeta}\frac{1}{2}\mathbf{w}^T\mathbf{w}+C\sum_{i=1}^{m}\zeta^{(i)}

subject to

t(i)(wTx(i)+b)1ζ(i)andζ(i)0for i=1,2,,m.t^{(i)}(\mathbf{w}^T\mathbf{x}^{(i)}+b)\geq 1-\zeta^{(i)} \quad \text{and} \quad \zeta^{(i)}\geq 0 \quad \text{for } i=1,2,\dots,m.

Here, 12wTw\frac{1}{2}\mathbf{w}^T\mathbf{w} encourages a wider margin, while i=1mζ(i)\sum_{i=1}^{m}\zeta^{(i)} penalizes margin violations. The hyperparameter CC controls the trade-off between these two goals. A larger CC penalizes margin violations more strongly, leading to fewer violations but possibly a narrower margin. A smaller CC 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:

minα12i=1mj=1mα(i)α(j)t(i)t(j)x(i)Tx(j)i=1mα(i)\min_{\boldsymbol{\alpha}} \frac{1}{2}\sum_{i=1}^{m}\sum_{j=1}^{m}\alpha^{(i)}\alpha^{(j)}t^{(i)}t^{(j)}{\mathbf{x}^{(i)}}^T\mathbf{x}^{(j)}-\sum_{i=1}^{m}\alpha^{(i)}

subject to

α(i)0for all i=1,2,,mandi=1mα(i)t(i)=0.\alpha^{(i)} \geq 0 \quad \text{for all } i=1,2,\dots,m \quad \text{and} \quad \sum_{i=1}^{m}\alpha^{(i)}t^{(i)}=0.

Here, α(i)\alpha^{(i)} are the dual variables, t(i)t^{(i)} are the transformed labels where t(i){1,1}t^{(i)}\in\{-1,1\}, and x(i)Tx(j){\mathbf{x}^{(i)}}^T\mathbf{x}^{(j)} is the dot product between two training instances.

Once the optimal dual variables α^\hat{\boldsymbol{\alpha}} are found, we can recover the primal solution using:

w^=i=1mα^(i)t(i)x(i)\hat{\mathbf{w}}=\sum_{i=1}^{m}\hat{\alpha}^{(i)}t^{(i)}\mathbf{x}^{(i)}

and

b^=1nsi=1α^(i)>0m(t(i)w^Tx(i)),\hat{b}=\frac{1}{n_s}\sum_{\substack{i=1 \\ \hat{\alpha}^{(i)}>0}}^{m}\left(t^{(i)}-\hat{\mathbf{w}}^T\mathbf{x}^{(i)}\right),

where nsn_s 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 x(i)Tx(j){\mathbf{x}^{(i)}}^T\mathbf{x}^{(j)}.

Theorem. Mercer's Theorem Let X\mathcal{X} be a compact subset of Rn\mathbb{R}^n, and let K:X×XRK:\mathcal{X}\times\mathcal{X}\to\mathbb{R} be a continuous, symmetric function. If KK is positive semidefinite, meaning that for any finite set x(1),,x(m)X\mathbf{x}^{(1)},\dots,\mathbf{x}^{(m)}\in\mathcal{X} and any real coefficients c1,,cmc_1,\dots,c_m, we have

i=1mj=1mcicjK(x(i),x(j))0,\sum_{i=1}^{m}\sum_{j=1}^{m}c_i c_j K(\mathbf{x}^{(i)},\mathbf{x}^{(j)})\geq 0,

then there exists a Hilbert space H\mathcal{H} and a feature map ϕ:XH\phi:\mathcal{X}\to\mathcal{H} such that

K(a,b)=ϕ(a),ϕ(b)H.K(\mathbf{a},\mathbf{b})=\langle \phi(\mathbf{a}),\phi(\mathbf{b})\rangle_{\mathcal{H}}.

In SVMs, this means that if a function KK 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 ϕ\phi. This is what makes the kernel trick possible.

For example, the Gaussian RBF kernel is given by

K(a,b)=exp(γab2).K(\mathbf{a},\mathbf{b})=\exp(-\gamma\|\mathbf{a}-\mathbf{b}\|^2).

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 ϕ\phi 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.