K-Means Clustering Explained With a Worked Example

K-Means Clustering cover with three small groups of ceramic beads and larger centroid markers.

Suppose delivery times seem to fall into a short group and a long group. You have the times but nobody has labeled the deliveries. K-means clustering can assign each record to a group and summarize each group with a center.

K-means clustering groups observations around K centers called centroids. It alternates between assigning each observation to its nearest centroid and updating each centroid to the mean of its assigned observations. You choose K before the algorithm fits.

Try one run by hand

Take delivery times of 2, 4, 6, 12, 14, and 16 minutes. Set K to 2 and start with centers at 2 and 6. The point at 4 is equally close to both, so assign it to the first cluster for this example.

StepFirst groupSecond groupUpdated centers
First assignment2, 46, 12, 14, 163 and 12
Second assignment2, 4, 612, 14, 164 and 14
Third assignment2, 4, 612, 14, 164 and 14

The first update moves the centers to 3 and 12. At the next assignment, 6 is three minutes from the first center and six minutes from the second, so it changes groups. Once the centers reach 4 and 14, the next assignment leaves them unchanged.

K-means tries to reduce the sum of squared distances from observations to their assigned centers. This sum is called inertia. Here the first assignment with its updated centers has inertia 58. After the second update, the inertia is 16. The final two groups each contribute 8 because their distances from the centers are 2, 0, and 2.

Decide what distance should mean

When a record has several features, each feature can influence which center is closest. If you cluster deliveries by kilometers traveled and seconds delayed, the column with the larger numerical spread can dominate. Standardizing can make their scales comparable. If a unit of difference in one feature should matter more, choose that weighting explicitly.

Inspect outliers before fitting because squared distances give faraway points considerable influence. Check whether the outlier is an error or a rare but meaningful case. Keep the fitted scaling transformation and use it again when assigning new observations.

If you use PCA to draw a two-dimensional picture of clusters, remember that the projection can hide structure in other directions. If the model clusters in a higher-dimensional space, evaluate the grouping there as well. Unlimited AI guide to principal component analysis.

Choose K by comparing plausible groupings

Start with a small range of K values that makes sense for the task. Inertia generally decreases as more centers are available, so the lowest inertia alone favors more clusters. The elbow method looks for the point where adding another center gives much less improvement. Some datasets have no clear elbow.

Silhouette analysis examines how well a point fits its assigned cluster compared with its nearest alternative cluster. Scores near 1 suggest a clear assignment, scores near zero indicate a boundary, and negative values suggest that a point may fit better elsewhere. Compare cluster sizes and individual silhouettes alongside the average. Scikit-learn silhouette analysis example.

Silhouette scores for four clusters beside a scatter plot of their assigned observations.
Silhouette widths and scatter plots help diagnose different aspects of the same four-cluster fit. Chart by the scikit-learn developers.

Repeat the fit with several initializations. K-means can settle on different solutions depending on its starting centers. K-means++ chooses initial centers designed to spread them out, and multiple runs help when a single starting set produces a poor grouping.

Recognize when the clusters mislead

K-means assigns every observation to a center even when the data has no distinct groups. It works most naturally when clusters are compact and reasonably comparable in spread. Elongated groups, very unequal densities, and curved boundaries can produce results that look neat in color and still divide the underlying structure poorly. Scikit-learn demonstration of K-means assumptions.

Four scatter plots show how K-means partitions elongated groups, unequal spreads, uneven sizes, and data fitted with the wrong K.
These examples show how K-means responds to different shapes, spreads, and group sizes. Chart by the scikit-learn developers.

Check whether each group has a practical interpretation. For delivery times, compare routes, service levels, and sample counts across groups. If the grouping disappears when you change the data sample or a single feature, treat it as tentative. If the task has known labels and the goal is to predict them, use a supervised evaluation workflow.

Two questions before using the result

Can K-means assign a new observation?

Yes. Apply the same preprocessing used during training, then assign the observation to the nearest fitted centroid. If the population changes, check whether those old centers still describe it.

Are cluster numbers meaningful labels?

The numbers are identifiers. A refit can swap the IDs even when it finds similar groups. Name clusters by their measured profiles, such as shorter or longer deliveries, after examining the grouped observations.

Record the features, scaling, K, and how you compared initializations. Then inspect a few examples near each center and a few near cluster boundaries before using the grouping to make decisions.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top