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.
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.
A quick 3-question quiz on this lesson — instant feedback.
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.
Project-based AI / ML Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for AI / ML Foundations books — check the publication year so the examples match current versions.
Search Amazon →