Skip to content

k-means

learning

Level 2 of 3

Also called Lloyd's algorithm and k-means clustering.

test yourself

Six questions, four to pick from and two for paper. A point such as 7 6 stands 7 across and 6 up. In each figure, the crosses are the starting centres. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Six points, 1 1, 2 1, 1 3, 7 6, 8 8 and 9 6, with two centres started at 1 1 and 2 1. How many rounds until no centre moves?

02468100246810
the answer, and why

the answer is A

In round 1, the first centre takes 1, 1 and 1, 3 and moves to 1, 2; the second takes the rest and moves to 6.5, 5.25. In round 2, the point 2, 1 joins the first group, and both centres move again. Round 3 changes nothing.

if you chose another

  • Round 2 still moves both centres, so a third round is needed to see none move.
  • Round 1 moves both centres, so it cannot be the last.
  • That is how many points there are.

question 2

On the same points and centres, how many distances are measured in all?

02468100246810
the answer, and why

the answer is A

Each round measures 6 points against 2 centres, 12 distances, and there are 3 rounds.

if you chose another

  • That is one round only.
  • That counts 3 rounds of one distance a point.
  • That is one distance a point, once.

question 3

On the same points, how far does the second centre move in round 1?

02468100246810
the answer, and why

the answer is C

Its points are 2 1, 7 6, 8 8 and 9 6, whose middle is 6.5, 5.25. From 2, 1 that is 4.5 across and 4.25 up, the root of 38.31, about 6.19.

if you chose another

  • That is how far the first centre moves in round 1.
  • That is how far the second centre moves in round 2.
  • That is how far the first centre moves in round 2.

question 4

Which is true of k-means?

the answer, and why

the answer is C

It stops when no centre moves, and where it stops depends on where the centres start.

this tests best, worst and average, on the learn page

if you chose another

  • On the learn page, section 7, the same four points started top and bottom settle at 64, while left and right give 16.
  • Such a centre stays where it is.
  • Each round measures each point against every centre, k distances a point.

pen and paper

question 5about 12 minutes

Four points, 1 3, 1 7, 9 3 and 9 7. Run k-means twice, once from centres 5 3 and 5 7, once from 2 5 and 8 5.

  1. Write each point's group and each centre's place after each round.
  2. How many rounds does each run take?
  3. Work out each total squared distance. Which grouping is better?
a hint

A round in which no centre moves is the last.

a worked answer

From 5 3 and 5 7, the bottom points join the first centre and the top ones the second. Their middles are 5, 3 and 5, 7, where the centres already stand, so 1 round. Each of the 4 points is 4 from its centre, so the total is 4 times 4 squared, which is 64. From 2 5 and 8 5, the left points join the first centre and the right ones the second. The centres move to 1, 5 and 9, 5, and round 2 changes nothing, so 2 rounds. Each of the 4 points is 2 from its centre, so the total is 4 times 2 squared, which is 16. Left against right is better.

this tests how it works, on the learn page

what a full answer includes
  • Both runs played round by round.
  • One round and two rounds.
  • Totals 64 and 16, and the better named.

question 6about 10 minutes

Nine points in three rows, 1 0, 2 0, 3 0, 1 5, 2 5, 3 5, 1 10, 2 10 and 3 10. The centres start at 2 0, 2 5 and 2 10.

  1. Play the run round by round.
  2. Count the distances, and check them against 3n3n.
a hint

Each row is closer to itself than to any other row.

a worked answer

Round 1: each point joins its own row's centre, 3 rows of 3. Each row's middle is 2 across, where its centre already stands, so no centre moves and the run stops. It measures 9 points against 3 centres, 27 distances. 3n3n for 9 points is 27.

Question 1: nn points in three rows far apart, one above another, each spread evenly about the same middle across, and each centre started at its row's middle. How many distances, DD?
  1. the move: one round, and nothing moves

    the line:
    round 1: each point joinsits own row’s centre,and no centre moves\begin{gathered} \text{round 1: each point joins} \\ \text{its own row's centre,} \\ \text{and no centre moves} \end{gathered}
    02468100246810
    Six points in three rows, each centre started at its row's middle, 1 across. Round 1 moves no centre.

    why it is allowed: Each centre starts level with its row. Another row's centre is just as far across and farther up or down, so every point's nearest centre is its own row's. Each row is spread evenly about its centre, so its middle is where the centre already stands. If n is under 3, a row may have no points; that centre stays where it is. No centre moves, and the method stops.

  2. the move: count the one round

    the line:
    D=3nD = 3n

    why it is allowed: DD is the distances measured. By line 1 there is one round, and it measures each of the nn points against each of the 3 centres. For the six points, 18.

  3. the move: a ceiling

    the line:
    D≤3nD \le 3n

    why it is allowed: By line 2, DD is 3n3n, so it is at most 3n3n.

  4. the move: read it as a bound

    the line:
    D∈O(n)D \in O(n)

    why it is allowed: By line 3, DD is at most 3 times nn.

  5. the move: a floor

    the line:
    D≥3nD \ge 3n

    why it is allowed: By line 2, DD is at least 3n3n too.

  6. the move: read it as a bound

    the line:
    D∈Ω(n)D \in \Omega(n)

    why it is allowed: By line 5, DD never drops below 3 times nn.

  7. the move: ceiling and floor together

    the line:
    D∈Θ(n)D \in \Theta(n)

    why it is allowed: Lines 4 and 6 bound DD by the same growth, nn.

try it with a small nn: at n=6n = 6, line 2 gives D=18D = 18, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • One round, moving no centre.
  • Twenty-seven distances, matching 3n3n.