K-Means Clustering Algorithm from Scratch
K-Means partitions data into $K$ distinct clusters without prior labels. It alternates between assigning points to the nearest centroid and recomputing centroids as the arithmetic mean of assigned points until convergence 🎯.
points = [1.0, 2.0, 1.5, 8.0, 9.0, 8.5]
centroids = [1.0, 9.0]
for _ in range(5):
clusters = {0: [], 1: []}
for p in points:
idx = 0 if abs(p - centroids[0]) <= abs(p - centroids[1]) else 1
clusters[idx].append(p)
for i in clusters:
if clusters[i]:
centroids[i] = sum(clusters[i]) / len(clusters[i])
print(f"Converged Centroids: {[round(c, 1) for c in centroids]} 🎯")
# 52. K-Means Clustering Algorithm from Scratch K-Means partitions data into $K$ distinct clusters without prior labels. It alternates between assigning points to the nearest centroid and recomputing centroids as the arithmetic mean of assigned points until convergence 🎯. ```python points = [1.0, 2.0, 1.5, 8.0, 9.0, 8.5] centroids = [1.0, 9.0] for _ in range(5): clusters = {0: [], 1: []} for p in points: idx = 0 if abs(p - centroids[0]) <= abs(p - centroids[1]) else 1 clusters[idx].append(p) for i in clusters: if clusters[i]: centroids[i] = sum(clusters[i]) / len(clusters[i]) print(f"Converged Centroids: {[round(c, 1) for c in centroids]} 🎯") ``` Because K-Means optimizes sum-of-squared distances, bad initial centroid placement can trap it in local minima. Production libraries run multiple random restarts (K-Means++) to guard against unlucky initial seeds ⚡. --- ### Question Why is K-Means clustering classified as an unsupervised machine learning algorithm? 🧠- [ ] a. It discovers latent group structures directly from unlabelled input features without ground-truth targets 🎯 - [ ] b. It runs completely without CPU execution using autonomous kernel scheduler hooks - [ ] c. It guarantees finding the global mathematical optimum in exactly one iteration step - [ ] d. It mandates that every training sample must have an authoritative human-annotated label --- ### Challenge Given points `[1.0, 2.0, 1.5, 8.0, 9.0, 8.5]` and initial centroids `[1.0, 9.0]`, run 5 iterations of K-Means. Print the sorted centroids rounded to 1 decimal place. 🎯 <details> <summary>Solution</summary> ```python points = [1.0, 2.0, 1.5, 8.0, 9.0, 8.5] centroids = [1.0, 9.0] for _ in range(5): clusters = {0: [], 1: []} for p in points: idx = 0 if abs(p - centroids[0]) <= abs(p - centroids[1]) else 1 clusters[idx].append(p) for i in clusters: if clusters[i]: centroids[i] = sum(clusters[i]) / len(clusters[i]) print([round(c, 1) for c in sorted(centroids)]) ``` </details>