Data Warehousing and Data MiningUnit 916 min read
Cluster Analysis: Methods, Algorithms & Applications
Unit 9 of Data Warehousing and Data Mining explores clustering techniques—unsupervised learning methods to group similar data points—covering partitioning, hierarchical, density-based, and grid-based approaches, with real-world applications in market segmentation, fraud detection, and image compression.
TAKEAWAYS:
- Clustering groups unlabeled data into meaningful clusters based on similarity, requiring no predefined classes.
- Key algorithms include partitioning (K-means), hierarchical (agglomerative/dendrogram), density-based (DBSCAN), and grid-based (STING) methods.
- Distance metrics (Euclidean, Manhattan, cosine) and similarity measures (Jaccard, Pearson) define how data points are compared.
- Real-world uses include customer segmentation (e.g., Daraz’s recommendation systems), fraud detection (e.g., Ncell’s anomaly clustering), and medical imaging (e.g., tumor classification in hospitals).
- Challenges include scalability (large datasets), determining the optimal number of clusters, and handling noise/outliers.
- Evaluation metrics like silhouette score, Davies-Bouldin index, and elbow method help assess clustering quality.
1. Introduction to Cluster Analysis
Cluster analysis is an unsupervised learning technique that groups similar data points together without prior knowledge of labels. Unlike supervised learning (e.g., classification), clustering discovers hidden patterns in data, making it useful for exploratory data analysis.
Key Characteristics of Clustering
- No predefined classes: Algorithms learn patterns from data.
- Similarity-based grouping: Points in the same cluster are more similar to each other than to points in other clusters.
- Applications:
- Market segmentation (e.g., Daraz grouping customers by purchasing behavior).
- Image segmentation (e.g., separating objects in satellite imagery).
- Anomaly detection (e.g., Ncell identifying unusual call patterns).
Types of Clustering Algorithms
Clustering algorithms can be categorized based on their approach:
| Category | Description | Example Algorithms |
|---|---|---|
| Partitioning | Divides data into k non-overlapping clusters. | K-means, K-medoids |
| Hierarchical | Builds a tree of clusters (agglomerative or divisive). | Agglomerative, Divisive Hierarchy |
| Density-based | Groups points based on density (identifies arbitrary shapes). | DBSCAN, OPTICS |
| Grid-based | Divides space into grid cells and clusters dense cells. | STING, CLIQUE |
| Model-based | Assumes data is generated from a probability distribution. | Gaussian Mixture Models (GMM) |
2. Partitioning Methods: K-means Clustering
K-means is the most widely used clustering algorithm, especially for large datasets. It partitions data into k clusters by minimizing within-cluster variance.
How K-means Works
- Initialize: Randomly select k centroids (cluster centers).
- Assign: Assign each data point to the nearest centroid.
- Update: Recalculate centroids as the mean of all points in the cluster.
- Repeat: Iterate until centroids stabilize (convergence).
Worked Example: Customer Segmentation for Daraz
Suppose Daraz wants to segment customers based on purchase frequency (X) and average spending (Y). We use K-means with k=3 on the following data:
| Customer | Purchase Frequency (X) | Avg. Spending (Y) |
|---|---|---|
| A | 5 | 200 |
| B | 10 | 500 |
| C | 3 | 100 |
| D | 8 | 300 |
| E | 12 | 600 |
Step-by-Step Trace:
- Initialize centroids: Suppose we start with centroids at (5,200), (10,500), and (3,100).
- Assign points:
- A → Cluster 1 (nearest to (5,200))
- B → Cluster 2 (nearest to (10,500))
- C → Cluster 3 (nearest to (3,100))
- D → Cluster 1 (distance to (5,200) < distance to others)
- E → Cluster 2
- Update centroids:
- Cluster 1: Mean of A(5,200) and D(8,300) → (6.5, 250)
- Cluster 2: Mean of B(10,500) and E(12,600) → (11, 550)
- Cluster 3: C(3,100) → remains (3,100)
- Reassign and repeat until centroids converge.
Final Clusters:
- Cluster 1 (Low-frequency, moderate spend): A, D
- Cluster 2 (High-frequency, high spend): B, E
- Cluster 3 (Low-frequency, low spend): C
Visualization of Clusters:
Advantages and Limitations of K-means
| Advantages | Limitations |
|---|---|
| Simple and fast for large datasets. | Requires predefined k (number of clusters). |
| Works well with spherical clusters. | Sensitive to initial centroid placement. |
| Scalable to high-dimensional data. | Struggles with non-spherical clusters. |
| Easy to implement. | Outliers can distort centroids. |
Choosing the Optimal k (Elbow Method)
To determine the best k, plot the within-cluster sum of squares (WCSS) for different k values. The "elbow" point (where the rate of decrease slows) is the optimal k.
Example: For Daraz’s data, the elbow occurs at k=3, confirming our choice.
3. Hierarchical Clustering
Hierarchical clustering builds a dendrogram (tree-like structure) to represent nested clusters. It can be agglomerative (bottom-up) or divisive (top-down).
Agglomerative Hierarchical Clustering (Bottom-Up)
- Treat each point as a cluster.
- Merge the two closest clusters iteratively.
- Stop when all points are in a single cluster or a threshold is met.
Worked Example: Traffic Route Optimization for Kathmandu
Suppose NTC wants to cluster traffic routes based on congestion levels (X) and average speed (Y). Data:
| Route | Congestion (X) | Avg. Speed (Y) |
|---|---|---|
| A | 8 | 20 |
| B | 5 | 30 |
| C | 7 | 25 |
| D | 3 | 40 |
Dendrogram Construction:
- Start with single-point clusters: {A}, {B}, {C}, {D}.
- Merge B and D (smallest distance: Euclidean distance between (5,30) and (3,40) = 10.4).
- Merge A and C (distance between (8,20) and (7,25) = 5.4).
- Finally, merge {A,C} and {B,D}.
Advantages and Limitations
| Advantages | Limitations |
|---|---|
| No need to predefine k. | Computationally expensive (O(n³)). |
| Produces a hierarchy (useful for nested analysis). | Difficult to reverse merges (once merged, clusters stay merged). |
| Works well for small datasets. | Sensitive to noise and outliers. |
4. Density-Based Clustering: DBSCAN
DBSCAN (Density-Based Spatial Clustering of Applications with Noise) groups points based on density, making it ideal for clusters of arbitrary shapes and identifying outliers.
Key Concepts
- ε-neighborhood: All points within distance ε of a given point.
- Core point: A point with at least minPts neighbors within ε.
- Border point: A point within ε of a core point but not a core point itself.
- Noise: Points that are neither core nor border points.
Worked Example: Fraud Detection in Ncell Transactions
Ncell wants to detect fraudulent transactions using DBSCAN. Suppose:
- ε = 10 (distance threshold)
- minPts = 3 (minimum points to form a cluster)
Data (Transaction Amount vs. Frequency):
| Transaction | Amount (X) | Frequency (Y) |
|---|---|---|
| A | 500 | 1 |
| B | 500 | 2 |
| C | 500 | 3 |
| D | 5000 | 1 |
| E | 5000 | 1 |
| F | 500 | 100 |
Clustering Steps:
- Core Points: A, B, C (all within ε=10 of each other in frequency).
- Noise: D, E (isolated high-amount transactions → potential fraud).
- Border Point: F (within ε of A but not a core point).
Visualization:
graph TD
A["A(500,1)"] -->|"Core"| C1["Cluster 1"]
B["B(500,2)"] -->|"Core"| C1
C["C(500,3)"] -->|"Core"| C1
D["D(5000,1)"] -->|"Noise"| N["Noise"]
E["E(5000,1)"] -->|"Noise"| N
F["F(500,100)"] -->|"Border"| C1Advantages and Limitations
| Advantages | Limitations |
|---|---|
| Handles arbitrary cluster shapes. | Struggles with varying densities. |
| Identifies noise/outliers. | Sensitive to ε and minPts parameters. |
| No need to specify k. | Computationally intensive for large datasets. |
5. Grid-Based Clustering: STING
STING (Statistical Information Grid) divides space into a grid and clusters dense cells. It is efficient for spatial data (e.g., GPS coordinates).
How STING Works
- Divide space into rectangular cells.
- Compute statistics (e.g., mean, count) for each cell.
- Merge cells with similar densities into clusters.
Example: Location-Based Marketing for Pathao
Pathao wants to cluster high-demand areas in Kathmandu based on latitude (X) and longitude (Y). Suppose the grid is divided into 4 cells:
| Cell | Latitude Range | Longitude Range | Demand Count |
|---|---|---|---|
| 1 | 27.68-27.70 | 85.30-85.32 | 50 |
| 2 | 27.68-27.70 | 85.32-85.34 | 10 |
| 3 | 27.70-27.72 | 85.30-85.32 | 80 |
| 4 | 27.70-27.72 | 85.32-85.34 | 5 |
Clustering:
- Cells 1 and 3 have high demand → merged into a "High-Demand Cluster."
- Cells 2 and 4 → "Low-Demand Cluster."
Advantages and Limitations
| Advantages | Limitations |
|---|---|
| Fast for spatial data. | Requires uniform grid partitioning. |
| Handles large datasets efficiently. | Struggles with non-uniform densities. |
| Works well with preprocessed data. | Less flexible than density-based methods. |
6. Model-Based Clustering: Gaussian Mixture Models (GMM)
GMM assumes data is generated from a mixture of Gaussian distributions. It uses Expectation-Maximization (EM) to estimate parameters.
Example: Stock Price Analysis for NEPSE
Suppose NEPSE wants to cluster daily stock price changes into volatility patterns. Data (Daily Return % vs. Volume):
| Day | Return (%) (X) | Volume (Y) |
|---|---|---|
| 1 | 2 | 1000 |
| 2 | -1 | 500 |
| 3 | 3 | 2000 |
| 4 | -2 | 300 |
GMM fits a mixture of Gaussians to identify:
- High-volatility, high-return cluster (Day 3).
- Low-volatility, stable cluster (Days 1, 2, 4).
Advantages and Limitations
| Advantages | Limitations |
|---|---|
| Probabilistic clustering (soft assignments). | Computationally expensive. |
| Works well with overlapping clusters. | Requires knowledge of number of components. |
| Handles non-spherical clusters. | Sensitive to initialization. |
7. Clustering Evaluation Metrics
To assess clustering quality, use:
| Metric | Description |
|---|---|
| Silhouette Score | Measures how similar a point is to its own cluster vs. other clusters. Range: [-1, 1]. Higher is better. |
| Davies-Bouldin Index | Ratio of within-cluster distance to between-cluster distance. Lower is better. |
| Elbow Method | Plots WCSS vs. k; optimal k is at the "elbow." |
| Dunn Index | Ratio of minimum inter-cluster distance to maximum intra-cluster distance. Higher is better. |
Example: For Daraz’s K-means clustering, a silhouette score of 0.65 indicates reasonably good separation.
8. Applications of Cluster Analysis
In the Real World
E-commerce (Daraz, Amazon):
- Use: Customer segmentation based on purchase history.
- How: K-means clusters users into "high-spenders," "bargain hunters," and "occasional buyers" to personalize recommendations.
Telecom (Ncell, NTC):
- Use: Fraud detection in call data.
- How: DBSCAN identifies unusual call patterns (e.g., sudden high-frequency calls from a single SIM).
Healthcare (Hospitals):
- Use: Disease outbreak prediction.
- How: Hierarchical clustering groups regions by infection rates to allocate resources.
Finance (Banks):
- Use: Credit risk assessment.
- How: GMM clusters loan applicants into "low-risk," "medium-risk," and "high-risk" groups.
Social Media (WhatsApp, YouTube):
- Use: Community detection.
- How: DBSCAN groups users into "active communities" based on interaction patterns.
9. Challenges in Clustering
- Scalability: Algorithms like hierarchical clustering struggle with large datasets.
- Determining k: No universal method; elbow method or silhouette score helps.
- Noise and Outliers: DBSCAN handles noise, but K-means is sensitive to outliers.
- High-Dimensional Data: Distance metrics become less meaningful (e.g., "curse of dimensionality").
- Interpretability: Clusters may not align with business logic (e.g., "Cluster 2" may not have a clear name).
Exam Tip
For TU/PU/NEB exams, focus on:
- Definitions: Clearly distinguish between partitioning, hierarchical, density-based, and grid-based clustering.
- Algorithms: Know the steps of K-means, DBSCAN, and hierarchical clustering inside out.
- Worked Examples: Be ready to apply K-means or DBSCAN to a small dataset (e.g., 5-6 points).
- Evaluation Metrics: Explain silhouette score and elbow method with an example.
- Real-World Applications: Link clustering to e-commerce (Daraz), fraud detection (Ncell), or healthcare.
- Limitations: Discuss why K-means fails for non-spherical clusters and how DBSCAN handles noise.
Common Exam Questions:
- "Explain K-means clustering with a step-by-step example."
- "How does DBSCAN differ from K-means?"
- "What is the elbow method, and how is it used?"
- "Apply hierarchical clustering to the given dataset."
- "Discuss the applications of clustering in Nepalese industries (e.g., banking, telecom)."
Visual Summary of Clustering Algorithms:
Based on the TU BITM syllabus for Data Warehousing and Data Mining (IT274), unit 9.
Discussion
Loading…