Guided project · Algorithms in detail

Find customer groups with K-means clustering

Use unsupervised learning to discover customer segments nobody labelled, then turn them into marketing ideas.

About 45 minIntermediateCopy-paste code
0 of 6 steps done

What you'll build

A scatter chart of customers coloured by group, with a name and an action for each group.

What you'll learn

  • Unsupervised learning in practice
  • Choosing K with the elbow method
  • Turning clusters into business decisions

You'll need

Google Colab (free, in your browser)

Create a free account to unlock this project

The step-by-step instructions, copy-ready prompts, progress tracker and portfolio texts are for members. It's free, takes under a minute, and unlocks all guided projects.

Want to try first? These projects are open to everyone: Prompt lab and One idea, five posts.

Before you start

Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot). Never paste private or confidential information.

Step by step

  1. Create customer data

    This makes 300 made-up customers with two numbers: yearly income and a spending score (1–100).

    Copy this code
    import numpy as np, pandas as pd
    import matplotlib.pyplot as plt
    
    rng = np.random.default_rng(7)
    centres = [(25, 20), (25, 80), (55, 50), (85, 20), (85, 80)]
    rows = [rng.normal(c, 6, size=(60, 2)) for c in centres]
    df = pd.DataFrame(np.vstack(rows), columns=['income_k', 'spending_score']).clip(1, 100).round(1)
    df.head()

    You should see: The first rows of a customer table.

  2. Look at it

    Plot every customer as a dot.

    Copy this code
    plt.scatter(df.income_k, df.spending_score, s=10)
    plt.xlabel('Income (thousands)'); plt.ylabel('Spending score'); plt.show()

    You should see: Dots that seem to form a few loose groups, but without labels.

  3. Choose K with the elbow method

    Try K from 1 to 9 and plot how tight the groups are (inertia).

    Copy this code
    from sklearn.cluster import KMeans
    
    inertia = [KMeans(n_clusters=k, n_init=10, random_state=0).fit(df).inertia_ for k in range(1, 10)]
    plt.plot(range(1, 10), inertia, marker='o')
    plt.xlabel('K'); plt.ylabel('inertia'); plt.show()

    You should see: A curve that bends sharply (the elbow) around K = 5.

    Why it matters: After the elbow, adding more groups barely helps.

  4. Run K-means with K = 5

    Find the 5 groups and colour the chart by group.

    Copy this code
    km = KMeans(n_clusters=5, n_init=10, random_state=0)
    df['group'] = km.fit_predict(df[['income_k', 'spending_score']])
    plt.scatter(df.income_k, df.spending_score, c=df.group, s=10, cmap='tab10')
    plt.scatter(*km.cluster_centers_.T, c='black', marker='x', s=120)
    plt.xlabel('Income (thousands)'); plt.ylabel('Spending score'); plt.show()

    You should see: Five coloured groups with a black X at the centre of each.

  5. Describe each group

    Print the average income and spending score per group.

    Copy this code
    print(df.groupby('group')[['income_k', 'spending_score']].mean().round(1))

    You should see: A small table with one row per group.

  6. Name the groups and act

    In a text cell, give each group a name (e.g. "high income, low spending: cautious savers") and one marketing idea.

    You should see: Five named segments with actions. This is how clustering is used in real businesses.

Level up

  • Try K = 3 and K = 8 and explain why 5 works better here.
  • Add a third column (age) and cluster again.

Lessons behind this project

Show it off

Finished? Post about it on LinkedIn. Here's a starting point; add a screenshot and your own words.

Copy this post
I used K-means clustering to find 5 customer segments in data with no labels, then named each one and suggested a marketing action. Unsupervised learning made simple with AI From Zero.
Share on LinkedIn

Add it to your portfolio

Employers love seeing what you've built. Use these ready-made texts, then add your own results and a screenshot.

1. A line for your CV or LinkedIn "Projects" section

Copy for your CV
Find customer groups with K-means clustering: A scatter chart of customers coloured by group, with a name and an action for each group. Skills: Unsupervised learning in practice, Choosing K with the elbow method, Turning clusters into business decisions. Tools: Google Colab (free, in your browser).

2. A README for GitHub

Create a free GitHub account, make a new repository, add a file called README.md and paste this in along with your notebook (File → Download → .ipynb).

Copy README.md
# Find customer groups with K-means clustering

Use unsupervised learning to discover customer segments nobody labelled, then turn them into marketing ideas.

## What I built
A scatter chart of customers coloured by group, with a name and an action for each group.

## What I learned
- Unsupervised learning in practice
- Choosing K with the elbow method
- Turning clusters into business decisions

## Tools
Google Colab (free, in your browser)

## How it works
1. Create customer data
2. Look at it
3. Choose K with the elbow method
4. Run K-means with K = 5
5. Describe each group
6. Name the groups and act

## Results
(Add a screenshot and 2–3 sentences about what you found. Never include API keys or personal data.)

## Next steps
- Try K = 3 and K = 8 and explain why 5 works better here.
- Add a third column (age) and cluster again.

---
Built by following the free guided project at https://learnaifromzero.com/projects/customer-segments.html