š Introduction
Hierarchical Clustering is an unsupervised machine learning algorithm used to group similar observations into clusters by creating a hierarchy of nested clusters. Unlike K-Means, it does not require specifying the number of clusters in advance. The clustering process is represented using a dendrogram, a tree-like diagram that illustrates how clusters are formed or divided.
Information
šÆ Learning Objectives
- Understand hierarchical clustering.
- Learn the difference between Agglomerative and Divisive clustering.
- Understand linkage methods and distance metrics.
- Interpret dendrograms for cluster selection.
š What is Hierarchical Clustering?
Hierarchical Clustering builds a hierarchy of clusters rather than producing a single partition of the dataset. The resulting hierarchy can be visualized using a dendrogram, allowing users to choose the desired number of clusters by cutting the tree at an appropriate level.
| Characteristic | Hierarchical Clustering |
|---|---|
| Learning Type | Unsupervised |
| Output | Hierarchy of clusters |
| Requires K in Advance | No |
| Visualization | Dendrogram |
š³ Types of Hierarchical Clustering
Agglomerative Clustering (Bottom-Up)
Starts with each observation as its own cluster. At each step, the two closest clusters are merged until only one cluster remains or a stopping criterion is reached.
Divisive Clustering (Top-Down)
Begins with all observations in one cluster and recursively splits clusters into smaller groups until each observation forms its own cluster or another stopping condition is met.
āļø Agglomerative Clustering Algorithm
Treat every observation as an individual cluster.
Compute distances between all clusters.
Merge the two nearest clusters.
Update the distance matrix.
Repeat until all observations belong to one cluster or the desired number of clusters is reached.
š Linkage Methods
Linkage methods determine how the distance between two clusters is calculated during the merging process.
| Linkage Method | Description |
|---|---|
| Single Linkage | Minimum distance between two clusters. |
| Complete Linkage | Maximum distance between two clusters. |
| Average Linkage | Average pairwise distance between clusters. |
| Ward Linkage | Minimizes the increase in within-cluster variance. |
š Distance Metrics
Euclidean Distance
Most commonly used for continuous numerical data.
Manhattan Distance
Suitable for grid-based or high-dimensional data.
Cosine Distance
Measures the angle between vectors rather than their absolute distance. Commonly used for text and document clustering.
š² Dendrogram
A dendrogram is a tree-like diagram that illustrates the sequence of cluster merges. The height at which two branches merge represents the distance between the corresponding clusters.
Remember
š Hierarchical Clustering vs K-Means
| Feature | Hierarchical Clustering | K-Means |
|---|---|---|
| Requires Number of Clusters | No | Yes |
| Output | Dendrogram | Cluster Centroids |
| Cluster Shape | Flexible | Prefers Spherical Clusters |
| Scalability | Less Scalable | Highly Scalable |
| Centroid Required | No | Yes |
šļø Important Hyperparameters
| Hyperparameter | Description |
|---|---|
| n_clusters | Desired number of output clusters. |
| linkage | Cluster linkage method. |
| metric | Distance metric used to measure similarity. |
| distance_threshold | Maximum linkage distance for cluster formation. |
š Evaluation Metrics
- Silhouette Score
- Davies-Bouldin Index
- Calinski-Harabasz Index
- Cophenetic Correlation Coefficient
āļø Advantages and Limitations
- No need to specify the number of clusters beforehand.
- Produces an intuitive dendrogram.
- Can discover clusters of different shapes and sizes.
- Works well for small and medium-sized datasets.
- Supports multiple linkage and distance methods.
- Computationally expensive for large datasets.
- Once clusters are merged or split, they cannot be undone.
- Sensitive to noise and outliers.
- Performance depends on the choice of linkage method and distance metric.
š Real-World Applications
| Application | Purpose |
|---|---|
| 𧬠Gene Expression Analysis | Group genes with similar expression patterns. |
| š Customer Segmentation | Identify groups of customers with similar behavior. |
| š Document Clustering | Organize related documents. |
| š Social Network Analysis | Discover communities within networks. |
| š¼ļø Image Segmentation | Partition images into meaningful regions. |
| š„ Medical Research | Identify patient groups with similar characteristics. |
š» Practical Example
Agglomerative Hierarchical Clustering Using Scikit-learn
from sklearn.cluster import AgglomerativeClustering
import numpy as np
# Sample data
X = np.array([
[1, 2], [1, 3], [2, 2],
[8, 8], [9, 8], [8, 9]
])
# Create model
model = AgglomerativeClustering(
n_clusters=2,
linkage="ward"
)
# Train and predict clusters
labels = model.fit_predict(X)
print("Cluster Labels:")
print(labels)ā ļø Common Mistakes
- Using Hierarchical Clustering on extremely large datasets.
- Ignoring feature scaling before computing distances.
- Selecting an inappropriate linkage method.
- Misinterpreting the dendrogram when choosing clusters.
- Ignoring the influence of outliers.