Noah Roberts
Machine Learning from Scratch · Part 6

Margins, Kernels, and Support Vectors: An Intuitive Guide to SVMs

What makes a good decision boundary? SVMs maximize the distance from it, and with the help of kernels they can reshape the coordinate space to handle complex data.

August 25, 2025 · 25 min read

We have seen in previous machine learning models that classification often comes down to finding the right decision boundary. But what makes one boundary better than another? SVMs (Support Vector Machines) answer this question geometrically: an optimal decision boundary is the one that maximizes the distance between the closest points of different classes, allowing us to better distinguish between the two and make better, more confident predictions. This geometric approach makes SVMs an elegant solution to finding an optimal decision boundary and is intuitive to visualize. With the help of kernels, it allows SVMs to handle complex problems that aren’t linearly separable.

Optimal Decision BoundaryOptimal Decision Boundary
Figure 1: All three boundaries separate the data, but only the middle one leaves room on both sides.

Intuition of Margin

When training a classifier, it’s not enough to simply draw a line that separates the two classes. Like we saw in logistic regression, we also care about how confident we are in our predictions.

This distance from an example to the decision boundary is what we call the margin.

Margin Calculation

The margin for training example xix_i for a decision boundary defined by wTxi=0w^Tx_i=0 is:

γi=yi (wTxi)∥w∥\gamma_i = \frac{y_i \,(w^T x_i)}{\|w\|}

Since the weight vector ww is perpendicular to our decision boundary at every possible xx, when we compute the dot product wTxiw^T x_i, we’re really asking:

“How far does xix_i extend in the direction of the normal vector ww?”

The sign of this value tells us which side of the boundary the point lies on. However, this raw value isn’t a true distance; it depends on the scale of ww. If we doubled ww, every dot product would double as well. To remove this dependence, we normalize by the length of ww.

Incorporating Labels

The signed distance alone doesn’t account for which class the point should belong to. To fix this, we multiply by the true label yi∈{+1,−1}y_i \in \{+1, -1\}:

In other words, multiplying by yiy_i “flips” the sign when needed so that the margin is always positive for correct predictions and negative for incorrect ones.

Margin IntuitionMargin Intuition
Figure 2: Multiplying by yiy_i makes a point’s margin positive when it is classified correctly.

Margin of the Dataset

For a dataset with mm training examples, we care about the smallest margin across all points, since the point closest to the boundary is the most vulnerable to misclassification. Formally:

γ=min⁡{γ1,γ2,…,γm}\gamma = \min \{ \gamma_1, \gamma_2, \dots, \gamma_m \}
MarginsMargins
Figure 3: Point A sits close to the boundary (small γA\gamma_A); point B sits far from it (large γB\gamma_B).

Support Vectors

The points from either class that lie closest to the decision boundary are special points called support vectors. These points are critical because:

Support VectorsSupport Vectors
Figure 4: The circled support vectors lie on the margin and alone determine the boundary.

From Margins to Optimization

Now that we know what a margin is, the central goal of an SVM becomes clear: we want to maximize the smallest margin across all training examples. This makes the decision boundary between two classes as wide as possible.

Since the smallest margin is determined by the support vectors, the problem boils down to finding the decision boundary that creates the largest possible gap between the closest points of the two classes.

Formally, this can be written as a constrained optimization problem:

max⁡wγsubject toyi(wTxi)∥w∥≥γ,∀i\underset{w}{\max} \enspace \gamma \enspace \text{subject to} \enspace \frac{y_i (w^Tx_i)}{\|w\|} \geq \gamma, \forall i

Each constraint says: for every data point xix_i, its margin yi(wTxi)∥w∥\frac{y_i(w^Tx_i)}{\|w\|} must be at least γ\gamma.

The Primal Problem

Optimizing this directly is tricky because both γ\gamma and ww appear together in the same fraction. Luckily, SVMs can exploit a helpful property: if we scale ww, the decision boundary doesn’t change; it just rescales the margin. This means that we can fix the scale in a convenient way by requiring the support vectors to satisfy:

yi(wTxi)=1y_i (w^Tx_i) = 1
Decision BoundariesDecision Boundaries
Figure 5: The decision boundary between the margin boundaries wTx=1w^Tx = 1 and wTx=−1w^Tx = -1.

Under this normalization, the margin becomes:

γ=1∥w∥\gamma = \frac{1}{\|w\|}

So maximizing the margin γ\gamma is equivalent to minimizing the size of ww. Our optimization problem becomes:

min⁡w  12∥w∥2subject toyi(wTxi)≥1,∀i\underset{w}{\min} \; \frac{1}{2} {\|w\|}^2 \enspace \text{subject to} \enspace y_i (w^Tx_i) \geq 1, \enspace \forall i

Note: the 12\frac{1}{2} is just a mathematical convenience — it makes the derivatives cleaner when solving and doesn’t affect the solution.

This is the primal problem: it forces every point to be on the correct side of the boundary, at least 1 unit away.

At this stage, it is not obvious how to actually solve this problem. We need a systematic way to make ww as small as possible while still respecting all the margin constraints.

Lagrangian Functional

For constrained optimization, a standard tool is the Lagrangian functional:

L(x,λ)=f(x)+∑iλigi(x)\mathcal{L}(x, \lambda) = f(x) + \sum_i \lambda_i g_i(x)

where:

Intuition

The Lagrangian takes the original objective and adds penalties for violating constraints.

Thus, the optimizer naturally avoids constraint violations. This allows us to rewrite the primal problem:

min⁡x  f(x)subject togi(x)≤0,∀i⟺min⁡x  max⁡λ  L(x,λ)\underset{x}{\min} \; f(x) \enspace \text{subject to} \enspace g_i(x) \leq 0, \enspace \forall i \enspace \Longleftrightarrow \enspace \underset{x}{\min} \; \underset{\lambda}{\max} \; \mathcal{L}(x, \lambda)

Why the maximization over λ\lambda?

Because some multipliers could otherwise be set to zero, “ignoring” violated constraints. Maximizing over λ\lambda finds the harshest penalties that make violations impossible to exploit.

The Dual Problem

So far, the order has been maximize λ\lambda, then minimize xx. That’s the primal problem. But what if we flip the order?

max⁡λ  min⁡x  L(x,λ)\underset{\lambda}{\max} \; \underset{x}{\min} \; \mathcal{L}(x, \lambda)

In this case, we find values of xx that minimize the objective function first, regardless of whether they violate any constraints. Then we maximize over λ\lambda, trying to push the minimized value up as much as possible.

This is the dual problem.

If the optimal values of the primal and dual problems are equal, this is called strong duality. When strong duality holds, the optimal solution is a saddle point of the Lagrangian.

max⁡λ  min⁡x  L(x,λ)≤min⁡x  max⁡λ  L(x,λ)\underset{\lambda}{\max} \; \underset{x}{\min} \; \mathcal{L}(x, \lambda) \leq \underset{x}{\min} \; \underset{\lambda}{\max} \; \mathcal{L}(x, \lambda)
Saddle PointSaddle Point
Figure 6: The optimum (x∗,λ∗)(x^*, \lambda^*) is a saddle point of the Lagrangian.

Finding the Optimal Solution

How do we know when we’ve reached the saddle point? This is where the Karush-Kuhn-Tucker (KKT) conditions come in. They provide the necessary conditions for optimality. The KKT conditions are:

  1. Primal feasibility — all constraints are satisfied:

    gi(x∗)≤0,∀ig_i(x^*) \leq 0, \enspace \forall i
  2. Dual feasibility — all multipliers are nonnegative:

    λi∗≥0,∀i\lambda_i^* \geq 0, \enspace \forall i
  3. Stationarity — the gradient of the Lagrangian vanishes:

    ∇xL(x∗,λ∗)=0\nabla_x \mathcal{L}(x^*, \lambda^*) = 0
  4. Complementary slackness — each constraint is either satisfied or ignored:

    λi∗gi(x∗)=0,∀i\lambda_i^* g_i(x^*) = 0, \enspace \forall i

    (if a constraint is satisfied, its multiplier can be positive; otherwise, the multiplier must be zero)

Together, these conditions certify that (x∗,λ∗)(x^*, \lambda^*) is the optimal solution.

Using the Lagrangian With SVMs

Now that we’ve built some intuition for how the Lagrangian balances the objective function against penalties, we can apply it directly to our SVM optimization problem. The primal SVM objective is:

min⁡w  12∥w∥2subject toyi(wTxi)≥1,∀i\underset{w}{\min} \; \frac{1}{2} {\|w\|}^2 \enspace \text{subject to} \enspace y_i (w^Tx_i) \geq 1, \enspace \forall i

Here:

gi(w)=yi(wTxi)≥1⇒1−yi(wTxi)≤0g_i(w) = y_i (w^Tx_i) \geq 1 \enspace \Rightarrow \enspace 1 - y_i (w^Tx_i) \leq 0

The Lagrangian then becomes:

L(w,λ)=12∥w∥2+∑i=1mλi[1−yi(wTxi)]\mathcal{L}(w, \lambda) = \frac{1}{2} {\|w\|}^2 + \sum_{i=1}^m \lambda_i \left[ 1 - y_i (w^Tx_i) \right]

Step 1. Stationarity

The stationary KKT condition says that at the optimal weights w∗w^*, the gradient of the Lagrangian with respect to ww vanishes. Since our objective is convex, this stationary point is indeed the minimum.

Important: this condition alone does not guarantee that w∗w^* is feasible. Feasibility comes from the other KKT conditions.

Taking the gradient:

∇wL(w,λ)=w+∑i=1mλi(−yixi)=0⇓w∗=∑i=1mλiyixi\begin{align*} \nabla_w \mathcal{L}(w, \lambda) &= w + \sum_{i=1}^m \lambda_i \left(- y_i x_i \right) = 0\\ &\Downarrow\\ w^* &= \sum_{i=1}^m \lambda_i y_i x_i \end{align*}

Notice that only the training points where λi>0\lambda_i > 0 contribute to w∗w^*. These points are our support vectors!

Convex vs Concave
Figure 7: A convex curve bends upward; a concave curve bends downward. Source: StudyWell

Step 2. Substituting Back into Lagrangian

We now plug w∗w^* into the Lagrangian to eliminate ww:

L(w∗,λ)=12∥w∗∥2+∑i=1mλi[1−yi(w∗Txi)]\mathcal{L}(w^*, \lambda) = \frac{1}{2} {\|w^*\|}^2 + \sum_{i=1}^m \lambda_i \left[ 1 - y_i (w^{*T}x_i) \right]

After simplification, this becomes:

∑i=1mλi−12∑i=1m∑j=1mλiλjyiyjxiTxj\sum_{i=1}^m \lambda_i - \frac{1}{2} \sum_{i=1}^m \sum_{j=1}^m \lambda_i \lambda_j y_i y_j x_i^T x_j

∥w∗∥2=w∗Tw∗=(∑iλiyixi)T(∑jλjyjxj)=∑i∑jλiλjyiyjxiTxj\|w^*\|^2 = w^{*T}w^* = \Big(\sum_i \lambda_i y_i x_i\Big)^T \Big(\sum_j \lambda_j y_j x_j\Big) = \sum_i \sum_j \lambda_i \lambda_j y_i y_j x_i^T x_j

Step 3. The Dual Problem

Because we’ve already minimized over ww, what remains is to maximize over λ\lambda. This gives us the dual problem:

max⁡λ∑i=1mλi−12∑i=1m∑j=1mλiλjyiyjxiTxjsubject toλi≥0,∀i\underset{\lambda}{\max} \sum_{i=1}^m \lambda_i - \frac{1}{2} \sum_{i=1}^m \sum_{j=1}^m \lambda_i \lambda_j y_i y_j x_i^T x_j \enspace \text{subject to} \enspace \lambda_i \geq 0, \enspace \forall i

Here, we’ve explicitly enforced the dual feasibility KKT condition. The other KKT conditions, primal feasibility and complementary slackness, are guaranteed to hold at the optimal solution (thanks to convexity and strong duality).

This dual is a quadratic programming (QP) problem. In practice, it can be solved efficiently using the SMO algorithm.

Step 4. Recovering the Weights

Once we solve for the optimal multipliers λ∗\lambda^*, we can recover the optimal weights to get our decision boundary:

w∗=∑i=1mλi∗yixiw^* = \sum_{i=1}^m \lambda_i^* y_i x_i

Soft-Margin SVMs

So far, we’ve assumed the data is perfectly linearly separable. In practice, this almost never happens. Real datasets are noisy, overlapping, or mislabeled. If we insisted on perfect separation, the SVM could overfit badly or even fail to find a solution.

To handle this, SVMs introduce soft margins. Instead of requiring every point to lie strictly outside the margin, we allow some points to be inside or even misclassified, but we penalize these violations.

Slack Variables

We introduce a nonnegative slack variable ξi≥0\xi_i \geq 0 for each training point:

yi(wTxi)≥1−ξiy_i (w^T x_i) \geq 1 - \xi_i

The Soft-Margin Objective

Our optimization problem becomes:

min⁡w,ξ    12∥w∥2+C∑i=1mξisubject toyi(wTxi)≥1−ξi,ξi≥0,∀i\min_{w, \xi} \;\; \tfrac{1}{2}\|w\|^2 + C \sum_{i=1}^m \xi_i \enspace \text{subject to} \enspace y_i (w^T x_i) \geq 1 - \xi_i, \enspace \xi_i \geq 0, \enspace \forall i

Here, C>0C > 0 is a hyperparameter that controls the trade-off:

Soft Margin IntuitionSoft Margin Intuition
Figure 8: A soft margin tolerates a few points (circled) on the wrong side.

Kernel Functions

With soft margins, SVMs can handle noisy or overlapping data: instead of requiring perfect separation, they balance margin width against some classification errors.

But what if the data isn’t just noisy, but truly not separable by a straight line? Even with slack variables, a linear boundary may still perform poorly if the underlying relationship is non-linear.

Feature Map

The solution is to transform the data into a higher-dimensional feature space where a linear separator does exist. For any dataset, there is always some mapping ϕ(x)\phi(x) that makes the data linearly separable in a higher dimension.

ϕ:Rn→Rm,m>n\phi: \mathbb{R}^n \rightarrow \mathbb{R}^m, \quad m > n

For example, we can transform a 1D input xx into 2D: ϕ(x)=(x,  x2)\phi(x) = (x, \; x^2). In this new space, a line might separate the data mapped into two-dimensional space where it was otherwise impossible before.

Feature MappingFeature Mapping
Figure 9: Mapping 1-D points with ϕ(x)=(x,x2)\phi(x) = (x, x^2) makes them separable by a line.

The problem is some feature mappings expand into millions of new features, and others can be infinite-dimensional. Computing all these features is expensive and impractical.

The Kernel “Trick”

When we derived the dual problem for SVMs, notice what appeared:

∑i∑jλiλjyiyjxiTxj\sum_i \sum_j \lambda_i \lambda_j y_i y_j x_i^T x_j

The optimization depends only on the dot product between two data points. So if we transform this data with a feature map, we’d get:

ϕ(xi)Tϕ(xj)\phi(x_i)^T\phi(x_j)

Instead of explicitly computing ϕ(x)\phi(x), we define a kernel function:

K(xi,xj)=ϕ(xi)Tϕ(xj)K(x_i, x_j) = \phi(x_i)^T\phi(x_j)

This lets us work as if we had mapped the data into the higher-dimensional space, without ever computing the coordinates. This is why kernels are sometimes called a trick. It’s a workaround to avoid expensive calculations by performing the dot product in the original low-dimensional space, then using the kernel to find its equivalent feature mapping in the higher-dimensional space.

KernelKernel
Figure 10: A kernel lifts the center cluster above the ring, where a plane separates them.

Polynomial Example

Suppose our inputs are 2-dimensional, and we want to map our features into quadratic space:

a=(a1,a2),b=(b1,b2)a = (a_1, a_2), \quad b = (b_1, b_2)

A quadratic mapping expands each vector into all degree-2 polynomial terms:

ϕ(x)=(x12,  2x1x2,  x22)\phi(x) = (x_1^2, \; \sqrt{2} x_1 x_2, \; x_2^2)

The dot product in this expanded space is:

ϕ(a)Tϕ(b)=a12b12+2a1a2b1b2+a22b22\phi(a)^T\phi(b) = a_1^2 b_1^2 + 2 a_1 a_2 b_1 b_2 + a_2^2 b_2^2

This is much more complicated than our original dot product aTb=a1b1+a2b2a^Tb = a_1b_1 + a_2b_2. And if our original inputs were nn-dimensional, the expanded quadratic space would have n2+n2\tfrac{n^2+n}{2} features. It grows quickly!

If you’ve had lots of experience with factoring polynomials, you may notice that:

(aTb)2=(a1b1+a2b2)2=a12b12+2a1a2b1b2+a22b22(a^Tb)^2 = (a_1 b_1 + a_2 b_2)^2 = a_1^2 b_1^2 + 2 a_1 a_2 b_1 b_2 + a_2^2 b_2^2

which is exactly the same as ϕ(a)Tϕ(b)\phi(a)^T\phi(b)! So instead of explicitly computing the feature mapping, we can define a kernel function that automatically computes the same mapping.

General Polynomial Kernel

More generally, the polynomial kernel is:

K(a,b)=(aTb+c)dK(a, b) = (a^Tb + c)^d

where:

There are many different kinds of kernel functions out there. But not all functions are kernels.

What Defines a Kernel?

Consider a finite set of mm points; let matrix KK be defined as:

K(x1,x1)K(x_1, x_1) K(x1,x2)K(x_1, x_2) …\dots K(x1,xm)K(x_1, x_m)
K(x2,x1)K(x_2, x_1) K(x2,x2)K(x_2, x_2) …\dots K(x2,xm)K(x_2, x_m)
⋮\vdots ⋮\vdots ⋱\ddots ⋮\vdots
K(xm,x1)K(x_m, x_1) K(xm,x2)K(x_m, x_2) …\dots K(xm,xm)K(x_m, x_m)

A function KK is a kernel function if and only if its corresponding kernel matrix is:

This is known as Mercer’s Theorem.

Wrapping Up

Support Vector Machines bring together geometry, optimization, and linear algebra into a beautifully unified model. It’s one of my personal favorites because it’s so intuitive to visualize.

Imagine you’re asked to place the hour hand of an analog clock exactly halfway between the 1 and 5. You would naturally point it at the 3.

Just as the 3 is the most balanced position between 1 and 5, the SVM finds the most balanced boundary between two classes. SVMs remain one of the best examples of how theory and intuition meet to produce a model that is both mathematically elegant and practically powerful.