Concepts 🚀 Project 🧮 Labs
🧮 Labs / ML Advanced / SVM visualized — hyperplane + margins + support vectors (iris)

SVM visualized — hyperplane + margins + support vectors (iris)

SEE the SVM hyperplane, the margin, and the support vectors. Compare linear vs RBF kernel. See how C controls the margin width.

script.py v3.1
Python output click Run to execute
Click "Run" to execute the code on the left.
The output will appear here.
🧠 How it works

SVM (Support Vector Machine) — VISUALIZED: GOAL: find the hyperplane that MAXIMIZES THE MARGIN between two classes. Not just any separating line — the one with the BIGGEST gap. DECISION BOUNDARY: w·x + b = 0 (solid line). MARGINS: w·x + b = ±1 (dashed lines). The gap between them = the margin. SUPPORT VECTORS: the points ON the margin (circled in green). These are the ONLY points that determine the boundary. Move any other point → boundary stays. That's why SVM is memory-efficient — you only need to remember the support vectors. C PARAMETER (regularization): C=1 (soft margin): wide margin, allows misclassifications. More robust to outliers. C=100 (hard margin): narrow margin, tries to classify everything. Might overfit. KERNEL TRICK (for non-linear data): linear: straight line. Fast, interpretable. Use when data is linearly separable. rbf (Gaussian): curved boundary. Maps data to INFINITE dimensions. Most popular. poly: polynomial boundary. Good for specific cases. GAMMA: controls how far each point's influence reaches. High gamma = each point only affects nearby region (can overfit). Low gamma = each point affects far away (smoother boundary). VERSICOLOR vs VIRGINICA: these species overlap in petal dimensions. Linear SVM with C=1 accepts some errors (soft margin). RBF kernel curves around the overlap — higher accuracy but might overfit.

✏️ Try these changes
  • Change kernel from 'linear' to 'poly' with degree=3
    → polynomial boundary — curved like RBF but different shape
  • Change gamma from 'scale' to 10
    → high gamma → each point has small influence → tighter, more complex boundary
  • Change gamma from 'scale' to 0.01
    → low gamma → each point has large influence → smoother boundary
  • Use all 3 iris classes (remove the mask)
    → SVM does multi-class via one-vs-one — 3 boundaries instead of 1