Skip to main content

How does k-means work, and how do you choose k?

Machine learning basics · Intermediate 1

Short answer

k-means picks k centres, assigns each point to its nearest centre, moves each centre to the mean of its points, and repeats until assignments stop changing. To choose k, try several values and look at the elbow in within-cluster distance, the silhouette score, and above all whether the clusters are useful for the business question.

Deeper answer

k-means assumes roughly round clusters of similar size and is sensitive to feature scale and outliers, so scale features first. It finds a local optimum, so run it several times with different starts; k-means++ initialisation helps.

The elbow method plots within-cluster sum of squares against k and looks for where improvement flattens. Silhouette measures how much closer points are to their own cluster than to the next one. Neither is decisive: for customer segments, the right k is often the number of groups the marketing team can actually act on. If clusters are irregular in shape, consider DBSCAN or a Gaussian mixture.

What a strong answer covers

Answer it out loud in the mock interview about 1.5 min

Follow-ups to expect

Common mistakes

Learn it

Unsupervised learning

Today's AI turns are used

Lessons, roadmaps, videos, practice cards and the code playground stay free with your account, with no limit.