Menu

Hierarchical Clustering and DBSCAN

Hierarchical Clustering and DBSCAN

K-Means is useful for many clustering tasks, but it is not suitable for every dataset. Other algorithms offer different ways to discover groups.

Hierarchical Clustering

Hierarchical clustering builds a hierarchy of clusters. Its results can be represented using a tree-like diagram called a dendrogram.

There are two main approaches:

  • Agglomerative clustering: Starts with individual data points and repeatedly merges the closest clusters.
  • Divisive clustering: Starts with one large cluster and repeatedly splits it into smaller clusters.

Agglomerative clustering is commonly used in practical applications.

Example:

from sklearn.cluster import AgglomerativeClustering
from sklearn.preprocessing import StandardScaler
X = [
[20, 2],
[25, 3],
[22, 2],
[80, 8],
[85, 9],
[78, 7]
]
X_scaled = StandardScaler().fit_transform(X)
model = AgglomerativeClustering(n_clusters=2)
labels = model.fit_predict(X_scaled)
print("Cluster assignments:", labels)

Hierarchical clustering can help explore relationships between groups, although it may become computationally expensive for large datasets.

DBSCAN: Density-Based Clustering

DBSCAN stands for Density-Based Spatial Clustering of Applications with Noise. It groups closely packed data points and can identify isolated points as noise.

Unlike K-Means, DBSCAN does not require the number of clusters to be specified beforehand.

It uses two important parameters:

  • eps: The neighborhood distance within which points are considered close.
  • min_samples: The minimum number of nearby points required for a dense region.

DBSCAN can discover irregularly shaped clusters and is useful when a dataset contains noise.

Example:

from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
X = [
[20, 2],
[21, 2],
[22, 3],
[80, 8],
[81, 8],
[82, 9],
[150, 20]
]
X_scaled = StandardScaler().fit_transform(X)
model = DBSCAN(eps=0.8, min_samples=2)
labels = model.fit_predict(X_scaled)
print("Cluster assignments:", labels)

In DBSCAN, a label of -1 indicates a point classified as noise. The resulting clusters depend on the parameters and the density of the data.

Comparing Clustering Algorithms

Feature

K-Means

Hierarchical Clustering

DBSCAN

Number of clustersSpecified in advanceOften specified when cutting the hierarchyDiscovered from density
Cluster shapeBest for compact groupsDepends on linkage and distanceCan identify irregular shapes
Handles noiseSensitive to outliersDepends on the methodExplicitly identifies noise
Main limitationRequires KCan be expensive for large datasetsSensitive to density parameters

The best algorithm depends on the shape, size, density, and characteristics of the dataset.


Artificial Intelligence Tutorial

VA

Vishalini A



Learn via Video Course

Artificial Intelligence and Machine Learning Course Logo

Artificial Intelligence and Machine Learning Course

55 hrs



Get in Touch For Details