CodeBlanks Logo Icon CodeBlanks

Summary - Hierarchical clustering

In this group you learned hierarchical clustering and dendrograms.

Summary: hierarchical clustering

In this group:

  • Agglomerative clustering starts from every example and merges the closest clusters; the result is a tree.
  • The linkage matrix and the dendrogram; a large distance jump is a good cut point.
  • fcluster (maxclust/distance) and AgglomerativeClustering (n_clusters/distance_threshold).
  • Linkage types: single (chains), complete, average, ward (default).
  • It is deterministic; slow on large data; for new examples a classifier is trained on the labels.

Practice Dendrogram and cut

Compute the Ward linkage on the small data, draw the dendrogram and save it. Cut at the middle of the largest jump between consecutive merge distances ((Z[i, 2] + Z[i + 1, 2]) / 2, i = np.diff(Z[:, 2]).argmax()) and print the labels with fcluster(..., criterion="distance").

Starter code

model.py
import numpy as np
from scipy.cluster.hierarchy import linkage, fcluster

X = np.array([[0, 0], [0, 1], [1, 0], [5, 5], [5, 6], [6, 5], [12, 0], [12, 1]])
from scipy.cluster.hierarchy import dendrogram
import matplotlib.pyplot as plt

# buraya yaz

Sample solution

model.py
import numpy as np
from scipy.cluster.hierarchy import linkage, fcluster

X = np.array([[0, 0], [0, 1], [1, 0], [5, 5], [5, 6], [6, 5], [12, 0], [12, 1]])
from scipy.cluster.hierarchy import dendrogram
import matplotlib.pyplot as plt

Z = linkage(X, method="ward")
fig, ax = plt.subplots()
dendrogram(Z, ax=ax)
fig.savefig("agac.png")
i = np.diff(Z[:, 2]).argmax()
esik = (Z[i, 2] + Z[i + 1, 2]) / 2
print(fcluster(Z, t=esik, criterion="distance"))

Expected output

[2 2 2 3 3 3 1 1]

Questions and answers

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

1 Which linkage produces clusters most similar to KMeans?

  • single
  • average
  • complete
  • ward Correct answer

Explanation: Like KMeans, Ward picks the merge that least increases the within-cluster squared distance.

2 Draw the dendrogram.

Question
____(Z, ax=ax)
Answer
dendrogram(Z, ax=ax)

Explanation: dendrogram lives in scipy.cluster.hierarchy and draws with matplotlib.

3 Match the tools.

Term Meaning
linkage Merge matrix
dendrogram Tree drawing
fcluster Cutting the tree
AgglomerativeClustering scikit-learn version

All lessons in Intermediate