K-means clustering is an unsupervised algorithm that splits data into k groups of similar points. It starts with k cluster centres, assigns every point to its nearest centre, moves each centre to the average of its assigned points, and repeats until assignments stop changing. It is fast and simple, but you must choose k, scale your features, and remember that it assumes roughly round, similarly sized clusters.
What is clustering for?
Clustering finds structure in data without labels. Nobody tells the algorithm which customers are "bargain hunters" or which learners are "weekend crammers"; it discovers groups whose members are similar to each other and different from other groups. Typical uses are customer and learner segmentation, grouping similar products or documents, compressing colours in images, finding unusual points (those far from every cluster centre) and creating features for supervised models. It's one of the main forms of unsupervised learning, alongside dimensionality reduction such as PCA.
How does the k-means algorithm work, step by step?
- Choose k, the number of clusters.
- Initialise k centres, usually with k-means++, which spreads initial centres apart.
- Assign each point to its nearest centre (Euclidean distance).
- Update each centre to the mean of the points assigned to it.
- Repeat steps 3–4 until assignments no longer change (or barely change).
Each iteration reduces the within-cluster sum of squares (also called inertia): the total squared distance from points to their centres. The algorithm always converges, but possibly to a local optimum that depends on the starting centres, which is why libraries run it several times with different starts and keep the best.
What does a small example look like?
Take learners described by two standardised features: weekly study hours and share of assignments submitted on time. With k = 3, k-means might find: a group with high hours and high on-time submission (steady learners), a group with low hours but decent on-time submission (efficient but busy, perhaps working professionals), and a group low on both (at risk). A mentor team could then design different nudges for each group. The algorithm produces the groups; people give them meaning by examining the centres and sample members.
How do you choose k?
The elbow method: run k-means for k = 1, 2, 3, … and plot inertia. It always falls as k rises (with k equal to the number of points it reaches zero), but the drop usually slows sharply at some point, the "elbow".
Illustrative values chosen to show the shape of a typical elbow at k = 3, not results from a real dataset.
Silhouette score: for each point, compares how close it is to its own cluster versus the nearest other cluster, from −1 to 1; higher averages mean better-separated clusters. Usefulness: the most important test. If marketing can only run three campaigns, five clusters may not help, however good the score.
Why must you scale features?
K-means measures distance, so features with large numeric ranges dominate. If one feature is monthly spending in rupees (thousands) and another is age in years (tens), clusters will be almost entirely about spending. Standardise numeric features (mean 0, standard deviation 1) before clustering, and think about whether every feature should count equally; you can weight features deliberately when domain knowledge says some matter more.
What are k-means' limitations?
| Limitation | Why | Alternative |
|---|---|---|
| Assumes round, similar-sized clusters | Uses distance to a single centre | Gaussian mixture models |
| Can't find odd shapes (rings, crescents) | Boundaries are straight lines between centres | DBSCAN, spectral clustering |
| Sensitive to outliers | Means are pulled by extreme points | K-medoids, remove or cap outliers |
| Needs k in advance | Part of the algorithm | DBSCAN, hierarchical clustering |
| Poor for categorical data | Means of categories aren't meaningful | K-modes, k-prototypes |
How is k-means used with embeddings?
Modern applications often cluster embeddings rather than raw features: sentence embeddings of support tickets to discover common issues, product embeddings to group similar items, or image embeddings to organise photo collections. Because embeddings place similar items close together, k-means on them groups items by meaning. For high-dimensional embeddings, cosine similarity often works better than raw Euclidean distance, so vectors are usually normalised first.
How do you evaluate and present clusters?
- Profile each cluster: size, average of each feature, and a few typical members.
- Name clusters in plain language that a business user would recognise.
- Check stability: rerun with different seeds or on a different sample; good clusters persist.
- Test usefulness: do different clusters respond differently to an action? That's the real measure.
- Visualise with PCA or another projection to two dimensions.
What mistakes do beginners make?
- Not scaling features, then "discovering" clusters that are just one feature's ranges.
- Treating clusters as ground truth rather than a useful simplification.
- Clustering on IDs or other meaningless numbers.
- Picking k from the elbow alone when the curve has no clear elbow.
How do hierarchical clustering and DBSCAN differ from k-means?
Hierarchical clustering builds a tree of clusters by repeatedly merging the closest groups (or splitting from the top). You don't choose k in advance; you cut the tree, drawn as a dendrogram, at the level that makes sense. It suits small to medium datasets and gives an intuitive picture of how groups relate. DBSCAN defines clusters as dense regions separated by sparse ones. It finds arbitrarily shaped clusters, labels isolated points as noise rather than forcing them into a group, and needs a neighbourhood radius and a minimum number of points instead of k. It struggles when clusters have very different densities. Trying k-means alongside one of these is a quick way to learn whether your data's structure is really round and separate, or something else.
How is clustering used in Indian businesses?
Retailers and e-commerce companies segment customers by purchase frequency, basket size and categories to tailor offers. Lenders group borrowers by repayment behaviour. Telecom companies cluster usage patterns to design plans. Edtech platforms group learners by engagement to decide who needs a mentor's call. In each case, the clusters are a starting point for decisions, and their value is proven only when the different groups actually respond differently to what the business does next.
How do you use clusters after you find them?
Clusters become useful when they drive action. Common next steps are to add the cluster label as a feature in a supervised model, assign new customers or learners to the nearest existing centre in real time, track how many people move between clusters each month as a signal of changing behaviour, and design one experiment per cluster to test whether a tailored approach beats a generic one. Revisit the clustering periodically, because behaviour and data drift, and yesterday's segments may no longer describe today's users.
How should you practise?
Cluster a public customer or survey dataset, choose k with both the elbow and silhouette methods, profile and name each cluster, and write one recommendation per cluster. Then try DBSCAN on the same data and compare. K-means and PCA are part of the unsupervised learning topics in Program Zero's machine learning course; read the types of AI for where unsupervised learning fits overall.