04.13 Lesson 13 of 16
1 hClustering with K-Means
Group observations without target labels and understand K-means initialization, inertia, scaling and evaluation limits.
By the end of this lesson
You will be able to
- Explain the assign-and-update procedure.
- Describe the within-cluster sum-of-squares objective.
- Explain why scale and initialization matter.
- Use inertia and silhouette scores cautiously.
K-means alternates assignment and updates
K-means starts with k centroids. Each observation is assigned to its nearest centroid, then each centroid moves to the mean of its assigned observations. The two steps repeat until assignments or centroid movement stabilize. The objective is to minimize within-cluster sum of squared distances. K-means is sensitive to scale, initialization and outliers, and tends to work best when clusters are reasonably compact under Euclidean distance.
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
X_scaled = StandardScaler().fit_transform(X_numeric)
clusterer = KMeans(n_clusters=4, n_init=20, random_state=42)
labels = clusterer.fit_predict(X_scaled) Cluster quality is not prediction accuracy
Inertia decreases as more clusters are added, so the elbow method is a heuristic rather than proof of the correct k. A silhouette score compares how close a sample is to its own cluster relative to other clusters, but it also has assumptions and limitations. Because there are no target labels, external classification metrics are not automatically appropriate. Inspect cluster sizes, feature summaries, stability and whether a downstream decision can use the groups. A cluster is a pattern in this representation, not a discovered natural truth.
Apply what you learned
ExerciseExplore numeric clusters
Use a small numeric feature table to fit several K-means models and compare the structure they produce.
- Select numeric features and inspect their units and distributions.
- Scale features before fitting K-means.
- Compare several values of k with inertia and silhouette score.
- Inspect cluster sizes and feature summaries in original units.
- Write two limitations of the discovered clusters and one question requiring domain validation.
The work uses scaled features, records initialization settings, compares more than one k and interprets clusters as exploratory structure rather than ground truth.
Free references
Study resources
- 01 K-means Clustering Video
Watch the individual K-means explanation. Focus on the assignment and centroid-update steps.
- 02
Check your understanding
Self-test
-
Question 1
What are the two repeated steps in K-means?
Assign each observation to its nearest centroid, then update each centroid to the mean of its assigned observations. -
Question 2
Why is a low inertia value not enough to select k?
Inertia naturally decreases as k increases; it must be considered with cluster size, stability, silhouette and the intended use.
Completion check
Move on when
You can run K-means on scaled features and evaluate the resulting groups with more than one diagnostic.