Visualizations
359 / 500

359 · Machine learning

Gaussian Mixture EM

Expectation maximization fits Gaussians to a painting and turns it into a poster.

A procedurally painted scene is sampled into 16,000 pixels, each a point in five dimensions: red, green, blue and its scaled x and y position. Expectation maximization fits a mixture of full-covariance Gaussians to them: the E step gives every pixel a responsibility under each component using a 5x5 Cholesky factor per Gaussian and log-sum-exp, and the M step re-estimates each component's weight, mean and covariance from those soft assignments, so the log likelihood never goes down. The painting shows each component's position ellipses at one and two sigma, the poster beside it paints every pixel with the responsibility-weighted blend of component colors, and below every pixel is scattered in an opponent color plane with the projected color covariances. Changing K splits the broadest Gaussian along its principal axis or retires the lightest one, so the poster re-quantizes in place.

Try it. Use K minus and K plus (or the arrow keys) to change the number of Gaussians, toggle soft blending and hard assignments with S, drag the position weight to trade color fidelity for spatial coherence, and click the painting to drop a fresh Gaussian there. Hover either image to see the responsibilities at that pixel. N switches between three paintings and R reseeds.

  • Expectation maximization
  • Full-covariance Gaussian mixtures
  • Cholesky and log-sum-exp
  • k-means++ seeding
  • Component splitting by power iteration

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 demo of expectation maximization fitting a Gaussian mixture model to an image with 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 resizes with the window.
- Paint a small procedural picture in code (for example a sunset: a sky gradient, a sun, a dark mountain silhouette and a lake) at about 160 by 100 pixels. Treat each pixel as a point (x, y) in pixel coordinates, scaled to 0 to 1.
- Fit a mixture of 6 two-dimensional Gaussians with full covariance by EM. E step: compute each pixel's responsibility under each component, weight times density, normalized (use log-sum-exp). M step: update each component's weight, mean and 2x2 covariance from the responsibilities. Add a small value to the diagonal so nothing collapses.
- Run one EM iteration every quarter second and draw each component as an ellipse at one and two standard deviations over the picture.

Once that works, make it beautiful:
- Extend the points to five dimensions, (r, g, b, x, y), with a weight on position. Use a 5x5 Cholesky factor per component for the density and log determinant.
- Show a poster beside the painting: color each pixel with the responsibility-weighted blend of the component mean colors, and offer a toggle for hard argmax colors.
- Plot the log likelihood per pixel after each iteration so I can see it never decreases, and let me change the number of components with the arrow keys.

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 splitting the broadest component when K grows, plotting pixels in color space with the color covariances, or loading a photo of my own.
PreviousOptical BenchA blueprint ray tracer: prisms, lenses, mirrors and a fiber with real glass dispersion. NextRubens' TubeGas flames on a brass pipe rise and dip with the standing sound wave inside.

Related visualizations

  • Gaussian Process InkMachine learning Exact GP posteriors painted as watercolor bands with inked function samples.
  • Gaussian SplattingMachine learning 3D Gaussian splatting in a 2D canvas, trained from scratch by gradient descent.
  • CMA-ESMachine learning An evolution strategy's search ellipse stretches, turns and collapses onto the optimum.
  • Normalizing FlowMachine learning An invertible network trains live and bends a Gaussian and its grid like taffy.
  • Diffusion SketchpadMachine learning A diffusion model trains live and condenses pure noise into whatever shape you draw.
  • AfterimageIllusions Stare at a surreal landscape, then see full color in a picture that is pure gray.
  • Pigment MixingSimulation Blue and yellow paint make green here: spectral Kubelka-Munk mixing beside naive RGB.
  • Mondrian ForestMachine learning Decision trees cut the plane into a De Stijl painting; a forest blurs it into glazes.
  • Seamless CloneAlgorithms Poisson image editing: a pasted moon takes on the light of a sunset sky.

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

← More from Emergent Mind Labs