
A straight line rarely divides the world. Most interesting boundaries — a tumor from healthy tissue, spam from ham, one speaker's voice from another — are curved, nested, entangled. The support vector machine's answer is not to draw a better line, but to lift the data into a space where a line *will* do, and then draw it there. This project is a from-scratch simulator that makes that lift visible.
Problem
Support Vector Machines are a cornerstone of classical ML, but the leap from linear SVMs to kernel SVMs is where most tutorials lose practitioners. The “kernel trick” is typically presented as math without intuition: without ever showing . Students and engineers memorize the RBF formula but cannot visualize what and actually do to the decision boundary. This simulator closes that gap by rendering the decision surface in 2D and 3D, live.
Constraints
- From-scratch implementation: No sklearn, no libsvm — the QP solver and kernel must be visible in code
- Interactive exploration: and must be adjustable in real time with immediate visual feedback
- Dual output modes: Static 2D contours (for papers/notes) and interactive 3D surface (for exploration)
- Pedagogical clarity: Code structure must mirror the mathematical derivation (dual problem → support vectors → decision function)
- Dependencies: Minimal — NumPy, CVXOPT (QP), Matplotlib, Tkinter (stdlib)
Architecture & Design Decisions
Four-Stage Pipeline
The simulator is organized as four cooperating stages, each deliberately separable:
- Input module — loads CSV/XLSX datasets, lets user pick feature/label columns
- Computational engine — implements Gaussian RBF kernel, drives CVXOPT QP solver to recover Lagrange multipliers
- Inference engine — evaluates for new points
- Rendering module — Matplotlib produces static 2D contours and interactive 3D decision surface
Decision: Separate computation from rendering. The engine returns the decision function as a callable; the renderer maps it over a grid. This makes the math testable independently of visualization.
Kernel Implementation
Decision: Implement directly with NumPy broadcasting. No approximation, no caching — datasets are small enough () for exact kernel matrix.
Tradeoff: Exact kernel matrix is memory. For pedagogical use this is fine; production SVMs use decomposition methods (SMO) or kernel approximation (Nyström, random Fourier features).
QP Solver (CVXOPT)
Decision: Use CVXOPT’s interior-point QP solver for the dual problem: subject to and .
Why: CVXOPT is reliable, well-tested, and the dual formulation makes support vectors explicit (). Alternative: implement SMO from scratch (pedagogically valuable but diverts focus from kernel visualization).
Rendering: 2D + 3D
Decision: Matplotlib for both. 2D: contourf for class regions, contour for margin lines. 3D: plot_surface with as Z-axis, colored by class probability.
Tradeoff: Matplotlib 3D is not GPU-accelerated; large grids () lag. For a teaching tool, 200×200 grid is responsive enough.
GUI (Tkinter)
Decision: Tkinter (stdlib) for hyperparameter sliders (, ) and live classification of custom points. No external GUI deps.
Evaluation
| Metric | Target | Actual | Method |
|---|---|---|---|
| Decision boundary matches sklearn SVM | 100% on test sets | TODO(aditya): verify against sklearn on UCI datasets | Unit tests: compare predictions on 5 UCI datasets |
| 3D surface renders at >15 FPS | >15 FPS | TODO(aditya): profile on 200×200 grid | Manual test on M1/M2 Mac, Intel i7 |
| / slider latency | <100ms | TODO(aditya): measure | Time from slider drag to surface update |
| Numerical stability (ill-conditioned QP) | No solver failures | TODO(aditya): test edge cases | Synthetic near-degenerate datasets |
Limitations
- Scalability: Kernel matrix is ; becomes slow and memory-heavy. Not suitable for production-scale data.
- Binary classification only: No native multi-class (one-vs-one / one-vs-rest) — would need wrapper.
- No kernel approximation: Nyström / random Fourier features not implemented.
- Tkinter GUI is basic: No theming, limited layout control, no dark mode.
- Single-threaded: QP solve blocks UI; no progress indicator for large solves.
What’s Next
- Add multi-class support — one-vs-rest wrapper with unified decision surface
- Implement kernel approximation — Nyström method for
- Port GUI to web — Pyodide/WASM or a simple React frontend calling a FastAPI backend
- Add more kernels — Polynomial, Sigmoid, Laplacian, with same visualization
- Export animations — record / sweeps as MP4/GIF for teaching
Repository
Further Reading
- On the changing work of software engineers — where ML tooling fits into practice.
- The Rasch Model — another case where a clever transformation turns an intractable measurement into a linear one.
- Burges (1998), “A Tutorial on Support Vector Machines for Pattern Recognition” — the canonical introduction.
- The Kernel Trick, explained (distill.pub) — intuitive treatments of high-dimensional mappings.