Initialization
- Machine Learning course
- Intermediate
- Lesson 54/112
- 5 min
KMeans uses k-means++ as the default initialization method.
Why initialisation matters
Lloyd's algorithm depends on the initial centres. A bad start (for example two centres landing in the same real cluster) gets stuck in a local optimum: one cluster is split in two, two clusters are merged.
for rs in range(3):
km = KMeans(n_clusters=5, init="random", n_init=1, random_state=rs).fit(X)
print(rs, round(km.inertia_)) # a different result per seedThat's why the initialisation method and the number of repeats are KMeans's most important settings.
k-means++
scikit-learn's default initialisation is k-means++: the first centre is chosen at random, and each following centre is chosen from points far from the existing centres, with probability proportional to the squared distance. The centres start spread out:
km = KMeans(n_clusters=5, init="k-means++", n_init=1, random_state=0)
# init="random": uniformly at random among the examplesk-means++ gives both a better solution and usually fewer rounds.
Practice Compare initialisations
For seeds 0-9, train a n_init=1 KMeans (5 clusters) with init="random" and with init="k-means++". Print the worst (largest) inertia for each method as integers with round on one line (random first).
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)
# 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)
kotu = []
for init in ["random", "k-means++"]:
kotu.append(round(max(KMeans(n_clusters=5, init=init, n_init=1, random_state=rs).fit(X).inertia_ for rs in range(10))))
print(*kotu)Expected output
2224 2145
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the typical failure of random initialisation?
- Raising an error
- Two centres landing in the same cluster Correct answer
- The algorithm not converging
- Centres being deleted
Explanation: In that case one real cluster is split and two clusters elsewhere are merged.
2 Initialise the centres spread out.
km = KMeans(n_clusters=5, init=____)
km = KMeans(n_clusters=5, init="k-means++")
Explanation: "k-means++" is the default.
3 Match the initialisations.
| Term | Meaning |
|---|---|
init="random" |
Uniform random examples |
init="k-means++" |
Choice proportional to distance |
bad initialization |
Local optimum |
n_iter_ |
Rounds needed |