Visualizations
079 / 500

079 · Machine learning

Kernel Trick Lift

Rings no line can split are lifted into 3D, where an SMO-trained SVM slices them flat.

Two classes that no straight line can separate (concentric rings, an XOR checker or a parabola) are lifted to a third coordinate by a feature map z = x^2 + y^2, x * y or x^2. In that space a soft-margin support vector machine, trained live with Platt's Sequential Minimal Optimization, finds a flat plane between them: pairs of Lagrange multipliers that violate the KKT conditions are optimized analytically and clipped to the box, the threshold is updated, and the plane tilts into place. Because the kernel is the dot product of the lifted points, the weight vector can be drawn directly as the plane, with its margin planes dashed and the support vectors ringed. Where the plane cuts the lifted surface is a gold curve, and dropped onto the floor it becomes the curved decision boundary: a circle, a pair of hyperbolas or a parabola.

Try it. Drag to orbit. Click the floor or the input-plane inset to add a point (shift-click or right-click for the other class) and the SVM retrains. Pick a dataset and a lift with the chips; mismatched lifts fail visibly. Keys: 1 to 3 datasets, K cycles the lift, Space drops the points back down or lifts them again.

  • Support vector machine
  • Sequential Minimal Optimization
  • Feature maps
  • Marching squares

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a visual explanation of the kernel trick with a real support vector machine, using JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and has a dark blue background.
- Generate about 120 points in the square [-1, 1] x [-1, 1]: one class inside radius 0.4, the other on a ring between 0.65 and 0.95. No straight line separates them.
- Lift every point to 3D with z = x^2 + y^2. Write a tiny 3D projection (yaw, pitch, perspective), draw a grid floor and the points as colored dots with a thin line down to their spot on the floor, and let the mouse drag rotate the view.
- Train a soft-margin SVM with the simplified SMO algorithm on the lifted points using a linear kernel (the dot product of the 3D vectors), C = 5. Run a few SMO steps per frame so you can watch it converge, and keep the weight vector w and bias b explicitly.
- Draw the plane w . p + b = 0 as a translucent grid of quads, and ring the support vectors (points with nonzero alpha).

Once that works, make it beautiful:
- Animate the points rising from the floor to their lifted height, then let the SVM train.
- Use marching squares on f(x, y) = w . (x, y, x^2 + y^2) + b to draw the decision curve on the floor, which turns out to be a circle, and tint the floor by the sign of f.
- Click the floor to add points of either class and retrain.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as an XOR dataset with the lift z = x * y, a 2D inset of the input plane, or showing the margin planes at f = +1 and -1.
PreviousBreadboard CPUA microcoded 8-bit breadboard computer paints cellular automata on a 16x16 LED matrix. NextMaze SolverA maze carves itself, then A* hunts for the way out.

Related visualizations

  • Image TracerAlgorithms Pixel art and a painted landscape traced into crisp, infinitely zoomable curves.
  • Manifold UnrollMachine learning Isomap and LLE unroll a paper Swiss roll, and tear it when k is too large.
  • Neural NetworkMachine learning A tiny neural network learns to classify points, live, in your browser.
  • ReLU Stained GlassMachine learning A ReLU network's exact linear regions, drawn as a stained glass window.
  • Optimizer Topo RaceMachine learning SGD, Adam, Lion and friends race downhill across a vintage topographic map.
  • Gaussian SplattingMachine learning 3D Gaussian splatting in a 2D canvas, trained from scratch by gradient descent.
  • Normalizing FlowMachine learning An invertible network trains live and bends a Gaussian and its grid like taffy.
  • Aperture ProblemIllusions One grating, one true motion, three holes that each report a different direction.
  • Mondrian ForestMachine learning Decision trees cut the plane into a De Stijl painting; a forest blurs it into glazes.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs