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:
Separates the two classes
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:
Where:
is the weight vector (slope of the line)
is the bias (how far the line is from the origin)
is the input point (like (2, 3), etc.)
Step 2: Set the Condition for Correct Classification
We want:
If , the point is above the line
If , the point is below the line
Combine both:
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:
To maximize this, we minimize:
Subject to:
That’s the core idea of SVM!
Part 2: Simple Numerical Example (Step by Step)
Data
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:
So the equation of the line is:
Step 2: Plug into the condition
Check all points:
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:
So the line is:
Check:
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:
Create a small dataset.
Visualize it.
Train a simple linear SVM using
sklearn
.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 .svm.intercept_
: The learned bias .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