random_state
- Machine Learning course
- Intermediate
- Lesson 57/112
- 5 min
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()) # TrueEven 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.
Practice 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.
Starter code
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
# buraya yazSample 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))Expected output
5 1.0 8 0.801
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 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 Correct answer
- The clusters are completely different
Explanation: ARI is independent of the numbering.
2 Make the result reproducible.
km = KMeans(n_clusters=5, n_init=10, ____=42)
km = KMeans(n_clusters=5, n_init=10, random_state=42)
Explanation: random_state fixes the initialisation randomness.
3 Match the situations.
| Term | Meaning |
|---|---|
Same seed |
Same labels |
Different seed, ARI = 1 |
Stable solution |
Different seed, low ARI |
Unstable, k suspicious |
no random_state |
Not reproducible |