Day 5:Unsupervised Machine Learning — `K-Means` Clustering on Text Embeddings
Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz
Week 5 — Day 5: Unsupervised Machine Learning — K-Means Clustering on Text Embeddings
Mentor's Note (10+ YOE Staff AI Engineer POV): What do you do when your CTO hands you 50,000 unlabeled customer chatbot questions or server incident logs and asks: "What are the top 5 recurring problem topics our users are asking about?" You don't have labels (), so you can't use Supervised Learning! Instead, you embed the texts into vectors and run Unsupervised Clustering (
K-Means) to automatically discover the natural topic groups!
Part 1: How K-Means Clustering Works (Lloyd's Algorithm & Objective Function)
K-Means partitions vectors into clusters by minimizing the Within-Cluster Sum of Squares (WCSS / Inertia):
where is the Centroid (center of gravity / mean vector) of cluster .
The 2-Step Alternating Loop (EM-Style):
- Assignment Step: Assign every point to the closest centroid :
- Update Step: Recompute each centroid as the exact mean of all points assigned to cluster : Repeat Steps 1 & 2 until centroids stop moving!
Part 2: Hands-On Lab — K-Means Coded From Scratch (kmeans_topic_clusterer.py)
Check out src/kmeans_topic_clusterer.py (in Tab 2: Codes — click Run in Browser to execute) and run the script and tests:
python week05_math_data_science_and_classical_ml/day05_unsupervised_ml_kmeans_clustering_embeddings/src/kmeans_topic_clusterer.py
python -m unittest discover -s week05_math_data_science_and_classical_ml/day05_unsupervised_ml_kmeans_clustering_embeddings/tests -v
In Simple English: Grouping Books on Shelves Without Labels (K-Means)
Suppose someone drops 5,000 unlabeled customer support tickets on your desk and says "Sort these into 4 piles by topic!" K-Means works in a simple 3-step dance:
- Drop 4 empty boxes (centroids ) randomly onto the floor.
- Assign: Walk through every ticket and put it into whichever box is closest to its embedding point.
- Recenter: Move each box to the exact middle (average) of all tickets sitting inside it. Repeat steps 2–3 until the boxes stop moving!
AI Use Case: Before building expensive RAG taxonomies, engineers run K-Means on support queries to discover the top 10 recurring user problems automatically! Check Tab 2 (Codes) to run our K-Means clusterer right in your browser.