Clustering is the branch of data mining that groups records so that items in the same group are more similar to each other than to items in any other group. Nobody tells the algorithm what the groups are — it discovers them. That makes clustering an unsupervised technique, and it is one of the most useful things you can do with a large, unlabeled dataset: customer segments, related documents, similar products, image regions, or suspicious outliers all fall out of the same basic idea.
This guide covers what cluster analysis actually is, the main types of clustering in data mining, the core algorithms (k-means, hierarchical, density-based and more), how you measure distance and quality, a worked Python example, how clustering scales to huge datasets with parallel and MapReduce-style computation, and where it is used in the real world.
What is cluster analysis?
Picture every record as a point in a multi-dimensional space — one axis per feature. A shop's customers might be plotted on axes like visits per month, average order value and days since last purchase. Points that sit close together behave alike; points far apart behave differently. Cluster analysis is the process of finding those dense neighborhoods automatically and labeling each point with the cluster it belongs to.
A cluster is usually summarized by a representative:
- In numeric (Euclidean) space, the natural summary is the centroid — the coordinate-wise average of the cluster's points — plus a spread measure like the average distance from the centroid.
- In non-numeric space, where "average" has no meaning, you instead pick an actual member that sits closest to all the others: the medoid (sometimes called the clustroid). You cannot average two document titles, so you nominate the most central document instead.
Because a whole cluster can be described by one representative and a spread, clustering also doubles as data compression and summarization — you replace millions of points with a handful of profiles you can reason about.
Clustering is one of the pillars of the field alongside classification and association rules; see data mining techniques and what is data mining for the wider picture.
Clustering vs classification
The two are easy to confuse:
- Classification is supervised. You already have labeled examples (spam / not-spam) and train a model to reproduce those labels on new data.
- Clustering is unsupervised. There are no labels. The algorithm proposes the groups, and it is up to you to interpret what each one means.
Use classification when you know the categories in advance. Use clustering when you suspect structure exists but you cannot name it yet.
Types of clustering in data mining
There is no single "correct" grouping of any dataset — different methods encode different assumptions about what a cluster is. The main types of clustering in data mining are:
1. Partitioning (centroid-based)
Split the data into a fixed number k of non-overlapping groups, each built around a center. k-means and k-medoids (PAM) are the classic examples. Fast and simple, but you must choose k up front and the method leans toward round, similarly-sized clusters.
2. Hierarchical
Build a tree (dendrogram) of nested clusters instead of one flat partition. Agglomerative methods start with every point as its own cluster and repeatedly merge the closest pair; divisive methods start with one big cluster and split it. You pick the number of clusters afterward by cutting the tree at a chosen height.
3. Density-based
Define clusters as dense regions separated by sparse ones. DBSCAN, OPTICS and HDBSCAN can find arbitrarily-shaped clusters and, crucially, label sparse points as noise instead of forcing everything into a group. No need to pre-set k.
4. Distribution / model-based
Assume the data was generated by a mixture of probability distributions and fit them. A Gaussian Mixture Model (GMM) gives each point a probability of belonging to each cluster — "soft" assignment rather than the "hard" one-cluster-only assignment of k-means.
5. Grid-based
Quantize the space into a grid of cells and cluster the cells (STING, CLIQUE). This scales well to very large, low-dimensional datasets because the work depends on the number of cells, not the number of points.
A second axis cuts across all of these — hard vs soft (fuzzy) clustering. Hard clustering puts each point in exactly one cluster; fuzzy methods (fuzzy c-means, GMM) let a point belong partially to several, which matters when boundaries are genuinely blurry.
Clustering algorithms in data mining
Here are the workhorse clustering algorithms in data mining, with what they are good and bad at.
k-means clustering in data mining
k-means clustering in data mining is the algorithm most people meet first, because it is fast and intuitive:
- Choose k and initialize k centroids (smartly, using k-means++, not purely at random).
- Assign every point to its nearest centroid.
- Update each centroid to the mean of the points now assigned to it.
- Repeat 2–3 until assignments stop changing.
It minimizes within-cluster variance (inertia) and runs comfortably on millions of rows. The catches: you must pick k; results depend on initialization (run it several times); it assumes roughly spherical, equally-sized clusters; and it is sensitive to outliers and to unscaled features — always standardize your data first. Use k-medoids if you need a real data point as the center or a non-Euclidean distance.
Choosing k: the elbow method plots inertia against k and looks for the bend; the silhouette score measures how well-separated the clusters are; the gap statistic compares against random data. Treat these as guides, not oracles — domain sense often beats a metric.
Hierarchical clustering in data mining
Hierarchical clustering in data mining produces a dendrogram, which is a genuine advantage when you want to see structure at multiple scales rather than commit to one number of clusters. The key decision is the linkage — how you measure the distance between two clusters:
- Single linkage — nearest pair of points. Finds elongated, chain-like clusters but can "chain" unrelated groups together.
- Complete linkage — farthest pair. Produces compact clusters.
- Average linkage — mean pairwise distance. A middle ground.
- Ward's method — merges the pair that increases total within-cluster variance the least. Usually the best default for numeric data.
Classic agglomerative clustering is O(n²) or worse in memory, so it is best for thousands, not millions, of points — though modern approximate variants push that ceiling higher.
DBSCAN (density-based)
DBSCAN needs two parameters — a neighborhood radius eps and a minimum number of neighbors minPts — and then grows clusters outward from dense "core" points. Its strengths: it discovers clusters of any shape, decides the number of clusters itself, and isolates outliers as noise (which makes it a dual-purpose anomaly detector). Its weakness: a single eps struggles when clusters have very different densities — that is exactly what HDBSCAN was built to fix, and it is the modern default worth reaching for.
Gaussian Mixture Models (model-based)
A GMM fits k Gaussian "blobs", each with its own mean, size and orientation, using the Expectation-Maximization (EM) algorithm. Because clusters can be stretched and tilted ellipses, GMMs handle overlapping, non-spherical groups that break k-means, and they return calibrated membership probabilities. The cost is more parameters to fit and more sensitivity to initialization.
Spectral and embedding-based clustering
For data where similarity is more meaningful than raw coordinates — graphs, images, text — spectral clustering clusters points using the eigenvectors of a similarity matrix and can separate shapes that defeat centroid methods. In 2026 the most common modern pattern is embed then cluster: turn text, images or user behavior into dense vectors with a neural embedding model, then run k-means or HDBSCAN on those vectors. That is how topic clustering of documents and semantic product grouping are usually done today.
Distance measures: the heart of clustering
Every clustering algorithm is only as good as its notion of "close". The metric you choose is the model:
- Euclidean distance — straight-line distance; the default for continuous numeric features in an N-dimensional Euclidean space where a centroid (the average of the points) is well defined.
- Manhattan distance — sum of absolute differences; more robust to outliers.
- Cosine similarity — angle between vectors; the standard for text and high-dimensional sparse data, where magnitude matters less than direction.
- Jaccard distance — for sets and binary features (shared vs total elements).
- Gower distance — for mixed numeric-and-categorical tables.
The Euclidean / non-Euclidean split has a practical consequence. In Euclidean space you can summarize a cluster by its centroid. In a non-Euclidean space — clustering documents by shared rare words, or grouping moviegoers by overlapping tastes — there is no meaningful average, so you must represent each cluster by an actual member, the medoid/clustroid, chosen because it has the smallest total distance to the rest of the cluster. Always scale or normalize features before computing distances, or the largest-range column will silently dominate; see data normalization.
A worked example in Python
scikit-learn is the standard toolkit. Here is k-means end to end, including scaling and picking k by silhouette:
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
# X: rows = customers, columns = [visits, avg_order_value, recency_days]
X = np.array([
[22, 18.0, 3], [25, 21.5, 5], [ 2, 120.0, 40],
[ 3, 140.0, 55], [30, 15.0, 2], [ 1, 200.0, 70],
])
X_scaled = StandardScaler().fit_transform(X) # never skip this
best_k, best_score = None, -1
for k in range(2, 5):
labels = KMeans(n_clusters=k, n_init=10, random_state=42).fit_predict(X_scaled)
score = silhouette_score(X_scaled, labels)
print(f"k={k} silhouette={score:.3f}")
if score > best_score:
best_k, best_score = k, score
model = KMeans(n_clusters=best_k, n_init=10, random_state=42).fit(X_scaled)
print("Chosen k:", best_k)
print("Cluster labels:", model.labels_)
Swapping in a different method is a one-line change: AgglomerativeClustering(n_clusters=best_k, linkage="ward"), DBSCAN(eps=0.6, min_samples=3), or GaussianMixture(n_components=best_k). For very large datasets, MiniBatchKMeans trades a little accuracy for a big speed gain by updating centroids on small random batches.
Evaluating clusters
Because there are no labels, you judge clusters two ways:
- Internal metrics (no ground truth): silhouette score (–1 to 1, higher is better), Davies-Bouldin index (lower is better), and inertia (within-cluster sum of squares, used in the elbow method).
- External metrics (when you do have labels to compare against): Adjusted Rand Index (ARI) and Normalized Mutual Information (NMI).
The most important test is not a number, though — it is whether a domain expert looks at the clusters and says "yes, those are real, distinct groups I can act on."
Clustering at scale: parallel computing and MapReduce
Naïve clustering compares points pairwise, which explodes on big data — a million points implies on the order of half a trillion pairwise distances. Two things rescue you: shortcuts that avoid touching every pair, and parallel, distributed computation.
Clustering parallelizes cleanly, and the classic pattern is MapReduce:
- Map — split the data into chunks and give each chunk to a separate task, which clusters its points locally and emits a compact description of each partial cluster (centroid or medoid, count, and spread statistics such as the sum of distances, the maximum distance, and the sum of squared distances to other points).
- Reduce — a task collects all those partial-cluster descriptions and merges the ones that belong together into the final clustering.
The map/reduce idea comes from functional programming: map(square, [1,2,3,4,5]) returns [1,4,9,16,25] by applying a function to every element, and a reduce step then folds those intermediate results into a final answer. Google published the distributed version of this in 2004 and used it internally for web indexing, page ranking and document clustering; the framework hid the messy details of distribution, fault tolerance and I/O so engineers could focus on the clustering logic itself.
In 2026 you rarely hand-write MapReduce. The same divide-cluster-merge strategy lives inside modern engines:
- Apache Spark MLlib provides distributed
KMeans,BisectingKMeansandGaussianMixtureout of the box, and is the usual choice for clustering that does not fit on one machine. - Dask-ML and cuML (GPU-accelerated) parallelize scikit-learn-style clustering in Python.
- Streaming / online variants (streaming k-means, online mini-batch) handle data that never stops arriving, re-estimating cluster descriptions as new points flow in — the dynamic case where centroids must be continually recomputed.
The underlying lesson is unchanged from the MapReduce era: cluster the chunks in parallel, then cluster the summaries.
Real-world applications of clustering
- Customer segmentation — group buyers by behavior (RFM: recency, frequency, monetary) to target marketing and pricing.
- Document and topic clustering — organize articles, tickets or reviews by theme, typically by embedding the text and clustering the vectors.
- Image segmentation — group pixels into regions for computer vision and medical imaging.
- Anomaly and fraud detection — points that fit no cluster (DBSCAN noise) are your suspects.
- Recommendation — cluster users or items to power "customers like you" suggestions.
- Web and marketing analytics — cluster traffic sources or landing pages; the original motivation for this article was grouping referral channels by visits and pages-per-visit to see which behave alike.
- Bioinformatics — group genes or patients by expression profiles.
Every one of these depends on having a clean, well-structured dataset to cluster in the first place — often assembled by web scraping and then deduplicated and normalized. If building and maintaining that collection pipeline is not where you want to spend your time, Scraping.Pro delivers ready-to-analyze datasets — scraped, cleaned and structured to your schema — and can run AI processing such as classification and entity extraction as part of the same pipeline.
FAQ
What is the difference between clustering and classification? Classification is supervised — it learns from labeled examples to sort new data into known categories. Clustering is unsupervised — it discovers the groups itself, with no labels provided.
Which clustering algorithm should I start with? Start with k-means for speed and a baseline. If clusters are oddly shaped or you need outliers flagged, use HDBSCAN. If you want a multi-scale view, use hierarchical clustering with Ward linkage. If you need probabilistic membership, use a GMM.
How do I choose the number of clusters (k)? Use the elbow method (inertia), the silhouette score, or the gap statistic to narrow it down, then let domain knowledge make the final call. Density-based methods like DBSCAN/HDBSCAN sidestep the question by inferring the count from the data.
Do I need to scale my features before clustering? Almost always yes. Distance-based methods let large-range features dominate, so standardize (z-score) or min-max scale first — unless every feature is already on the same meaningful scale.
How does clustering handle millions of records?
Use MiniBatchKMeans for speed on one machine, or a distributed engine like Spark MLlib that applies the MapReduce-style "cluster the chunks, then merge" strategy across a cluster of machines.
The bottom line
Clustering turns an unlabeled pile of data into interpretable groups. Master three things and you can handle most real problems: pick the type of clustering whose definition of a cluster matches your data (centroid, hierarchy, density or distribution), choose a distance measure that reflects real similarity, and validate the result with both metrics and human judgment. When the data outgrows one machine, the parallel, MapReduce-style approach lets the same algorithms scale out — and when preparing the input data is the bottleneck, we can deliver it ready to cluster.