BeginnerCoder

Lesson 22 — k-means step (expert)

k-means: find groups without labels

Clustering finds groups in data that has no labels. k-means keeps k centres and repeats two steps: (1) assign every point to its nearest centre; (2) update every centre to the mean of the points assigned to it. Repeat until the centres stop moving.

groups = {0: [1, 2], 1: [9, 10]}
new_centres = [sum(g) / len(g) for g in groups.values()]   # [1.5, 9.5]

Why it matters: clustering is used for customer segments, image compression and spotting unusual data.

Want it another way?
Task

Write the update step: each centre becomes the mean of its group. Watch the centres move and then settle.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with AI / ML Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.