Basis Lab

Finding Structure Without Labels

Grouping Around Centres

Last timeDeciding What Close Means

Alternating between assigning each point to its nearest centre and moving each centre to the average of its points is the most used grouping method there is, and its shape assumption is built in.

Two steps, repeated

Pick a number of groups. Put that many centres somewhere. Then repeat two

steps until nothing moves.

Assign every point to whichever centre is nearest. Move every centre to the

average of the points assigned to it.

That is the whole method. It is the most used grouping procedure in existence,

it takes fifteen lines, and both of its serious limitations are consequences

of the first of those two steps.

FIG 1Four rounds on twelve points asked for two groups
steproundwhat the assignment changedwhere the centres moved tototal spread
1startcentres placed at two random points(2.0, 3.0) and (6.0, 5.0)48.6
217 points to the first, 5 to the second(2.6, 2.4) and (6.8, 6.1)21.3
32one point moved across(2.3, 2.2) and (6.5, 5.8)19.1
43one point moved back(2.4, 2.3) and (6.6, 5.9)19.0
54nothing changedunchanged19.0
5 steps
Most of the improvement arrives in the first round, which is typical. The loop stops when an entire round changes no assignment, and that happens quickly, usually within a dozen rounds however large the data is.
FIG 2The whole method
python
def group(points, k, rounds=100):
    centres = [points[i] for i in random_distinct_indices(len(points), k)]
    for _ in range(rounds):
        assigned = [nearest(p, centres) for p in points]        # step one
        moved = []
        for g in range(k):
            held = [p for p, a in zip(points, assigned) if a == g]
            moved.append(average(held) if held else centres[g])  # step two
        if moved == centres: break
        centres = moved
    return centres, assigned
print("assign to the nearest centre, move each centre to its average, repeat until still")
The empty group case has to be handled or the average of nothing is asked for. Leaving the centre where it was is the simplest answer and is what most implementations do; restarting it at the point furthest from any centre is the better one, because an empty group means the number of groups asked for was more than the data supports.

It must stop, and it need not be right

Each step can only make the total spread smaller or leave it alone.

Reassigning a point to a nearer centre reduces the total by definition. Moving

a centre to the average of its points is the position that minimises the

squared distances to them, so that step reduces the total too.

There are finitely many ways to assign points to groups, the total never

increases, and so the loop must stop.

That is a guarantee about termination and nothing more. It does not say the

arrangement reached is the best one, and usually it is not.

FIG 3The quantity both steps reduce
the centre of group g, which is the average of its points
how many points are in group g
squared straight line distance, which is where the previous lesson enters
Squared distance is what makes the average the right centre, and it is also what makes the method sensitive to points far from everything. One extreme point drags its centre towards itself exactly as one extreme value dragged the fitted line in the earlier course, and for the same reason.

Read that quantity carefully and the behaviour of the method follows from it.

It rewards groups that are compact, which means roughly round, and because

squared distances grow fast it also prefers groups of similar size: a large

loose group contributes an enormous amount and the method will split it.

The lesson stops here

4 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Working Without an Answer Key
  2. 02Deciding What Close Meansopening only
  3. 03Grouping Around Centresyou are here
  4. 04How Many Groups Are Thereopening only
  5. 05Groups That Are Not Blobsopening only
  6. 06Describing It With Fewer Numbersopening only
  7. 07Flagging the Odd One Outopening only
  8. 08Judging a Result With No Truth to Checkopening only

Read alongside