Module 11 · Machine Learning: How Models Learn · Lesson 38 of 51

Clustering and the model zoo

K-means step by step, choosing k with the elbow, and a tour of the main model families (linear, trees, forests, boosting, nearest neighbours) and when to use each.

In this lesson · about 40 minutes

  • Explain the two steps of k-means and why scaling matters
  • Choose k with an elbow chart and name the clusters in business terms
  • Match common model families to problems

Try it · data lab

K-means, step by step

Group members by visits and spend. Step through assign and move, try different k, and read the elbow chart.

k
Iteration 1 of 12
123Visits in 90 days →Average spend ($)

Cluster 1: 19 members, about 16 visits, $4.73 average spend

Cluster 2: 62 members, about 8 visits, $6.27 average spend

Cluster 3: 19 members, about 21 visits, $6.16 average spend

04812171234567k = 3k (number of clusters)Inertia (lower = tighter)
Each iteration does two things: every member joins the nearest centre, then each centre moves to the middle of its members. It stops when nobody switches. The elbow chart shows where adding clusters stops helping much (here around 3). Then name the clusters: “occasional visitors”, “daily regulars”, “big spenders”.

K-means in two steps

  1. Assign: each point joins the nearest centre.
  2. Move: each centre moves to the average of its points.

Repeat until assignments stop changing. Inertia is the total squared distance from points to their centres; it always falls as k grows, so look for the elbow where adding a cluster stops helping much. Scale features first, or the one with the biggest numbers decides everything. Run several random starts (scikit-learn does this) because a bad start can give a poor answer.

The model zoo

FamilyIdeaStrengthsWatch for
Linear / logistic regressionWeighted sum of featuresFast, explainable, a strong baselineMisses curves and interactions unless you engineer them
Decision treeA flowchart of yes/no questionsEasy to explain, handles mixed dataOverfits when deep
Random forestMany trees on random samples, averagedAccurate, robust, little tuningBigger, less explainable
Gradient boostingTrees added one by one to fix the last ones' errorsOften the best on tables of dataNeeds tuning; can overfit
k-nearest neighboursCopy the answer of similar past rowsSimple, no trainingSlow on big data, needs scaling
Neural networksLayers of weighted sums with bendsImages, text, soundUsually overkill for business tables

Exercise

One round of k-means by hand

Given six points and two starting centres, do one assign step and one move step. Set labels to a list of 0/1 (nearest centre for each point, by straight-line distance) and new_centres to a list of two [x, y] lists, rounded to 2 decimals.

Code that runs before yours
import numpy as np
points = np.array([[1, 2], [2, 1], [1.5, 1.8], [8, 8], [9, 7.5], [7.5, 9]])
centres = np.array([[2.0, 2.0], [6.0, 6.0]])

Flashcards

Module 11 flashcards

0 of 11 known

Say the answer out loud, flip the card, then be honest: cards you miss come back at the end.

11 left in the deck

Practice exam

Module 11 checkpoint

12 questions, 15 minutes.

  • 12 questions · 15 minutes
  • No feedback until you submit. Flag questions to come back to them.
  • Aim for 85% or better before you move on. The real exam's 750 out of 900 is roughly 80–83%.

Exercises solved: 0 of 1

Tip: solve each exercise first. Press Check to test your code.

Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.