Machine Learning1 code example
⚡ +100 XP

K-Means Clustering

1

What is Clustering?

Clustering is an unsupervised task that groups similar data points together without labels. Goal: high intra-cluster similarity, low inter-cluster similarity.

2

K-Means Algorithm

1. Choose K (number of clusters) 2. Randomly initialize K centroids 3. Assign each point to the nearest centroid (Euclidean distance) 4. Recompute centroids as the mean of assigned points 5. Repeat steps 3–4 until centroids stop moving (convergence) Time complexity: O(n·K·d·iterations)

3

Choosing K: The Elbow Method

Plot inertia (sum of squared distances to nearest centroid) vs K. The 'elbow' — where adding more clusters gives diminishing returns — is the optimal K. Alternative: Silhouette Score measures how similar a point is to its own cluster vs other clusters. Range: [−1, 1]. Higher is better.

4

K-Means + Elbow Method

💡

K-Means assumes spherical clusters of equal size. For complex shapes, use DBSCAN or Gaussian Mixture Models.

Finished reading? Mark it complete to earn your XP.