One of the most common techniques you'll encounter when starting data analysis is clustering.
Whether you want to segment customers, group survey respondents by similarity, or detect anomalous transactions, clustering is used across industries.
Note
What is Clustering?
Clustering is an unsupervised learning technique that automatically groups data points with similar characteristics.
Unlike supervised learning which requires labeled data, unsupervised learning finds hidden patterns and structure in unlabeled data on its own.
Concrete Example
- Customer A: Age 20s, Monthly spend $3,000, 10 visits
- Customer B: Age 20s, Monthly spend $2,800, 9 visits
- Customer C: Age 50s, Monthly spend $500, 2 visits
Clustering automatically groups A and B together (high-value customers) and C in a separate group (low-frequency customer).
Distance Metrics
In clustering, "similarity" is determined by distance or similarity measures.
Watch out
Distance Metrics
Euclidean measures the straight line; Manhattan follows the grid, block by block.
Euclidean Distance
The most commonly used distance metric. It measures the straight-line distance between two points and is K-Means' default.
- Intuitive and computationally simple
- Effective for normally distributed data
- Sensitive to outliers
Manhattan Distance
Calculates distance as if traveling along city blocks. More stable for high-dimensional data.
Cosine Similarity
Measures the angle between vectors. Highly effective for text analysis and document similarity.
What Makes a Good Cluster?
Before choosing an algorithm, it helps to define what "good" even means. A good clustering satisfies two simple ideas:
- Points in the same cluster are close together
- Points in different clusters are far apart
These two ideas have names. Cohesion measures how tight a cluster is (how close its own points are), and separation measures how far apart different clusters sit. A good clustering has high cohesion and high separation.
Note
Major Algorithms
K-Means
K-Means is the most popular clustering algorithm. It efficiently finds clusters in numeric data.
Advantages
- Very fast computation
- Simple to implement
- Suitable for large datasets
Disadvantages
- Must specify number of clusters (K) beforehand
- Sensitive to outliers
- Works best on spherical clusters
Note
Elbow Method
choosing k
Inertia drops sharply, then flattens. The 'elbow' — here k=3 — is a good choice.
DBSCAN
DBSCAN is a density-based clustering algorithm. Its key feature: it automatically identifies noise (outliers)!
Recommended for:
- GPS data (location-based services)
- Anomaly detection
- Irregularly shaped clusters
DBSCAN
density-based
Dense regions form clusters; isolated points are labelled noise — no k required.
Hierarchical Clustering
Hierarchical clustering builds a tree of nested groups — repeatedly merging the two closest clusters — and draws it as a dendrogram. Its defining advantage: you don't set the number of clusters up front.
- Produces a dendrogram — a full tree of how points merge
- You choose the number of clusters afterward, by cutting the tree at a height
- Reveals the nested structure, not just a flat partition
Reach for it when you don't know K in advance, or when the grouping itself has a hierarchy (e.g. products within categories within departments).
GMM (Gaussian Mixture Model)
K-Means forces each point into exactly one cluster. GMM instead models the data as a mix of Gaussian "blobs" and gives each point a probability of belonging to each cluster — soft assignment rather than hard.
- Handles clusters that overlap, where a point could plausibly belong to two
- Fits elliptical (stretched) clusters, not just the spheres K-Means assumes
- Returns membership probabilities — useful when the boundary is genuinely fuzzy
Choose GMM when your clusters overlap or aren't round, and when you want a confidence, not just a label.
Spectral Clustering
Spectral clustering is the tool for clusters that aren't blob-shaped at all — concentric rings, spirals, or other non-linear structures that K-Means and GMM get completely wrong. It builds a similarity graph of the points and cuts it into groups using the graph's eigenvectors.
- Captures non-linear, non-convex shapes (rings, crescents, chains)
- Works from connectivity, not straight-line distance to a center
- More computationally expensive, so best for small-to-medium datasets
SOM (Self-Organizing Map)
SOM is a neural network-based algorithm for dimensionality reduction and clustering. It visualizes high-dimensional data in 2D or 3D grids while simultaneously performing clustering.
SOM Advantages:
- Visualize high-dimensional data on 2D plane
- Understand complex patterns intuitively
- Preserve data topology (geometric structure)
Self-Organizing Map
SOM
SOM projects high-dimensional data onto a 2-D grid; neighbouring cells stay similar.
Which Algorithm Should You Use?
This is the question everyone actually has: given my data, what do I run? Match your data's characteristic to the algorithm built for it.
| Data characteristic | Recommended algorithm |
|---|---|
| Round, well-separated clusters | K-Means |
| Number of clusters unknown | Hierarchical Clustering |
| Lots of noise / outliers | DBSCAN or HDBSCAN |
| Clusters overlap | GMM (Gaussian Mixture) |
| High-dimensional, need visualization | SOM |
| Non-linear structure (rings, spirals) | Spectral Clustering |
Tip
Clustering with SKARI Platform
SKARI is an integrated data analytics platform that makes clustering accessible without coding.
Key SKARI Advantages
- No coding required: Drag-and-drop UI for analysis workflows
- Automated preprocessing: Data normalization and outlier handling
- Auto algorithm selection: Platform recommends optimal algorithm
- Built-in visualization: Charts and graphs generated automatically
- Easy deployment: Export trained models as APIs
Real-World Example: Customer Segmentation
Result: 100,000 online retail customers segmented into 5 groups
- VIP customers (5,000) → Personalized premium services
- Active customers (25,000) → Early access to new products
- Regular customers (45,000) → Regular newsletters
- New customers (20,000) → Welcome discount coupons
- At-risk customers (5,000) → Retention campaigns
Takeaway
Frequently Asked Questions
Q1. How do I determine the optimal K?
Use Elbow Method, Silhouette Score, or Gap Statistic to find the optimal number of clusters.
Q2. How is SOM different from K-Means?
K-Means partitions data into K groups, while SOM visualizes data on a 2D/3D grid while clustering simultaneously.
Q3. Which algorithm for very large datasets?
For over 1 million rows, K-Means is fastest. For visualization, use Mini-Batch K-Means or SOM in batch mode.
Conclusion
Here's the key takeaway: While K-Means is the most famous clustering algorithm, it's not a universal solution.
Takeaway
Data Normalization Guide
Essential preprocessing techniques before clustering
Advanced Clustering Techniques
Ensemble clustering and multi-level analysis
Getting Started with SKARI
Complete your first clustering project in 5 minutes