CodeBlanks Logo Icon CodeBlanks

Summary - KMeans

In this group you learned KMeans parameters and properties.

Summary: KMeans

In this group:

  • Lloyd's algorithm: assignment + update; inertia never rises but can get stuck in a local optimum.
  • k-means++ initialises with spread-out centres; n_init picks the best of several starts.
  • inertia_ depends on scale and always falls with k; the silhouette, Calinski-Harabasz and Davies-Bouldin complement it.
  • random_state gives reproducibility; ARI across seeds measures stability.
  • MiniBatchKMeans for large data.

Practice KMeans and MiniBatch

Train KMeans(n_clusters=5, n_init=10, random_state=0) and MiniBatchKMeans(n_clusters=5, batch_size=100, n_init=3, random_state=0) on the same data. Print the two inertias as integers with round on one line, then the ARI of the two labellings with round(..., 3).

Starter code

model.py
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans

X, _ = make_blobs(n_samples=500, centers=5, cluster_std=1.0, random_state=3)
from sklearn.cluster import MiniBatchKMeans
from sklearn.metrics import adjusted_rand_score

# buraya yaz

Sample solution

model.py
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans

X, _ = make_blobs(n_samples=500, centers=5, cluster_std=1.0, random_state=3)
from sklearn.cluster import MiniBatchKMeans
from sklearn.metrics import adjusted_rand_score

km = KMeans(n_clusters=5, n_init=10, random_state=0).fit(X)
mb = MiniBatchKMeans(n_clusters=5, batch_size=100, n_init=3, random_state=0).fit(X)
print(round(km.inertia_), round(mb.inertia_))
print(round(adjusted_rand_score(km.labels_, mb.labels_), 3))

Expected output

1000 1002
1.0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If KMeans results vary a lot from seed to seed, what should be tried first?

  • Deleting random_state
  • Increasing n_init and reviewing k Correct answer
  • Setting max_iter to 1
  • Removing scaling

Explanation: More starts reduce local optima; instability can also point to a wrong k.

2 Use mini-batch KMeans for large data.

Question
mb = ____(n_clusters=5, batch_size=256)
Answer
mb = MiniBatchKMeans(n_clusters=5, batch_size=256)

Explanation: MiniBatchKMeans uses only a batch of data in each round.

3 Match the concepts.

Term Meaning
Lloyd Assignment and update
k-means++ Spread-out start
n_init Choosing the best start
ARI across seeds Stability

All lessons in Intermediate