🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101

Customer segmentation (K-means)

Group customers by buying behaviour (RFM) with K-means and turn clusters into marketing actions.

Intermediate⏱ 6–10 hoursđŸĒœ 7 stepsPythonscikit-learnpandas

What you'll learn

  • ◆Unsupervised learning
  • ◆RFM features
  • ◆Choosing k (elbow, silhouette) and describing clusters

What you'll build

  • ✓RFM table from transactions
  • ✓Scaled features and K-means
  • ✓Elbow and silhouette plots
  • ✓Named segments with suggested actions

Step-by-step guide

  1. 1

    Data

    Use a transactions dataset such as the UCI Online Retail dataset.

  2. 2

    Clean

    Remove cancelled invoices and missing customer IDs; compute line revenue.

  3. 3

    RFM

    Recency (days since last order), Frequency (orders), Monetary (total spend) per customer.

  4. 4

    Scale

    Log-transform skewed values, then StandardScaler.

  5. 5

    Pick k

    Fit K-means for k = 2..10; plot inertia (elbow) and silhouette_score; choose a k you can explain.

  6. 6

    Describe clusters

    Average R, F, M per cluster; name them ("Champions", "At risk", "New").

  7. 7

    Actions

    One recommendation per segment, plus a 2D PCA plot coloured by cluster.

🚀 Make it stand out

  • +Compare with DBSCAN or hierarchical clustering
  • +Customer lifetime value
  • +Dashboard for marketing