ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)
Set your Publisher ID in src/config/ads.ts to display live ads.
Machine Learning with Python Tutorial• 7 min read
K-Means Clustering & The Elbow Method
Centroids, inertia, Voronoi partitions, and silhouette scores
K-Means is an unsupervised clustering algorithm that partitions unlabeled data points into K distinct groups by iteratively updating centroid positions.
The K-Means Convergence Algorithm
1Initialize
K cluster centroids randomly (or via k-means++).2Assignment Step: Assign each data point to its nearest centroid using Euclidean distance.
3Update Step: Recalculate centroids as the arithmetic mean of all points assigned to that cluster.
4Repeat until centroids stop moving (convergence).
The Elbow Method: Plotting Inertia (within-cluster sum of squares) vs K reveals an "elbow" point representing the optimal cluster count.
Customer Segmentation with K-Means
from sklearn.cluster import KMeans
import numpy as np
# Customer purchase data: [annual_spend, visit_frequency]
X = np.array([
[12000, 45], [14000, 50], [11500, 48], # High value
[1200, 6], [1500, 8], [900, 5], # Low value
[5000, 22], [6200, 25], [4800, 20] # Moderate
])
kmeans = KMeans(n_clusters=3, init="k-means++", random_state=42)
clusters = kmeans.fit_predict(X)
print("Assigned cluster IDs:", clusters)
print("Centroid coordinates:
", np.round(kmeans.cluster_centers_, 1))Groups customers into natural demographic clusters without labels.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)
Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
In K-Means clustering, what visual heuristic is commonly used to choose the optimal number of clusters K?