K-means is the most used clustering method in business analytics and also one of the most misunderstood, because people meet it as a single button in a tool. We will run it by hand on a toy table, from first guess to final answer, and watch every step.

The data

Ten customers of an online store. For each we have two numbers: orders placed in the last six months, and average basket size in hundreds of rupees (so 3.0 means ₹300). The aim is to find natural groups without anyone telling the computer what the groups are.

CustomerOrdersBasket (₹00)
C142.0
C252.5
C363.0
C453.5
C52214.0
C62516.0
C72012.0
C82415.0
C91230.0
C101434.0

Plot them in your head. A small-and-infrequent group at the bottom left, a frequent mid-basket group, and two customers who order less often but spend heavily. We pick k = 3, because we already suspect three groups. K-means does not choose k for you.

The loop

The algorithm repeats two moves. Assign each point to the nearest centre. Then move each centre to the average position of the points assigned to it. Stop when nothing changes. “Nearest” means straight-line distance on the two numbers.

Start with centres placed on three customers: C1, C5 and C9, so (4, 2), (22, 14) and (12, 30).

Assignment. C2, C3 and C4 are all close to C1’s centre, so they join it. C6, C7 and C8 are close to C5’s centre. C10 at (14, 34) is nearest to C9’s centre. That gives groups {C1, C2, C3, C4}, {C5, C6, C7, C8} and {C9, C10}.

Update. The first group’s average is (5, 2.75). The second’s is (22.75, 14.25). The third’s is (13, 32). Re-run the assignment with these new centres and nobody changes group. The loop has converged in a single round, which is rare in real data but fine for a toy.

To judge the result we use the within-cluster sum of squared distances, the total of each point’s squared distance from its own centre. For our groups it is 3.25 plus 23.5 plus 10.0, which is 36.75. Smaller means tighter groups.

Same data, unlucky start

Now begin with centres on C1, C2 and C5. Two of the three centres sit inside the small-and-infrequent group. In the first assignment C1 keeps its own centre, C2, C3 and C4 go to the second centre, and everything else, including the two heavy spenders C9 and C10, goes to the third because it is the only centre out there. Update: the third centre moves to about (19.5, 20.2), dragged toward C9 and C10. Run again and nothing moves. The algorithm halts.

This is a stable answer, and a bad one. The sum of squares is about 581.5, against 36.75 earlier (I ran both through a small script to confirm). One “cluster” now mixes frequent mid-basket customers with high-basket occasional ones, which are the sort of people a marketing team would treat very differently.

The lesson is that k-means finds a local optimum, and which one depends on where the centres start. Practical defences: run it many times from random starts and keep the lowest sum of squares, and use a smarter seeding scheme such as k-means++, which spreads the first centres apart. Most libraries do both for you. If you use a tool that does neither, you will sometimes get the stuck answer without knowing.

What the toy hides

Scale. Distance treats a one-unit change in each variable as equal. Here basket runs from 2 to 34 while orders run from 4 to 25, so basket carries a bit more influence. In real data, one column in rupees (thousands) and another in counts (single digits) would let the rupee column decide everything. Standardise each variable first, typically by subtracting its mean and dividing by its standard deviation.

Choosing k. Plot the sum of squares for k = 1, 2, 3 and so on. It always falls as k grows, so look for the bend where gains become small. Better still, ask whether the groups are usable: can a business team name each one and act on it differently? Eight clusters that nobody can describe are worse than four that everyone can.

Shape and outliers. K-means likes round, similarly sized blobs, since each group is summarised by a mean. Long, curved or very uneven groups confuse it, and one extreme spender can drag a centre. Capping or isolating outliers helps. Categories such as city or payment type don’t fit naturally either, because “distance” between categories is meaningless without extra work.

Finally, clusters are descriptions, not truths. Run it again next quarter and customers will migrate. Treat the labels as a lens you maintain, much as you would with segments from an RFM scoring exercise, and judge them by whether a decision improves.

Figures and names in the examples are illustrative.

Leave a Reply

Trending