K-means clustering
Partition feature vectors around a fixed number of learned centres.
The data contain three visible groups, but their class labels are unknown.
These deliberately imperfect starting centres make the effect of the updates visible.
Each point takes the colour of its nearest centre. Arrows show the move from the initial centres to their assigned sample means.
The displayed result follows twelve assignment/mean updates. Different initial centres can produce a different local solution.
Replace centres with cluster means and repeat.
Nearest-centre assignments and mean updates are computed from the displayed sample coordinates.
Try it on an image
Experiment at pixel level
Cluster colour samples into a small palette, then replace each pixel by its cluster centre.
The engine loads on your first run. Your images stay in this browser.
Scroll over either image to zoom at the pointer. Use the scrollbars to pan both views over the same relative area. Zoom is relative to the input; pixel coordinates belong to each image. Warps can change scene correspondence.
Pixel inspector RGBA · native values · matched scale · 9 × 9 output pixels
Select a pixel
Select a pixel
When to use it
Quantize colours, group visual features or build a visual vocabulary.
How it works
- 01Initialize K centres.
- 02Assign every sample to its nearest centre.
- 03Replace centres with cluster means and repeat.
minimize Σ ||sample − assigned centre||²
What to tune
K selects the number of clusters; attempts repeats initialization; KMEANS_PP_CENTERS improves centre seeding.
Where it breaks down
Results depend on initialization and feature scaling. Spherical Euclidean clusters may not match the underlying structure.
TypeScript API
Open an entry for its exact overloads, parameter descriptions, result ownership and pinned upstream source.
All of these calls execute on the CPU. Native objects need explicit disposal. See matrices and ownership and build compatibility.
Related methods
Support vector machines
Learn a decision boundary that balances class separation against margin violations.
K-nearest-neighbour classification
Predict a sample from the labels or values of nearby training examples.
Random forests
Combine decisions from trees that split feature space into regions.
Principal component analysis
Find orthogonal directions that explain the greatest variance in centred data.