Gradient

Courses

∇\nabla∇The maths behind AI0/72
π\piπMaths0/84
⋆\star⋆AI for everyone0/10
Γ\GammaΓGame theory0/49
py\texttt{py}pyPython for AI0/15
y^\hat{y}y^​Build your own AI0/15
All courses

Tuition

My tuitionOlympiad trackCompetitions

Practice

ProjectsAsk a questionTrain on a GPUExplorationsMaths StoriesUnsolved problemsPlaygroundScience labsMock testsOlympiad

Reference

GlossaryBlogDiscoveries
Blog › Explore in 3D

25 November 2026 · 3 min readInteractive 3D

Groups of colourful dots, an artwork about pattern recognition
Photo by Google DeepMind on Unsplash

What is k-means clustering? Finding groups

No labels, no teacher: just points and a simple two-step loop. Step through k-means one round at a time.

Suppose a shop has thousands of customers and wants to know what kinds there are. Nobody has labelled them. K-means is a simple method that finds groups anyway, by repeating two steps until nothing changes.

Press “One round” again and again. Each colour is one group.

The two steps

  1. Assign: every point joins the centre it is closest to.
  2. Update: every centre moves to the average position of its points.
  3. Repeat until the centres stop moving.

Each round can only make the groups tighter, so the loop always settles. Usually it takes just a handful of rounds.

What can go wrong

  • You must choose k, the number of groups, in advance.
  • A bad starting position can split one real group in two and merge two others. Press “New starting centres” to see it happen.
  • It assumes groups are round blobs; long or curved groups confuse it.

This kind of learning, with no right answers given, is called unsupervised learning.

A worked example on a line

Group the numbers 1, 2, 3, 10, 11, 12 into k = 2 clusters, starting with centres at 1 and 2.

  1. Assign: 1 goes to centre 1; everything else is nearer 2
  2. Update: centres become 1 and the mean of 2, 3, 10, 11, 12 = 7.6
  3. Assign again: 1, 2, 3 nearer 1; 10, 11, 12 nearer 7.6
  4. Update: centres become 2 and 11; nothing changes after this, so stop

The algorithm found the two obvious groups in three rounds. In 3D the same two steps, assign then move, run on points in space.

Choosing k

K-means needs you to say how many groups to find. A common trick, the elbow method, runs it for k = 1, 2, 3 … and plots the total distance from points to their centres. The distance always falls as k grows, but there is often a bend (an elbow) after which extra clusters help little. That bend is a good choice.

Limits

  • It assumes roughly round clusters of similar size.
  • Different starting centres can give different answers, so it is run several times.
  • Features on very different scales (rupees and centimetres) must be rescaled first.

K-means is unsupervised: it finds groups without being told any right answers.

Better starting points

Random starting centres can land badly, for example two in the same group. K-means++ picks the first centre at random and each next one far from those already chosen. It usually gives better clusters in fewer rounds and is the default in most libraries.

Glossary

  • Centroid: the average position of a cluster.
  • Inertia: total squared distance from points to their centres.
  • Elbow method: choosing k where gains level off.
  • Feature scaling: putting features on similar ranges.

Try these yourself

  1. Q1. Points 1, 2, 9, 10 with centres 1 and 10: final clusters? {1, 2} and {9, 10}, centres 1.5 and 9.5
  2. Q2. Mean of (1, 1), (3, 1), (2, 4)? (2, 2)
  3. Q3. Point (4, 4), centres (0, 0) and (5, 5): which is nearer? (5, 5)
  4. Q4. Does k-means need labelled data? No

Where it came from

The idea was proposed by Stuart Lloyd at Bell Labs in 1957 for compressing signals and published in 1982. The name k-means came from James MacQueen in 1967.

In short

Assign points to the nearest centre, move centres to the average, repeat. Choose k with the elbow method and rescale features first.

Keep going

  • Lesson: clustering
  • Glossary: clustering
  • Clustering in real life
  • What are word embeddings? Words as points
  • What is cosine similarity? How alike two things are

Read and listen in

Indian languages

World languages

Questions people ask

How does k-means clustering work?

It repeats two steps: assign each point to its nearest centre, then move each centre to the average of its points, until nothing changes.

How do you choose the number of clusters?

Try several values of k and look for the elbow where adding more clusters stops reducing the distance much, or use knowledge of the problem.

Is k-means supervised or unsupervised?

Unsupervised. It needs no labels; it finds structure in the data by itself.

More articles

  • Where is clustering used in real life?
  • What are word embeddings? Words as points
  • What is cosine similarity? How alike two things are