Lesson 8 of 15
K-Means Clustering
K-Means Clustering
K-means is the most widely used clustering algorithm. It partitions data points into clusters by iterating two steps:
Step 1 — Assignment
Assign each point to the nearest centroid:
Step 2 — Update
Move each centroid to the mean of its assigned points:
Inertia (Within-Cluster Sum of Squares)
A common quality metric is inertia — the sum of squared distances from each point to its assigned centroid:
Lower inertia means tighter, more compact clusters.
Your Task
Implement:
assign_clusters(X, centroids)→ list of cluster indices (one per point)update_centroids(X, assignments, k)→ new centroids as mean of assigned pointskmeans_inertia(X, assignments, centroids)→ total within-cluster sum of squares
Python runtime loading...
Loading...
Click "Run" to execute your code.