Machine Learning
random_state
random_state makes KMeans results reproducible.
random_state and reproducibility
KMeans's initialisation is random; without random_state different centres and label numbers can come out on every run. For reports and tests, the seed must be fixed:
a = KMeans(n_clusters=5, n_init=10, random_state=42).fit(X).labels_
b = KMeans(n_clusters=5, n_init=10, random_state=42).fit(X).labels_
print((a == b).all()) # True
Even if the label numbers change with different seeds, ARI = 1 when the clusters are the same; this shows the solution is stable.
A stability test
The way to tell whether a clustering is reliable is to rerun it on different seeds or subsamples of the data and see how well the results agree:
ref = KMeans(n_clusters=k, n_init=10, random_state=0).fit_predict(X)
for rs in range(1, 5):
diger = KMeans(n_clusters=k, n_init=10, random_state=rs).fit_predict(X)
print(rs, adjusted_rand_score(ref, diger))
With a wrong k the results change from seed to seed; with the right k they are stable. This is an extra hint for choosing k.
Exercise Stability
For k = 5 and k = 8, compare the labels of seed 0 with those of seeds 1-4 using ARI, with n_init=1. For each k print the mean ARI with round(..., 3) together with k on one line.
Show example solution
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.metrics import adjusted_rand_score
for k in [5, 8]:
ref = KMeans(n_clusters=k, n_init=1, random_state=0).fit_predict(X)
ari = [adjusted_rand_score(ref, KMeans(n_clusters=k, n_init=1, random_state=rs).fit_predict(X)) for rs in range(1, 5)]
print(k, round(np.mean(ari), 3))
Test yourself
-
If ARI is 1 with different seeds but the label arrays differ, what has happened?
- There is a bug
- ARI was computed wrong
- The same clusters were found, only the numbers changed
- The clusters are completely different
Show answer
The same clusters were found, only the numbers changed — ARI is independent of the numbering.
-
Make the result reproducible.
km = KMeans(n_clusters=5, n_init=10, ____=42)
Show answer
km = KMeans(n_clusters=5, n_init=10, random_state=42)
-
Match the situations.
- Same seed
- Same labels
- Different seed, ARI = 1
- Stable solution
- Different seed, low ARI
- Unstable, k suspicious
- no random_state
- Not reproducible
Next lesson: Summary - KMeans