ENع
←Clustering
Lesson 10 · Classic machine learning

Clustering

14 min10 steps5 questionsIntermediate
About this lesson

Clustering is unsupervised learning: it groups data that has no labels, by putting similar (close) points together. Watch k-means sort six café customers into two groups. It repeats two moves: every customer joins its nearest centre, then every centre moves to the mean of its customers. A poor start gets fixed, one customer switches sides and the inertia falls until nothing changes. Then see how to choose k, and why the start matters.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 10 · Interactive
01 / 10

Groups, but no labels

Six café customers, each a dot: visits per month across, spend per visit up. Nobody labelled them. Clustering, a kind of unsupervised learning, finds groups of similar points by itself. We use the classic algorithm, k-means.

customer   visits/month   spend/visit (KD)
  0        1              2
  1        2              1
  2        3              3
  3        6              6
  4        7              8
  5        8              7

no labels: nobody says which group a customer is in
clustering = unsupervised learning: find the groups
Loading figure…
or ← →
Read the lesson step by step
  1. Groups, but no labels

    Six café customers, each a dot: visits per month across, spend per visit up. Nobody labelled them. Clustering, a kind of unsupervised learning, finds groups of similar points by itself. We use the classic algorithm, k-means.

    customer   visits/month   spend/visit (KD)
      0        1              2
      1        2              1
      2        3              3
      3        6              6
      4        7              8
      5        8              7
    
    no labels: nobody says which group a customer is in
    clustering = unsupervised learning: find the groups
  2. Similar means close

    k-means measures similarity as straight-line distance. From (1, 2) to (2, 1) is √2 ≈ 1.41: similar customers. From (1, 2) to (7, 8) is √72 ≈ 8.49: very different. Close points belong together.

    distance = √((x1 − x2)² + (y1 − y2)²)
    
    (1, 2) → (2, 1)   √(1 + 1)   = √2 ≈ 1.41
    (1, 2) → (7, 8)   √(36 + 36) = √72 ≈ 8.49
    
    Python: math.dist(p, q)
  3. Pick k starting centres

    We ask for k = 2 groups. k-means starts with k centres, here two of the customers: c1 = (1, 2) and c2 = (3, 3). A centre (centroid) is the point that stands for its group. This start is poor, on purpose.

    k = 2                       (we choose k)
    cs = [pts[0], pts[2]]
    c1 = (1, 2)   c2 = (3, 3)
    
    two neighbours as starting centres:
    a poor start, so we can watch k-means fix it
  4. Assign: join the nearest

    Each customer joins its nearest centre. (2, 1) is 1.41 from c1 but 2.24 from c2, so it joins c1. (3, 3) is c2 itself, and the three on the right are nearer c2: c2's group has four.

        g = assign(pts, cs)
      (1, 2)   c1  0.00  c2  2.24  -> c1
      (2, 1)   c1  1.41  c2  2.24  -> c1
      (3, 3)   c1  2.24  c2  0.00  -> c2
      (6, 6)   c1  6.40  c2  4.24  -> c2
      (7, 8)   c1  8.49  c2  6.40  -> c2
      (8, 7)   c1  8.60  c2  6.40  -> c2
  5. Update: move to the mean

    Each centre moves to the mean (average) of its group. c1: mean of (1, 2) and (2, 1) is (1.5, 1.5). c2: mean of its four is (6, 6). The inertia, the total squared distance to the centres, falls from 102 to 29.

        new = means(pts, g)
    c1 = mean of (1, 2), (2, 1)         = (1.5, 1.5)
    c2 = mean of (3, 3), (6, 6), (7, 8), (8, 7)
       = (24 / 4, 24 / 4)                   = (6, 6)
    inertia = sum of (distance to own centre)²
            102 -> 29
  6. Assign again: one switches

    Same rule, new centres. (3, 3) is now √4.5 ≈ 2.12 from c1 but √18 ≈ 4.24 from c2, so it switches to c1. Every other customer keeps its centre. The inertia drops to 15.5.

        g = assign(pts, cs)
      (1, 2)   c1  0.71  c2  6.40  -> c1
      (2, 1)   c1  0.71  c2  6.40  -> c1
      (3, 3)   c1  2.12  c2  4.24  -> c1  switched
      (6, 6)   c1  6.36  c2  0.00  -> c2
      (7, 8)   c1  8.51  c2  2.24  -> c2
      (8, 7)   c1  8.51  c2  2.24  -> c2
    inertia = 15.5
  7. Update again

    c1 moves to the mean of (1, 2), (2, 1) and (3, 3): (2, 2). c2 moves to the mean of the other three: (7, 7). The inertia falls to 8.

        new = means(pts, g)
    c1 = mean of (1, 2), (2, 1), (3, 3) = (2, 2)
    c2 = mean of (6, 6), (7, 8), (8, 7) = (7, 7)
        cs = new
    inertia 15.5 -> 8
  8. Nothing changes: stop

    One more pass: every customer keeps its centre, so the means stay put and the loop stops. k-means has converged: occasional small spenders and frequent big spenders. No step ever raises the inertia: 102, 29, 15.5, 8.

        g = assign(pts, cs)   # nobody moves
        new = means(pts, g)   # same centres
        if new == cs: break   # True: stop
    
    centres  c1 (2, 2)   c2 (7, 7)
    groups   0 0 0 1 1 1   (0 = c1, 1 = c2)
    inertia  102 -> 29, 15.5 -> 8, 8 -> 8
  9. Choosing k

    k-means does not choose k for you. The best inertia for k = 1, 2, 3, 4 is 83, 8, 5, 2. It keeps falling as k grows, so look for the elbow, where it stops falling fast: here, k = 2.

    k   best inertia
    1    83  ############################
    2     8  ###
    3     5  ##
    4     2  #
    
    big drop from k = 1 to 2, small after: the elbow is 2
    k = 6 would give 0: every customer its own group
  10. Starts matter

    k-means only finds a local best: a different start can end in worse groups. So it is often run several times from different starts, keeping the lowest inertia. The k-means++ start spreads the first centres apart.

    from sklearn.cluster import KMeans
    km = KMeans(n_clusters=2, n_init=10).fit(pts)
    km.cluster_centers_   # the centres
    km.labels_            # the group of every point
    km.inertia_           # the best of the 10 runs
    
    default start: init='k-means++'

Quiz Unlocks after the last step.