Simplified Mathematical Proof of SVM

Nerd Cafe

Part 1: Simplified Mathematical Proof of SVM

Goal of SVM

Imagine you have two kinds of points on a 2D graph:

  • 🟢 Class +1

  • 🔴 Class -1

The goal of SVM is to draw the best straight line that:

  1. Separates the two classes

  2. Is as far away as possible from the closest points

This line is called the decision boundary, and the closest points are called support vectors.

Step 1: Define the Line

We define the separating line (or hyperplane) as:

WTX+b=0W^{T}X+b=0

Where:

  • WTW_{T} is the weight vector (slope of the line)

  • bb is the bias (how far the line is from the origin)

  • XX is the input point (like (2, 3), etc.)

Step 2: Set the Condition for Correct Classification

We want:

  • If y=+1y=+1, the point is above the line

WTX+b1W^{T}X+b\ge 1
  • If y=1y=−1, the point is below the line

WTX+b1W^{T}X+b\le -1

Combine both:

yi(WTX+b)1y_{i}\left( W^{T}X+b \right)\ge 1

Step 3: Maximize the Margin

The margin is the distance from the line to the closest point. SVM wants to maximize this margin. The margin is:

Margin=2WMargin=\frac{2}{\left\| W \right\|}

To maximize this, we minimize:

12W2\frac{1}{2}\left\| W \right\|^{2}

Subject to:

yi(WTxi+b)1y_{i}\left( W^{T}x_{i}+b \right)\ge 1

That’s the core idea of SVM!

Part 2: Simple Numerical Example (Step by Step)

Data

Point
x i ​=(x1 , x2 )
yi

A

(1, 1)

+1

B

(2, 2)

+1

C

(2, 0)

-1

D

(0, 0)

-1

We want to draw a line that separates the +1 and -1 classes.

Step 1: Assume Solution (try values for 𝑤 and 𝑏)

Let’s guess a line:

W=(1,1),b=0W=(1,−1),b=0

So the equation of the line is:

x1x2=0    (or)    x2=x1x_{1}−x_{2}=0 \;\;(or)\;\;x_{2}=x_{1}

Step 2: Plug into the condition yi(WTxi+b)1y_{i}\left( W^{T}x_{i}+b \right)\ge 1

Check all points:

Point
wT.xi ​+b
Result yi ​× (⋅)

A (1,1), +1

1×1−1×1=0

(+1)×0=0 ❌ Not ≥ 1

B (2,2), +1

2−2=0

(+1)×0=0 ❌

C (2,0), -1

2−0=2

(−1)×2=−2 ❌

D (0,0), -1

0−0=0

(−1)×0=0 ❌

So our guess was wrong.

Step 3: Try Better Line

Try:

W=(1,1),b=3W=(1,1),b=−3

So the line is:

x1+x2=3x_{1}+x_{2}=3

Check:

Point
x1 ​ +x2 ​
yi ​ (⋅+b)

A (1,1), +1

1+1=2

1(2−3)=−1 ❌

B (2,2), +1

2+2=0

1(4−3)=1 ✅

C (2,0), -1

2+0=0

−1(2−3)=+1 ✅

D (0,0), -1

0+0=0

−1(0−3)=3 ✅

So only point A fails. Almost correct!

Step-by-Step Python Code: SVM Example

Let’s implement a simple Support Vector Machine (SVM) from scratch using Python and NumPy, step by step, based on the math we discussed.

We’ll:

  1. Create a small dataset.

  2. Visualize it.

  3. Train a simple linear SVM using sklearn.

  4. Show the decision boundary and support vectors.

Step 1: Import Libraries

Step 2: Define a Small Dataset

Step 3: Train the SVM Model

Step 4: Plot the Data, Decision Boundary, and Support Vectors

Output

Explanation

  • svm.support_vectors_: The actual support vectors found by the algorithm.

  • svm.coef_: The learned weight vector WW.

  • svm.intercept_: The learned bias bb.

  • The dashed lines are the margins (distance from the decision boundary).

  • The solid black line is the separating hyperplane.

Step 5: Print the Model Parameters

Add this code after training the SVM:

Output

Last updated