K-Means Clustering
Part of the AI Engineer Roadmap
In short
You have thousands of customers and no labels. K-Means groups similar ones together, so you can treat bargain hunters, weekend shoppers and big spenders differently.
An unsupervised algorithm that partitions data into K clusters by minimizing distance to cluster centroids — the default first tool for finding groups in unlabeled data.

Think of it like this
Placing chai stalls on campus
You may open three chai stalls on campus. Put them anywhere, then each student walks to the nearest one. Now shift each stall to the middle of its own crowd. Students re-pick their nearest stall. Repeat until nobody switches. Those three crowds are your clusters.
Watch it happen
One run of K-Means
1/6You decide how many clusters you want, say k = 3.
The big ideas
What you need to know
You must pick k
K-Means won't decide the number of groups. Try several values and use the elbow plot or silhouette score.
Scale your features
Distance drives everything. Income in rupees would drown out age in years unless you standardise first.
Likes round blobs
K-Means assumes compact, roughly round clusters of similar size. Long or curved shapes confuse it.
Start matters
Different starting centres can give different results. n_init runs it several times and keeps the best.
See it in code
Walk through the code
1from sklearn.cluster import KMeans2from sklearn.datasets import make_blobs3from sklearn.preprocessing import StandardScaler4 5X, _ = make_blobs(n_samples=300, centers=3, random_state=42)6X = StandardScaler().fit_transform(X)7 8for k in range(2, 7):9 km = KMeans(n_clusters=k, n_init=10, random_state=0).fit(X)10 print(k, round(km.inertia_, 1)) # look for the elbow11 12km = KMeans(n_clusters=3, n_init=10, random_state=0).fit(X)13print(km.cluster_centers_)1/3Make 300 points in 3 hidden groups and scale them.
inertia_ is the total squared distance from points to their centre. It drops fast until the right k, then flattens.
Save yourself the pain
Common mistakes
Oops
Running K-Means on raw columns with wildly different units.
Do this instead
Standardise first, ideally with StandardScaler inside a Pipeline.
Oops
Treating cluster numbers 0, 1, 2 as meaningful labels.
Do this instead
Cluster IDs are arbitrary. Profile each cluster's averages and give it a human name yourself.
Your mission
Segment mall customers
Quick check
After assigning points to centres, what does K-Means do next?
Go deeper
- 1Implement K-Means on a 2D dataset and visualize the clusters forming over iterations.
- 2Learn the elbow method for choosing K.
- 3Understand K-Means' limitations: it assumes spherical, similarly-sized clusters.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.