Lucas Fonseca
  • Research
  • Projects & students
  • Resources
  • About & contact
Skip to content

DSML / 02 / CLUSTERING

Where do the groups come from?

K-means alternates between assigning points to a nearby centre and moving that centre. Slow it down to see how those two simple steps create a partition.

All DSML activities · Regression · Clustering · Classification

Start here: Points are observations described by two or three features. Numbered squares are cluster centres. Press Assign points, then Move centres. Use the rotation slider to inspect the 3D view; rotation changes the view, not the distances used by the algorithm.

Loading the interactive activity… JavaScript is needed for the controls; you can still read the explanations below.

Cluster centres and membership counts

K and starting-centre changes keep the same observations. Reset restores the starting centres; in the worked example it restores the three specified centres.

Explore the numerical data

Look before you press Play

  1. Assign the points. Predict where each centre will move, then choose Move centres. Why does a centre sometimes move a long way?
  2. Let the algorithm converge. Choose New starting centres on the same data and compare the result. Does it always find the same grouping?
  3. Change K from 2 to 5. Notice that k-means creates the requested number of centres even when that choice does not match the apparent groups. A centre can also end up with no assigned points.

A small example to work through

Choose Worked example: 24 points. It uses the observations from my classroom k-means example, with starting centres (1, 2), (2, 3) and (9, 3). Before assigning points, predict which centre is closest to (10, 2). Then inspect the data table.

Reveal the first assignment

The squared distances from (10, 2) to the three starting centres are 81, 65 and 2. The point is therefore assigned to cluster 3 in this website’s numbering. Python labels often start at 0; cluster numbers are identifiers, not ordered categories.

What counts as convergence?

A complete iteration assigns each point to its nearest centre, then moves each centre to the mean of its assigned points. This activity stops when the next assignment step changes no memberships. An empty cluster keeps its previous centre.

The displayed within-cluster sum of squared distances measures how far observations lie from their assigned centres. Each assignment or mean update should not increase that objective. Reaching a stable result does not guarantee the best possible solution; different initial centres can lead to different results.

K-means uses Euclidean distance, so feature units and scaling matter. A 3D projection can make distant points look close. The numerical tables show all coordinates and memberships without relying on colour or a particular viewpoint.

Connect it to movement

Clustering could help explore recurring patterns in movement features before labels are available. A cluster is a mathematical grouping; it does not automatically represent a movement intention, diagnosis or clinically meaningful category.

Next: compare classifiers Back to Data Science and Machine Learning

Adapted from my k-means-3d visualisation and quiz/kmeans.py example. The website separates assignments from centre updates and provides keyboard-controlled rotation and numerical tables.

Lucas Fonseca
Human movement for rehabilitation, health and independence.

 

CHART · Email
University of Nottingham