Machine Learning (ML) · Intermediate level

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

  1. Scale the agent features.
  2. Run k-means for k from 2 to 8.
  3. Plot the elbow and silhouette scores.
  4. 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

  1. Why scale data before k-means?

    Show answer

    It uses distances, so large-scale features would dominate.

  2. 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.