Course contentsShow
← Machine Learning and Deep Learning
Lesson 362 of 3,5388. Unsupervised Learning: ClusteringFree lesson

Implementing Hierarchical Clustering with Scikit-learn

Using AgglomerativeClustering, selecting linkage criteria, plotting dendrograms, and cutting trees for predictions.

Implementing Hierarchical Clustering with Scikit-learn

What you'll learn: How to use Scikit-learn's tools to perform hierarchical clustering, visualize dendrograms, and extract cluster labels from your data.

Putting Hierarchical Clustering into Practice

Now that you understand the theory behind agglomerative clustering, linkage criteria, and dendrograms, it's time to implement it using Scikit-learn. The AgglomerativeClustering class makes this straightforward.

Using AgglomerativeClustering

The main tool is AgglomerativeClustering. You initialize it by specifying:

  • n_clusters: How many clusters you want (equivalent to "cutting" the dendrogram at a certain height)
  • linkage: The linkage criterion ('ward', 'complete', 'average', or 'single')
from sklearn.cluster import AgglomerativeClustering

# Create clustering model with Ward linkage
model = AgglomerativeClustering(n_clusters=3, linkage='ward')

# Fit and predict cluster labels
labels = model.fit_predict(X)

Visualizing Dendrograms

Scikit-learn doesn't directly create dendrograms, but you can use scipy.cluster.hierarchy:

from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt

# Create linkage matrix
Z = linkage(X, method='ward')

# Plot dendrogram
dendrogram(Z)
plt.show()

The dendrogram shows you the entire clustering tree, helping you decide where to "cut" (choose n_clusters).

Making Predictions

After fitting, use the labels_ attribute to get cluster assignments for your data. Unlike K-Means, hierarchical clustering doesn't naturally predict on new data, but you can examine the fitted labels to understand groupings.

Key Takeaway: Scikit-learn's AgglomerativeClustering implements hierarchical clustering with various linkage options, while scipy's dendrogram visualization helps you choose the optimal number of clusters by examining the tree structure.