Agglomerative Clustering and Dendrogram Visualization using Scikit-Learn

 

Experiment

Agglomerative Clustering and Dendrogram Visualization using Scikit-Learn


๐ŸŽฏ Objective

To implement Agglomerative Hierarchical Clustering using scikit-learn and visualize the clustering using a dendrogram.


๐Ÿ“˜ Theory

๐Ÿ”น Hierarchical Clustering

Hierarchical clustering creates a tree-like structure of clusters:

  • Agglomerative (Bottom-Up) → merges closest clusters step-by-step

๐Ÿ”น Agglomerative Clustering in Scikit-Learn

The class AgglomerativeClustering:

  • Does not require manual distance computation
  • Supports linkage methods:
    • single
    • complete
    • average
    • ward (default)

๐Ÿ”น Linkage Methods

MethodDescription
Single    Minimum distance
Complete    Maximum distance
Average    Mean distance
Ward    Minimizes variance

๐Ÿ”น Dendrogram

  • Visual representation of cluster merging
  • Height = distance between clusters
  • Helps decide number of clusters

๐Ÿงพ Sample Dataset

Point    X    Y
A    1    1
B    2    1
C    4    3
D    5    4
E    8    7

๐Ÿ’ป Program (Python Code)

import numpy as np import matplotlib.pyplot as plt from sklearn.cluster import AgglomerativeClustering from scipy.cluster.hierarchy import dendrogram, linkage # ------------------------------- # Step 1: Dataset # ------------------------------- X = np.array([ [1, 1], [2, 1], [4, 3], [5, 4], [8, 7] ]) labels = ['A', 'B', 'C', 'D', 'E'] # ------------------------------- # Step 2: Apply Agglomerative Clustering # ------------------------------- model = AgglomerativeClustering( n_clusters=3, # choose number of clusters linkage='single' # same as manual experiment ) cluster_labels = model.fit_predict(X) # ------------------------------- # Step 3: Plot Clusters # ------------------------------- plt.figure() plt.scatter(X[:, 0], X[:, 1], c=cluster_labels) for i, txt in enumerate(labels): plt.text(X[i][0]+0.1, X[i][1]+0.1, txt) plt.title("Agglomerative Clustering (Scikit-Learn)") plt.xlabel("X") plt.ylabel("Y") plt.show() # ------------------------------- # Step 4: Dendrogram # ------------------------------- Z = linkage(X, method='single') plt.figure() dendrogram(Z, labels=labels) plt.title("Dendrogram (Scipy)") plt.xlabel("Data Points") plt.ylabel("Distance") plt.show() # ------------------------------- # Step 5: Output # ------------------------------- print("Cluster Labels:", cluster_labels)

๐Ÿ“Š Output

๐Ÿ”น Cluster Labels (Example)

Cluster Labels: [2 2 0 0 1]

๐Ÿ”น Cluster Interpretation

  • Cluster 1 → A, B, C, D
  • Cluster 2 → C,D
  • Cluster 3 → E

๐Ÿ”น Dendrogram

  • Shows merging sequence:
    1. A + B
    2. C + D
    3. (AB) + (CD)
    4. (ABCD) + E







๐Ÿ“Œ Result

Agglomerative clustering was successfully performed using scikit-learn, and the hierarchical structure was visualized using a dendrogram.

  • Library implementation simplifies clustering
  • Dendrogram provides clear visualization
  • Results match manual implementation

Comments

Popular posts from this blog

Machine Learning Lab PCCSL508 Semester 5 KTU CS 2024 Scheme manual - Dr Binu V P

Lab Assignment-2

Lab Assignment-1