CodeBlanks Logo Icon CodeBlanks

n_init

n_init sets the number of different initializations for KMeans.

n_init

n_init is how many times KMeans runs with different starts; the solution with the lowest inertia is kept. It is the simplest and most effective insurance against local optima:

km = KMeans(n_clusters=5, n_init=10, random_state=0).fit(X)
# 10 separate starts, the best is kept

In newer scikit-learn versions the default is n_init="auto": 1 run with k-means++, 10 runs with random initialisation.

Time and quality

As n_init grows the time rises linearly while the gain diminishes. On large datasets MiniBatchKMeans runs much faster by updating with a small part of the data in each round:

from sklearn.cluster import MiniBatchKMeans
mb = MiniBatchKMeans(n_clusters=5, batch_size=256, n_init=3, random_state=0).fit(X)
print(round(mb.inertia_))   # close to KMeans, much faster

With millions of examples MiniBatchKMeans may be the only practical option.

Practice The effect of n_init

Train KMeans (5 clusters, random_state=2) with init="random" for n_init = 1, 5, 20. On each line print n_init and the inertia as an integer with round.

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)

# 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)

for n in [1, 5, 20]:
    km = KMeans(n_clusters=5, init="random", n_init=n, random_state=2).fit(X)
    print(n, round(km.inertia_))

Expected output

1 2221
5 1000
20 1000

Questions and answers

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

1 With <code>n_init=10</code>, which solution is returned?

  • The one with the lowest inertia Correct answer
  • The last run
  • Their average
  • The first run

Explanation: Each run is independent; the best is chosen.

2 Run the algorithm with 20 different starts.

Question
km = KMeans(n_clusters=5, ____=20, random_state=0)
Answer
km = KMeans(n_clusters=5, n_init=20, random_state=0)

Explanation: n_init is the number of starts; max_iter is the round limit within one run.

3 Match the parameters.

Term Meaning
n_init Number of starts
max_iter Round limit
MiniBatchKMeans Fast mini-batch version
batch_size Batch size

All lessons in Intermediate