A comprehensive Python and NumPy implementation of Support Vector Machines, covering both Hard Margin and Soft Margin formulations using the Sequential Minimal Optimization (SMO) algorithm.
- Introduction
- Features
- Installation
- Quick Start
- Mathematical Background
- Visualization
- File Structure
- Comparison with Sklearn
Support Vector Machine (SVM) is a powerful supervised learning algorithm for classification. The key idea is to find a hyperplane that maximizes the margin between two classes.
- ✅ Pure Python + NumPy (no external ML libraries for training)
- ✅ Educational visualizations with animated GIFs
- ✅ Both Hard and Soft Margin support
- ✅ Multiple kernel functions (Linear, RBF, Polynomial)
- ✅ Comparison benchmarks with sklearn
| Feature | Description |
|---|---|
| Hard Margin SVM | For perfectly linearly separable data |
| Soft Margin SVM | Allows misclassifications via slack variables |
| SMO Optimizer | Sequential Minimal Optimization algorithm |
| Kernels | Linear, RBF (Gaussian), Polynomial |
| Visualizations | Decision boundary plots, training GIFs |
pip install numpy matplotlib scikit-learnfrom svm_hard import HardMarginSVM
import numpy as np
X = np.array([[1, 2], [2, 3], [3, 3], [2, 1], [3, 2]])
y = np.array([1, 1, 1, -1, -1])
model = HardMarginSVM(kernel='linear')
model.fit(X, y)
predictions = model.predict(X)from svm_soft import SoftMarginSVM
model = SoftMarginSVM(C=1.0, kernel='rbf', gamma=0.5)
model.fit(X, y)
predictions = model.predict(X)from svm_hard import HardMarginSVM
from visualizer import SVMVisualizer
model = HardMarginSVM(kernel='linear', record_history=True)
model.fit(X, y)
viz = SVMVisualizer(model, X, y)
viz.plot_decision_boundary(title="Hard Margin SVM", save_path="decision_boundary.png")
viz.create_educational_gif(filename="training_animation.gif")
viz.create_margin_evolution_gif(filename="margin_evolution.gif")Given dataset
Decision rule:
Objective: Maximize margin
Introduces slack variables
- C → ∞: Hard margin (no misclassification allowed)
- C → 0: Wide margin (allows more misclassification)
Using Lagrange multipliers
Constraints:
Sequential Minimal Optimization solves the QP by:
- Select two
$\alpha_i, \alpha_j$ violating KKT conditions - Optimize analytically (closed-form solution)
- Update bias
$b$ - Repeat until convergence
| Kernel | Formula |
|---|---|
| Linear | |
| RBF | $K(x, z) = \exp(-\gamma |
| Polynomial |
The SVMVisualizer class provides three visualization methods:
viz.plot_decision_boundary(title="SVM", save_path="boundary.png")Shows step-by-step optimization with annotations:
viz.create_educational_gif(filename="training.gif", fps=8)Dual-panel animation showing boundary and margin width:
viz.create_margin_evolution_gif(filename="margin.gif")svm/
├── README.md # This file
├── svm_core.py # Core SVM with SMO algorithm
├── svm_hard.py # Hard Margin SVM wrapper
├── svm_soft.py # Soft Margin SVM wrapper
├── visualizer.py # Enhanced visualization utilities
├── compare_bmark.py # Sklearn comparison benchmark
└── outputs/ # Generated plots and GIFs
Run the benchmark:
python compare_bmark.pyThis generates:
outputs/hard_margin_final.png- Final decision boundaryoutputs/hard_margin_training.gif- Training animationoutputs/hard_margin_evolution.gif- Margin evolutionoutputs/soft_margin_final.png- RBF kernel resultoutputs/soft_margin_training.gif- RBF training animation
- Cortes, C., & Vapnik, V. (1995). Support-vector networks. Machine Learning, 20(3), 273-297.
- Platt, J. (1998). Sequential Minimal Optimization: A Fast Algorithm for Training Support Vector Machines. Microsoft Research Technical Report.