The RBF-SVM Visualizer logo

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:

  1. Input module — loads CSV/XLSX datasets, lets user pick feature/label columns
  2. Computational engine — implements Gaussian RBF kernel, drives CVXOPT QP solver to recover Lagrange multipliers
  3. Inference engine — evaluates for new points
  4. 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

MetricTargetActualMethod
Decision boundary matches sklearn SVM100% on test setsTODO(aditya): verify against sklearn on UCI datasetsUnit tests: compare predictions on 5 UCI datasets
3D surface renders at >15 FPS>15 FPSTODO(aditya): profile on 200×200 gridManual test on M1/M2 Mac, Intel i7
/ slider latency<100msTODO(aditya): measureTime from slider drag to surface update
Numerical stability (ill-conditioned QP)No solver failuresTODO(aditya): test edge casesSynthetic 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

  1. Add multi-class support — one-vs-rest wrapper with unified decision surface
  2. Implement kernel approximation — Nyström method for
  3. Port GUI to web — Pyodide/WASM or a simple React frontend calling a FastAPI backend
  4. Add more kernels — Polynomial, Sigmoid, Laplacian, with same visualization
  5. Export animations — record / sweeps as MP4/GIF for teaching

Repository

★ rbf-svm-simulator
A from-scratch RBF Support Vector Machine simulator with 2D/3D decision-surface visualization.
0 stars Python MIT
View on GitHub →

Further Reading