DS701 Session 4 — Wed Sep 16, 2026
Name the quantity and write it as a formula.
Points \(1.2,\ 1.8,\ 2.1,\ 7.3,\ 7.9,\ 8.2,\ 9.1,\ 10.5\), initial centers \(A = 4.0\), \(B = 11.0\).
Cluster customers on age (18–80) and income ($20k–$200k), unscaled. What determines the clusters?
A clustering is a grouping of data objects such that objects within a group are similar and objects in different groups are dissimilar.
So we want to:
And it is unsupervised — no labels go in, and any labels we put on the clusters we invent afterwards.

Partitional setup for \(k\)-means: data are points in \(\mathbb{R}^d\), every object belongs to one and only one cluster (mutually exclusive), the clusters cover everything (exhaustive), and \(K\) is given in advance.
Neither step 2 nor step 3 can increase WCSS \(\Rightarrow\) it always converges — but only to a local minimum.
scikit-learn’s default, and n_init restarts the whole thing several times and keeps the best WCSS.
\(K\) is our first hyperparameter: a parameter that must be set before the model parameters (the centers) can be learned.
WCSS alone can’t pick it — WCSS decreases monotonically in \(K\), all the way to \(0\) at \(K = n\).
So: sweep \(K = 1, 2, 3, \dots\), fit each, and score it.
Assignment is “nearest center”, so cluster regions are straight-sided Voronoi cells and clusters are implicitly assumed to be spheres around their center.
Because \(k\)-means looks for spherical clusters in Euclidean distance, the units of each feature are part of the model.
\[ \begin{bmatrix}\text{age } 27\\\text{income } 75000\\\text{gender } 0\end{bmatrix}, \qquad \begin{bmatrix}\text{age } 45\\\text{income } 42000\\\text{gender } 1\end{bmatrix} \]
Standard remedy, per feature and independently:
Whenever you build or pick a distance metric, think about feature scale first.
\(k\)-means is a workhorse when the assumptions roughly hold:


scikit-learnGoal: implement one Lloyd’s iteration yourself, check it against sklearn.cluster.KMeans, then the stretch — build the silhouette coefficient from its definition, sweep \(k\), and settle a case where the elbow and the silhouette disagree — and finally break \(k\)-means on purpose and diagnose why.
numpy, matplotlib, scikit-learn only.The activity notebook goes live on the day of the lecture. Colab is optional — you can also open it on GitHub and run it locally.
Full lecture notes: \(k\)-Means Clustering