What if you have data but no answers? Unsupervised learning finds the labels for you. K-Means is the algorithm that brings order to unlabeled chaos.
1The Centroid Logic
K-Means works by placing 'K' number of points called Centroids in your feature space. It then assigns every data point to its nearest centroid. After assignment, it calculates the average of those points and moves the centroid to that new 'mean' position.
2Finding the Elbow
A common question is: 'How do I know what K should be?'. The Elbow Method provides the answer. By plotting the Inertia (sum of squared distances) against the number of clusters, you'll see a sharp drop that eventually levels off. The 'elbow' of this curve is the optimal K.
3The Limitations
K-Means is incredibly fast but has weaknesses. It assumes clusters are spherical and similar in size. It also struggles with noise and outliers, which can pull centroids away from the true center of the data.
4Step-by-Step Breakdown
Welcome to Unsupervised Learning. In K-Means, we don't have target labels. The algorithm must discover hidden groupings on its own.
First, we import the KMeans class from Scikit-Learn's cluster module. We also need numerical data to group.
Checkpoint: In K-Means, what does the 'K' specifically stand for?
- βNumber of Kernels
- βNumber of Clusters
When instantiating, we specify n_clusters. The algorithm will randomly place 'centroids' and move them until points are grouped optimally.
K-Means works by minimizing 'Inertia'βthe sum of squared distances of samples to their closest cluster center.
How do we choose the best K? We use the 'Elbow Method'. We plot inertia for different K values and look for the 'elbow' point.
Checkpoint: At what point does the K-Means algorithm stop its iterative process?
- βWhen centroids stop moving
- βWhen all points are used
Congratulations! You've mastered centroid-based clustering. You can now categorize unlabeled datasets with ease.
Run Real K-Means Clustering. Finish clustering two well-separated point pairs into 2 groups.
Level Up π
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Label Cluster Meaning in Text, Not Just Color-Coded Scatter Points
A K-Means result visualized purely as colored dots on a scatter plot conveys nothing to a screen reader β always follow up with a text summary interpreting each cluster ('Cluster 1: high-spend, infrequent buyers'), turning an abstract numeric grouping into an actionable, readable description.
<p>Cluster 0: high spend, low frequency ("whales"). Cluster 1: frequent, low-value purchases.</p>SEO Implications
- 1
Cluster Assignments Are Runtime Output, Not Page Content
The specific centroids and cluster labels K-Means produces exist only for one run against one dataset β this page's SEO value is its own explanation of centroid-based clustering and the Elbow Method, not any specific clustering result shown in the examples.
Best Practices
Always Scale Features Before Running K-Means
K-Means uses Euclidean distance to assign points to centroids, so a feature with a much larger numeric range (like income vs. age) will dominate the distance calculation and skew clusters toward that one dimension. Apply StandardScaler before fitting, exactly as you would for KNN.
Run K-Means Multiple Times with Different Initializations
K-Means' result depends on where centroids are randomly initialized, and a bad initialization can converge to a poor local optimum. Scikit-Learn's n_init parameter (default 10) automatically reruns the algorithm with different starting points and keeps the best result β don't override it to 1 for the sake of speed unless you understand the tradeoff.
Frequent Bugs
Choosing K based on the Elbow Method's inertia curve, but misreading a gradual curve as having a clear elbow.
Real-world data often produces a smooth, gradually decreasing inertia curve with no obvious 'elbow', unlike the clean examples in tutorials β forcing a clusters count from an ambiguous curve produces an arbitrary, poorly justified choice. When the elbow isn't clear, use a complementary method like the Silhouette Score to validate the choice of K instead of relying on inertia alone.
Real-World Examples
Customer Segmentation for a Marketing Campaign
A retail company runs K-Means on customer (recency, frequency, monetary value) data to identify distinct buyer segments β the resulting clusters (e.g., 'loyal high-spenders', 'one-time bargain shoppers', 'lapsed customers') directly inform three separate, targeted email marketing campaigns instead of one generic message sent to everyone.
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=3, n_init=10)
segments = kmeans.fit_predict(X_scaled) # RFM features