Machine Learning · Chapter 17 of 40

K-Means Clustering

K-MEANS groups data into K clusters by iteratively assigning points to the nearest CENTROID and updating centroids to the cluster mean.

Unsupervised — no labels needed.

Example 1 (python)
from sklearn.cluster import KMeans
k = KMeans(n_clusters=3, n_init=10, random_state=42).fit(X)
print(k.labels_[:10])
Output
[0 2 1 0 1 2 0 1 2 0]

Each point gets a cluster ID.

Example 2 (python)
# Use the elbow method to pick K

Plot inertia vs K and pick the elbow.

Key points

  • Unsupervised — no labels.
  • Groups by distance to centroid.
  • You choose K.
  • Sensitive to feature scaling and initialization.
💡 Note: Set `n_init=10` (or higher) to avoid landing in a bad local minimum.

📝 Quick Quiz

1. K-means is:

2. The 'K' is:

3. Elbow method helps choose: