2.4.4SVM, Naive Bayes & Probabilistic Models

Linear, polynomial, and RBF kernels

1,964 words9 min readdifficulty · medium

WHY do we even need kernels?

WHAT the linear SVM does: it finds a separating hyperplane wx+b=0w^\top x + b = 0. Its decision function, after training, can be written using only dot products between the query point xx and the support vectors xix_i:

f(x)=iαiyi(xix)+bf(x) = \sum_{i} \alpha_i y_i \,(x_i^\top x) + b

WHY this matters: the data xx only ever appears inside a dot product xixx_i^\top x. So if the data isn't linearly separable in its raw form, we can map it with ϕ(x)\phi(x) into a richer space and hope it becomes separable there. Then the decision function needs ϕ(xi)ϕ(x)\phi(x_i)^\top\phi(x).

HOW the trick saves us: computing ϕ(x)\phi(x) explicitly can be expensive or infinite-dimensional. But if there's a function KK with

K(x,z)=ϕ(x)ϕ(z)K(x,z) = \phi(x)^\top \phi(z)

we never build ϕ\phi. We just replace every dot product by KK. That replacement is the whole game.


The three workhorse kernels

Here γ\gamma (gamma), cc (coef0), and dd (degree) are hyperparameters. Let us derive each feature map so you see why they are valid.

1. Linear kernel — the trivial map

Derivation. Take ϕ(x)=x\phi(x)=x. Then ϕ(x)ϕ(z)=xz=K(x,z)\phi(x)^\top\phi(z)=x^\top z=K(x,z). Done. It is a kernel with the identity feature map. Use it when dd is large and data is already ~linearly separable (text, sparse features).

2. Polynomial kernel — derive the feature map from scratch

Take the simplest case d=2d=2, c=1c=1, γ=1\gamma=1, in 2D with x=(x1,x2)x=(x_1,x_2), z=(z1,z2)z=(z_1,z_2).

K(x,z)=(xz+1)2=(x1z1+x2z2+1)2K(x,z)=(x^\top z+1)^2=(x_1z_1+x_2z_2+1)^2

Expand it (this is why it's a kernel — we can read off ϕ\phi):

=x12z12+x22z22+2x1x2z1z2+2x1z1+2x2z2+1= x_1^2z_1^2 + x_2^2z_2^2 + 2x_1x_2z_1z_2 + 2x_1z_1 + 2x_2z_2 + 1

Why this step? Group each term as (something in xx)×\times(same something in zz). That grouping is a dot product ϕ(x)ϕ(z)\phi(x)^\top\phi(z) with

ϕ(x)=(x12,  x22,  2x1x2,  2x1,  2x2,  1)\phi(x)=\big(x_1^2,\;x_2^2,\;\sqrt{2}\,x_1x_2,\;\sqrt{2}\,x_1,\;\sqrt{2}\,x_2,\;1\big)

So a degree-2 polynomial kernel silently gives you all squares, cross-terms and linear terms — a 6-dimensional feature space from 2 inputs, computed with one dot product and one square. The cc controls how much weight low-order terms get; larger cc → more emphasis on lower-degree features.

3. RBF kernel — why it's an infinite-dimensional map

Start from xz2=xx2xz+zz\lVert x-z\rVert^2 = x^\top x - 2x^\top z + z^\top z:

K(x,z)=eγxz2=eγx2eγz2normalizers  e2γxzK(x,z)=e^{-\gamma\lVert x-z\rVert^2}=\underbrace{e^{-\gamma\lVert x\rVert^2}e^{-\gamma\lVert z\rVert^2}}_{\text{normalizers}}\; e^{\,2\gamma\, x^\top z}

Now Taylor-expand the last factor:

e2γxz=k=0(2γ)kk!(xz)ke^{2\gamma x^\top z}=\sum_{k=0}^{\infty}\frac{(2\gamma)^k}{k!}(x^\top z)^k

Why this step? Each (xz)k(x^\top z)^k is itself a (homogeneous) polynomial kernel of degree kk, i.e. a dot product in a finite space. A weighted sum of kernels is a kernel, and here the sum runs to infinity → RBF corresponds to an infinite-dimensional feature map containing polynomial features of every degree. That's why it's so expressive.

Figure — Linear, polynomial, and RBF kernels

Worked examples


Common mistakes (steel-manned)


Active recall

Recall Test yourself (hide the answers)
  • Why does SVM only need KK and never ϕ\phi? → The decision function contains data only inside dot products.
  • What property must a Gram matrix have to be a valid kernel? → Symmetric positive semi-definite (Mercer).
  • Why is RBF "infinite dimensional"? → Taylor expansion of e2γxze^{2\gamma x^\top z} is an infinite weighted sum of polynomial kernels.
  • What does γ\gamma control in RBF? → The reach/locality of each point; the bias–variance tradeoff.

Flashcards

What is the kernel trick?
Replacing every dot product xzx^\top z in an algorithm by K(x,z)=ϕ(x)ϕ(z)K(x,z)=\phi(x)^\top\phi(z), gaining a high-dim feature space without computing ϕ\phi.
Formula for the linear kernel?
K(x,z)=xzK(x,z)=x^\top z, with feature map ϕ(x)=x\phi(x)=x.
Formula for the polynomial kernel?
K(x,z)=(γxz+c)dK(x,z)=(\gamma\,x^\top z + c)^d.
Formula for the RBF/Gaussian kernel?
K(x,z)=exp(γxz2)K(x,z)=\exp(-\gamma\lVert x-z\rVert^2).
What condition makes a function a valid kernel?
Its Gram matrix is symmetric positive semi-definite (Mercer's theorem).
Feature map of degree-2 poly kernel (xz+1)2(x^\top z+1)^2 in 2D?
(x12,x22,2x1x2,2x1,2x2,1)(x_1^2,x_2^2,\sqrt2 x_1x_2,\sqrt2 x_1,\sqrt2 x_2,1).
Why is RBF infinite-dimensional?
Because e2γxz=k(2γ)kk!(xz)ke^{2\gamma x^\top z}=\sum_k \frac{(2\gamma)^k}{k!}(x^\top z)^k mixes polynomial kernels of every degree.
Effect of large γ\gamma in RBF?
Very local influence → wiggly boundary → overfitting risk.
Effect of small γ\gamma in RBF?
Smooth, near-linear boundary → underfitting risk.
When prefer the linear kernel?
High-dimensional sparse data (text) where nonlinearity would overfit; also for speed.
Why must you scale features before RBF/poly?
They depend on xz2\lVert x-z\rVert^2/xzx^\top z, which are dominated by large-magnitude features.

Recall Feynman: explain to a 12-year-old

Imagine judging how "alike" two toys are. The linear way: line them up and see how much they point the same direction. The polynomial way: also compare their combinations — colour AND size together, not just each alone. The RBF way: just measure how close together they are — touching toys are "1", far-apart toys are "0". The magic trick is that a computer can measure this alikeness directly, without ever building the giant table of every possible combination. That shortcut lets the SVM draw curvy fences between groups while only doing simple arithmetic.

Connections

  • Support Vector Machines — kernels plug into the dual decision function.
  • The Kernel Trick — the general principle behind all three.
  • Mercer's Theorem — PSD Gram matrix condition.
  • Bias-Variance Tradeoffγ\gamma, dd, and CC control it.
  • Feature Scaling / Standardization — required preprocessing.
  • Overfitting and Regularization — why not to crank complexity.
  • Gaussian / RBF functions — same bell shape as in Naive Bayes densities.

Concept Map

decision uses only

map data with

needs

expensive to build

replaces dot product with

equals

valid if

type

type

type

phi is identity

expands to

form

Linear SVM hyperplane

Dot products of points

Feature map phi

Higher-dim space

Kernel trick

Kernel K x,z

phi x dot phi z

Mercer PSD Gram matrix

Linear kernel

Polynomial kernel

RBF Gaussian kernel

Squares and cross-terms

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho SVM ke paas ek problem hai: data seedhi line se separate nahi ho raha. Solution kya hai? Data ko kisi bade dimension me le jao jahan wo separate ho jaye. Lekin bade dimension me har point ka features compute karna mehenga hai. Yahan kernel trick aata hai: SVM ko sirf dot product chahiye, poora feature vector nahi. Agar humare paas ek function K(x,z)K(x,z) hai jo directly ϕ(x)ϕ(z)\phi(x)^\top\phi(z) de de, to hum bina us bade space me gaye kaam chala lete hain. Yehi jaadu hai.

Teen popular kernels hain. Linear K=xzK=x^\top z — bas raw alignment, jab data pehle se lineary separable ho (text data, sparse features) tab best. Polynomial K=(γxz+c)dK=(\gamma x^\top z+c)^d — ye andar hi andar features ke combinations (cross-terms jaise x1x2x_1x_2) bana deta hai, jisse curvy boundary ban jaati hai. RBF K=eγxz2K=e^{-\gamma\lVert x-z\rVert^2} — ye do points ki nazdeeki naapta hai; same point pe K=1K=1, door jaate hi KK girta hai. Iska ek Taylor expansion nikaalo to pata chalta hai ki ye infinite-dimensional feature space ke barabar hai — isliye sabse powerful hai.

Sabse important knob hai γ\gamma (RBF me) aur degree (poly me). Zyada γ\gamma = har point ka asar sirf apne chhote area tak = bahut wiggly boundary = overfitting. Kam γ\gamma = smooth, almost linear. Iska matlab ye bias–variance ka control hai, jise cross-validation se tune karte ho. Practical tip (80/20 rule): pehle linear try karo, kaam na kare to RBF try karo, aur features ko hamesha standardize karo warna badi magnitude wala feature sab pe haavi ho jaayega.

Test yourself — SVM, Naive Bayes & Probabilistic Models

Connections