Machine Learning

SVM Margins and Kernels

SVMs choose a separating boundary by maximizing margin; support vectors are the active constraints, and kernels replace dot products with feature-space similarity.

status: reviewimportance: importantdifficulty 4/5math: graduateread: 20mlive demo

Concept Structure

SVM Margins and Kernels

01Intuition

Start with the picture, metaphor, or geometric mechanism.

02Math

Make the objects explicit and connect them with notation.

03Code

Mirror the equations with runnable implementation details.

04Interactive Demo

Manipulate the mechanism and watch the idea respond.

3prerequisites
2next concepts
3related links

Learner Contract

What this page should let you do.

You are here becauseSVMs choose a separating boundary by maximizing margin; support vectors are the active constraints, and kernels replace dot products with feature-space similarity.

This Machine Learning concept is the current idea: keep the same invariant visible across Intuition, Math, Code, Interactive Demo.

By the end4/4 sections ready | runnable code expected | live demo

Explain the mechanism, trace the main notation, and test one prediction in the live demo.

Do this firstIntuition

Read the intuition before the notation; the math should name a mechanism you already felt.

Test the linkManipulate one control and predict the visible change.Then continue to Model Selection and Hyperparameter Search

Claim/source review status

Substantive review recorded

1/1 claims have bounded review metadata; still check caveats and source scope.Metadata-derived; review may be AI-assisted. Not a human certification.
Claims1/1 reviewed
Sources5 cited
Codeattached
Demolive
Reviewed2026-07-02
Updatedpage 2026-07-02

Learning item flow

4/4 sections readyAsk about thisResearch room
ConceptSVM Margins and KernelsMachine Learning
5 sources attachedLocal snapshot ready
concept:machine-learning/svm-margins-kernels
01

01

Intuition

Build the mental picture first so the rest of the page has something to attach to.

Section prompt

A linear classifier draws a boundary. An SVM asks a sharper question:

Among all boundaries that separate the classes, which one leaves the widest empty road between them?

That empty road is the margin. A wide margin is a geometric preference for a boundary that is not nervously threading between nearby points. The points touching the road edges are the important ones: move them and the road changes; move a far-away correctly classified point and nothing happens.

Those edge-touching points are the support vectors.

This is the KKT story in classifier form. Each training point imposes a constraint:

yi(wxi+b)1.y_i(w^\top x_i+b)\ge 1.

If the constraint is loose, its multiplier is zero. If it is tight, its multiplier can be positive. So the learned normal vector is built from active training points:

w=iαiyixi.w=\sum_i \alpha_i y_i x_i.

Kernels preserve that story but change the notion of similarity. If every formula only needs dot products xixjx_i^\top x_j, we can replace those dot products with a kernel K(xi,xj)K(x_i,x_j) that behaves like an inner product in some feature space. The decision rule becomes a weighted similarity vote from support vectors:

f(x)=iαiyiK(xi,x)+b.f(x)=\sum_i \alpha_i y_i K(x_i,x)+b.

The dangerous misconception is that kernels are magic nonlinear boundaries. More precisely: the model is still a linear separator in a feature space; the kernel lets us compute feature-space inner products without explicitly writing the feature map.

02

02

Math

Translate the story into symbols, assumptions, and a derivation you can inspect.

Section prompt

Let the training set be (xi,yi)i=1n(x_i,y_i)_{i=1}^n, where xiRdx_i\in\mathbb R^d and yi{1,+1}y_i\in\{-1,+1\}. A linear classifier uses

f(x)=wx+b,y^=sign(f(x)).f(x)=w^\top x+b, \qquad \hat y=\operatorname{sign}(f(x)).

The signed functional margin of point ii is

mi=yi(wxi+b).m_i = y_i(w^\top x_i+b).

The geometric distance from xix_i to the boundary is the signed margin divided by the normal length:

γi=yi(wxi+b)w2.\gamma_i=\frac{y_i(w^\top x_i+b)}{\|w\|_2}.

Because multiplying (w,b)(w,b) by a positive constant leaves the sign of f(x)f(x) unchanged, the hard-margin SVM fixes the functional margin scale at mi1m_i\ge 1 and then maximizes geometric margin by minimizing w2\|w\|_2:

minw,b12w22subject toyi(wxi+b)1, i=1,,n.\min_{w,b}\frac12\|w\|_2^2 \quad \text{subject to}\quad y_i(w^\top x_i+b)\ge 1,\ i=1,\ldots,n.

The distance from the decision boundary to each margin rail is

1w2,\frac{1}{\|w\|_2},

so the full margin width between the two rails is

2w2.\frac{2}{\|w\|_2}.

Write the constraints as 1yi(wxi+b)01-y_i(w^\top x_i+b)\le 0 and attach multipliers αi0\alpha_i\ge 0. The hard-margin dual is

maxαiαi12ijαiαjyiyjxixj\max_\alpha \sum_i \alpha_i -\frac12\sum_i\sum_j \alpha_i\alpha_j y_i y_j x_i^\top x_j

subject to

αi0,iαiyi=0.\alpha_i\ge 0, \qquad \sum_i \alpha_i y_i=0.

At the optimum,

w=iαiyixi.w^\star=\sum_i \alpha_i^\star y_i x_i.

Complementary slackness gives the support-vector rule:

αi(1yi((w)xi+b))=0.\alpha_i^\star\left(1-y_i((w^\star)^\top x_i+b^\star)\right)=0.

If yif(xi)>1y_i f(x_i)>1, the point is outside the margin and αi=0\alpha_i=0. If αi>0\alpha_i>0, the point is active: it lies on the margin in the hard-margin case, or it is on/inside the soft-margin tube in the soft-margin case.

For nonseparable data, introduce slack variables ξi0\xi_i\ge 0:

minw,b,ξ12w22+Ciξi\min_{w,b,\xi} \frac12\|w\|_2^2+C\sum_i \xi_i

subject to

yi(wxi+b)1ξi,ξi0.y_i(w^\top x_i+b)\ge 1-\xi_i, \qquad \xi_i\ge 0.

Equivalently, the slack term is the hinge loss:

max(0,1yif(xi)).\max(0,1-y_i f(x_i)).

The parameter CC controls how much the optimizer pays for margin violations relative to a wider margin.

Now the kernel step. If the dual and prediction only need inner products, replace

xixjx_i^\top x_j

with a valid kernel

K(xi,xj)=ϕ(xi),ϕ(xj).K(x_i,x_j)=\langle \phi(x_i),\phi(x_j)\rangle.

The dual becomes

maxαiαi12ijαiαjyiyjK(xi,xj),\max_\alpha \sum_i \alpha_i -\frac12\sum_i\sum_j \alpha_i\alpha_j y_i y_j K(x_i,x_j),

and prediction becomes

f(x)=iαiyiK(xi,x)+b.f(x)=\sum_i \alpha_i y_iK(x_i,x)+b.

For example, the RBF kernel

Kγ(x,z)=exp(γxz22)K_\gamma(x,z)=\exp(-\gamma\|x-z\|_2^2)

makes nearby points highly similar and far-away points weakly similar. It can produce nonlinear boundaries in the original input plane because it is linear in a different, implicit feature space.

03

03

Code

Keep the implementation aligned with the notation so the algorithm is legible.

Section prompt
import numpy as np

X = np.array([
    [1.0, 0.0], [0.0, 1.0], [2.0, 1.4],
    [-1.0, 0.0], [0.0, -1.0], [-1.6, -1.4],
])
y = np.array([1, 1, 1, -1, -1, -1], dtype=float)

# Hand-solvable hard-margin solution for this symmetric toy.
alpha = np.array([0.5, 0.5, 0.0, 0.5, 0.5, 0.0])
b = 0.0
w = (alpha * y) @ X

scores = X @ w + b
functional_margins = y * scores
support = np.flatnonzero(alpha > 1e-9)

print("w:", w)
print("margin rail distance:", 1 / np.linalg.norm(w))
print("full margin width:", 2 / np.linalg.norm(w))
print("support vector indices:", support.tolist())
print("functional margins:", np.round(functional_margins, 3))

probe = np.array([0.35, 0.75])

def rbf(a, z, gamma=1.4):
    return np.exp(-gamma * np.sum((a - z) ** 2, axis=1))

linear_score = np.sum(alpha * y * (X @ probe)) + b
rbf_score = np.sum(alpha * y * rbf(X, probe)) + b

print("linear support-vector score:", round(linear_score, 3))
print("RBF support-vector score:", round(rbf_score, 3))

The first score matches wx+bw^\top x+b. The RBF score uses the same support-vector expansion shape but a different similarity function. In a real kernel SVM, the αi\alpha_i values are solved using that kernel's Gram matrix; this toy isolates the prediction rule.

04

04

Interactive Demo

Use direct manipulation to connect the explanation to a moving system.

Section prompt

The lab starts with a hand-solvable hard-margin classifier. Predict which points control the score before revealing the coefficients. Then switch between a linear kernel and an RBF kernel to see what changes: not the support-vector expansion, but the similarity values inside it.

Live Concept Demo

Explore SVM Margins and Kernels

The stage is code-native and interactive. Use it to test the explanation against the mechanism.

difficulty 4/5graduatecode-aligned
Demo Prediction Checkpoint

Manipulate one control and predict the visible change.

Commit to what SVM Margins and Kernels should make visible before reading the result.

After The First Pass

Turn the concept into an inspected object.

Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.

Mechanism Storyboard

See the idea move before the page explains it

SVMs choose a separating boundary by maximizing margin; support vectors are the active constraints, and kernels replace dot products with feature-space similarity.

Prediction open01 / Intuition
Prediction lens

Start with the picture, metaphor, or geometric mechanism.

Commit first

Before reading further, choose the kind of change SVM Margins and Kernels should make visible.

Visual Inquiry

Make the image answer a mathematical question

SVMs choose a separating boundary by maximizing margin; support vectors are the active constraints, and kernels replace dot products with feature-space similarity.

4/4 stages readyLive demo connected
Prediction

Which visible object should carry the first intuition?

Commit first

Pick the cue that should make SVM Margins and Kernels easier to reason about before the page gives the answer.

Source Grounding

Canonical references for the mechanism on this page.

course-notes · 2022CS229 Main Notes: Support Vector Machines and Kernel MethodsStanford CS229

Supports functional/geometric margins, the hard-margin primal, dual form, KKT complementarity, support vectors, the kernel trick, Gaussian kernels, and soft-margin/slack formulation.

Open source
book · 2009The Elements of Statistical Learning, Chapter 12Hastie, Tibshirani, and Friedman

Supports support-vector classifier geometry, soft-margin cost parameter behavior, hinge-loss framing, support-vector coefficient categories, and radial-kernel SVM behavior.

Open source
book · 2023An Introduction to Statistical Learning, Chapter 9James, Witten, Hastie, Tibshirani, and Taylor

Supports maximal-margin classifiers, support-vector classifiers, slack variables, cost parameter intuition, support vectors, and polynomial/radial kernel SVMs.

Open source
book · 2020Mathematics for Machine Learning, Chapter 12Deisenroth, Faisal, and Ong

Supports margin derivation, primal and dual soft-margin SVM, hinge-loss equivalence, kernel trick, Gram/kernel matrices, and the caveat that kernel SVMs still solve linear hyperplanes in feature space.

Open source
book · 2004Convex Optimization, Chapter 5Boyd and Vandenberghe

Background source for Lagrange duality, duality gap, and KKT optimality conditions used to interpret active SVM margin constraints.

Open source

Claim Review

SVMs choose a separating boundary by maximizing margin; support vectors are the active constraints, and kernels replace dot products with feature-space similarity.

Status1 substantive review recorded

Claims without a substantive review badge still need exact source-support review.

Sources5 references

cs229-2022-svm-margins-kernels, hastie-2009-esl-svm-kernels, james-2023-islr-svm, deisenroth-2020-mml-svm, boyd-2004-duality-kkt-background

Local checks4 local checks

Use equations, runnable code, and demos to check whether the source support is operational.

Substantively reviewedA hard-margin SVM maximizes geometric margin by minimizing ||w|| under unit functional-margin constraints; KKT complementarity makes only active margin constraints have nonzero dual coefficients, and kernel SVM prediction replaces dot products with valid kernel similarities in the support-vector expansion.Claim metadata: source checked

The sources support the margin objective, hard/soft-margin constraints, dual coefficient expansion, complementarity-based support-vector interpretation, hinge-loss pressure, and kernel substitution.

Sources: CS229 Main Notes: Support Vector Machines and Kernel Methods, The Elements of Statistical Learning, Chapter 12, An Introduction to Statistical Learning, Chapter 9, Mathematics for Machine Learning, Chapter 12, Convex Optimization, Chapter 5The interactive lab uses an exactly hand-solvable hard-margin toy and a separate kernel-score probe. It does not implement a general quadratic-programming solver, SMO, statistical generalization bounds, or a trained RBF-kernel SVM.A bounded review summary is present; still check caveats and exact reference scope.

Checked CS229, ESL, ISLR, MML, and Boyd for margin geometry, hard/soft-margin SVMs, support-vector KKT complementarity, hinge/slack/C behavior, kernel substitution, and feature-space hyperplane caveats. GPT Pro critique remains pending.

Reviewer: codex-local-source-review; reviewed 2026-07-02

Practice Loop

Try the idea before it explains itself

SVMs choose a separating boundary by maximizing margin; support vectors are the active constraints, and kernels replace dot products with feature-space similarity.

Readiness0/3 checks ready
Predict

Before touching the demo, predict one visible change that should happen in SVM Margins and Kernels.

Hint 1

Reveal when your model needs a nudge.

Hint 2

Reveal when your model needs a nudge.

Hint 3

Reveal when your model needs a nudge.

Grounded research drawerClose
ConceptSVM Margins and KernelsMachine Learning
Runnable code comparisonSVM Margins and Kernels runnable code 1scores = X @ w + bPrediction before revealSVM Margins and Kernels interactive demoManipulate one control and predict the visible change.
Grounded room questionWhat is the smallest example that makes SVM Margins and Kernels click without losing the math?Local snapshot ready

Research Room

Attach the question to a claim, equation, code, or demo

Pick the concept, equation, source, runnable code, claim, misconception, or demo state before asking for help. The handoff keeps that page item in context.
Next local actionNo local draft saved yet

Open the draft below to save one note and next action in this browser.

conceptMachine Learning

SVM Margins and Kernels

Attached question

What is the smallest example that makes SVM Margins and Kernels click without losing the math?

Local action draftNo local draft saved yetExpand only when ready to capture one local next action
Local action draft

This draft stays in this browser, attached to the selected learning item.

No local draft saved.
Evidence to inspect
  • References to inspect: attached references on this page.
  • Definition, prerequisite, and contrast concept links
  • The equation or runnable code that makes the concept operational
  • One demo state that shows the invariant instead of a slogan
What would resolve this
  • The learner can state the mechanism in their own words
  • The learner can name the prerequisite that would repair confusion
  • The learner can predict how the mechanism changes under one perturbation
Grounded AI handoff

I am working in Continuous Function's research reading room. Object: concept - SVM Margins and Kernels Selected item key: recorded for copy. Context: Machine Learning Page anchor: recorded for copy. Open question: What is the smallest example that makes SVM Margins and Kernels click without losing the math? Evidence to inspect: - References to inspect: attached references on this page. - Definition, prerequisite, and contrast concept links - The equation or runnable code that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.

View it in context
concept/concept-notebook/machine-learning/svm-margins-kernels concept:machine-learning/svm-margins-kernels