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.
K-means groups data into k clusters by repeatedly assigning each point to its nearest centre and moving each centre to the average of its points. It is fast and simple but assumes roughly round clusters of similar size.

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
- 1Run KMeans on scaled customer data and plot the clusters on two features.
- 2Describe each cluster with groupby averages and give it a plain-English name.
- 3Run it with different random seeds and see if the clusters change.
Also part of these roadmaps
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.