Intermediate level · Module 4: Unsupervised Learning · Lesson 1 of 3
k-Means Clustering
Group similar records with k-means and choose a sensible number of clusters.
In this lesson you will
- Run k-means on scaled data
- Choose k with the elbow and silhouette methods
Key concepts
k-meanscentroidinertia and elbowsilhouette score
The lesson
k-means places k centres and assigns each record to the nearest one, moving the centres until they settle. Because it uses distances, scale features first.
Choosing k is a judgement: the elbow method plots within-cluster spread against k and looks for a bend; the silhouette score measures how well separated clusters are. Business usefulness matters as much as the numbers.
k-means assumes roughly round clusters of similar size and every point belongs to a cluster. Results can depend on the starting centres, so use several initialisations and a fixed random_state.
Step by step
- Scale the agent features.
- Run k-means for k from 2 to 8.
- Plot the elbow and silhouette scores.
- Choose k and describe each cluster's average profile.
Practice activity
Produce a first segmentation of agents with k-means and justify the number of clusters.
Expected result: A chosen k with evidence and a profile table for each cluster.
Check your understanding
-
Why scale data before k-means?
Show answer
It uses distances, so large-scale features would dominate.
-
Does k-means always produce clusters, even in random data?
Show answer
Yes; the clusters must be checked for meaning.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.