04.13 Lesson 13 of 16

1 h

Clustering 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

Exercise

Explore numeric clusters

Use a small numeric feature table to fit several K-means models and compare the structure they produce.

  1. Select numeric features and inspect their units and distributions.
  2. Scale features before fitting K-means.
  3. Compare several values of k with inertia and silhouette score.
  4. Inspect cluster sizes and feature summaries in original units.
  5. Write two limitations of the discovered clusters and one question requiring domain validation.
Done when

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

2 links
  1. 01
    K-means Clustering Video

    Watch the individual K-means explanation. Focus on the assignment and centroid-update steps.

  2. 02
    Clustering Article

    Read K-means, initialization, inertia, limitations and evaluation sections.

Check your understanding

Self-test

2 questions
  1. Question 1

    What are the two repeated steps in K-means?

  2. Question 2

    Why is a low inertia value not enough to select k?

Completion check

Move on when

You can run K-means on scaled features and evaluate the resulting groups with more than one diagnostic.

Quick find

Search Michi

Type to search every stage, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels