CodeBlanks Logo Icon CodeBlanks

Advantages

Hierarchical clustering shows cluster structure without specifying k in advance.

Advantages of hierarchical clustering

Hierarchical clustering stands out when:

  • k is unknown: the dendrogram shows the structure at different levels together.
  • A nested structure is meaningful: taxonomies like species-genus-family, product categories.
  • Small to medium data: it works comfortably up to a few thousand examples.
  • It is deterministic: there is no random initialisation; every run gives the same result.

Its drawbacks: slowness on large data and not being applicable to new examples.

A classifier on the cluster labels

To assign new examples to clusters, a common pattern is training a classifier on the labels found by hierarchical clustering:

etiket = AgglomerativeClustering(n_clusters=3).fit_predict(X)
from sklearn.neighbors import KNeighborsClassifier
atayici = KNeighborsClassifier(n_neighbors=5).fit(X, etiket)
print(atayici.predict(X_yeni))

That way the clustering is done once and later assignments are fast and consistent.

Practice Carry the clustering to new data

Find the labels on the synthetic data with AgglomerativeClustering(n_clusters=3) and train a KNeighborsClassifier(n_neighbors=5) on them. Print with (... == ...).all() that the assigner's predictions for X[:5] match the original labels. Then run the same clustering again and print that the labels didn't change.

Starter code

model.py
from sklearn.datasets import make_blobs
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import adjusted_rand_score

X, y = make_blobs(n_samples=300, centers=3, cluster_std=[0.5, 1.5, 0.8], random_state=4)
from sklearn.neighbors import KNeighborsClassifier

# buraya yaz

Sample solution

model.py
from sklearn.datasets import make_blobs
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import adjusted_rand_score

X, y = make_blobs(n_samples=300, centers=3, cluster_std=[0.5, 1.5, 0.8], random_state=4)
from sklearn.neighbors import KNeighborsClassifier

etiket = AgglomerativeClustering(n_clusters=3).fit_predict(X)
atayici = KNeighborsClassifier(n_neighbors=5).fit(X, etiket)
print((atayici.predict(X[:5]) == etiket[:5]).all())
print((AgglomerativeClustering(n_clusters=3).fit_predict(X) == etiket).all())

Expected output

True
True

Questions and answers

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

1 Why is random_state not needed in hierarchical clustering?

  • The algorithm is deterministic Correct answer
  • There are always 2 clusters
  • The data isn't shuffled
  • It is needed

Explanation: At every step the closest pair is chosen; there is no randomness.

2 Train an assigner on the cluster labels.

Question
atayici = KNeighborsClassifier(n_neighbors=5).fit(X, ____)
Answer
atayici = KNeighborsClassifier(n_neighbors=5).fit(X, etiket)

Explanation: The cluster labels become the classifier's target.

3 Match the situations.

Term Meaning
k unknown Dendrogram
Nested categories Hierarchy
Millions of samples MiniBatchKMeans
Assigning a new sample KNN on labels

All lessons in Intermediate