ML Notes
Master K-Means clustering with centroid initialization, convergence criteria, elbow method, silhouette analysis, and practical implementation for customer segmentation.
K-Means is the most widely used clustering algorithm in machine learning, and for good reason — it is fast, intuitive, and scales well to large datasets. The algorithm partitions data into K groups by finding K center points (centroids) and assigning each data point to its nearest centroid. If you have ever used customer segmentation, color quantization in image compression, or document grouping, you have likely benefited from K-Means under the hood.
The Core Intuition
Imagine you are a pizza delivery company opening K new stores in a city. You want to place each store at a location that minimizes the average delivery distance for all customers in its zone. The optimal store locations are the centroids, and each customer is "assigned" to their nearest store. K-Means solves exactly this optimization problem — finding K center points that minimize the total distance from all data points to their assigned center.
This is formally called minimizing the Within-Cluster Sum of Squares (WCSS or inertia): for each cluster, sum the squared distances from every member point to the cluster centroid, then sum across all clusters.
The Algorithm: Simple Yet Powerful
K-Means follows a straightforward iterative procedure that alternates between two steps:
Step 1 — Assignment: Assign each data point to the nearest centroid (using Euclidean distance)
Step 2 — Update: Recalculate each centroid as the mean of all points assigned to it
Repeat these two steps until centroids stop moving (convergence) or a maximum number of iterations is reached. The algorithm is guaranteed to converge, though it may converge to a local minimum rather than the global optimum.
| a. Assign each point to nearest centroid | creates K clusters |
| Convergence | centroids move less than tolerance (default: 1e-4) |
| Typical iterations | 10-50 for most datasets |
Implementation from Scratch and with Sklearn
The Initialization Problem and K-Means++
A critical detail: K-Means results depend heavily on the initial centroid positions. Bad initialization can lead to suboptimal clustering where the algorithm converges to a local minimum far from the global optimum. Two solutions exist:
Multiple restarts (n_init): Run the algorithm multiple times with different random initializations and keep the result with lowest inertia. Sklearn defaults to n_init=10.
K-Means++ initialization: Instead of purely random initialization, K-Means++ selects initial centroids that are spread apart. The first centroid is random, and each subsequent centroid is chosen with probability proportional to its squared distance from the nearest existing centroid. This dramatically reduces the chance of poor initialization and is sklearn's default.
Choosing K: The Elbow Method and Silhouette Analysis
The hardest part of K-Means is choosing the number of clusters K. Two popular methods help:
Elbow Method
Plot WCSS (inertia) against different K values. As K increases, inertia always decreases (more clusters = points closer to centroids). Look for an "elbow" — a point where the rate of decrease sharply changes. Beyond this point, adding more clusters provides diminishing returns.
Silhouette Score
The silhouette score measures how similar a point is to its own cluster compared to neighboring clusters. Ranges from -1 (wrong cluster) to +1 (well-matched). Higher average silhouette = better clustering. Unlike the elbow method, the silhouette score has a clear optimum (the maximum).
Limitations of K-Means
Understanding K-Means limitations helps you know when to use alternatives:
Spherical cluster assumption: K-Means minimizes distance to centroids, naturally producing spherical (globular) clusters. It fails for elongated, ring-shaped, or irregular clusters — use DBSCAN or spectral clustering instead.
Sensitivity to outliers: Outliers pull centroids away from the true cluster center. Consider removing outliers first or using K-Medoids (which uses actual data points as centers instead of means).
Equal-size bias: K-Means tends to produce clusters of similar sizes because it optimizes total distance. If your clusters have vastly different sizes, the algorithm may split a large natural cluster to balance assignments.
Fixed K: You must specify K in advance. The elbow method helps but is sometimes ambiguous when there is no clear elbow.
Real-World Application: Customer Segmentation
Mini-Batch K-Means for Large Datasets
Standard K-Means recomputes distances to ALL points every iteration, making it slow for millions of data points. Mini-Batch K-Means uses random subsets (mini-batches) of data at each step, trading a tiny bit of accuracy for dramatically faster convergence — typically 3-5x faster than standard K-Means with negligible quality loss.
from sklearn.cluster import MiniBatchKMeans
# For datasets with millions of rows
mbk = MiniBatchKMeans(n_clusters=4, batch_size=256, random_state=42)
labels = mbk.fit_predict(X_scaled)Key Takeaways
K-Means partitions data into K spherical clusters by iteratively refining centroid positions. Always use K-Means++ initialization, standardize your features, and determine K using the elbow method combined with silhouette analysis. Remember its limitations (spherical assumption, outlier sensitivity, fixed K) and reach for DBSCAN or hierarchical clustering when these assumptions are violated. Despite its simplicity, K-Means remains the first clustering algorithm to try on any new dataset because of its speed, scalability, and ease of interpretation.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for K-Means Clustering.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, clustering, kmeans, k-means clustering
Related Machine Learning Topics