Cluster analysis

Cluster analysis

How hierarchical, centroid-based and density-based clustering group similar observations together without predefined labels, and how the number and quality of clusters are judged.

What it is

Cluster analysis is a group of methods that divide a set of objects into groups, called clusters, so that objects within a cluster are more similar to each other than to those in other clusters. There are no predefined labels, which makes it an unsupervised technique. It is used to find types of customers, patient subtypes, gene expression patterns, material classes, species groups, document topics and image regions, and to explore data before building models.

It answers questions such as: Are there natural groups in these data? How many? Which samples resemble each other? How are the groups different? What is a typical member of each? Different algorithms rest on different ideas of what a cluster is, so results depend on the method, the distance measure and the scaling of the variables, and no single “correct” clustering exists. Judgment and validation are part of the work.

Fundamentals

Similarity, distance and scaling

Clustering starts from a measure of dissimilarity between objects. For numerical variables, the Euclidean distance d = √Σ(xi − yi)2 is the default, Manhattan distance Σ|xi − yi| is less sensitive to outliers, Mahalanobis distance accounts for correlation and different variances, and correlation distance (1 − r) compares patterns of change (used for gene expression and spectra). For binary or categorical data, use Jaccard, simple matching or Gower’s distance for mixed types. Variables must be standardized (z-scores) when they have different units, or those with large ranges dominate. In high dimensions, distances become less informative (the curse of dimensionality), so reducing dimensions first, by PCA or feature selection, helps. Irrelevant or noisy variables degrade results.

Hierarchical clustering

Agglomerative clustering starts with each object as its own cluster and repeatedly merges the two closest clusters, producing a tree called a dendrogram; cutting the tree at a chosen height gives a given number of clusters. The merge rule (linkage) defines the distance between clusters: single (nearest pair) can chain clusters together; complete (farthest pair) gives compact clusters; average (UPGMA) is a compromise; and Ward’s method merges the pair that least increases the within-cluster sum of squares, favoring compact, equal-sized spherical clusters. Hierarchical clustering needs no preset number of clusters and shows structure at multiple levels, but it costs O(n2) memory and time, and early merges cannot be undone. It is often paired with a heatmap.

Partitioning methods: k-means and relatives

k-means divides data into k clusters by minimizing the within-cluster sum of squares, J = Σj Σx∈Cj ‖x − μj‖2. The algorithm (Lloyd’s) alternates between assigning each point to the nearest centroid and recomputing the centroids as cluster means until assignments stop changing. It is fast (O(n k) per iteration) but needs k in advance, assumes roughly spherical clusters of similar size, is sensitive to outliers and to the starting centroids (it finds a local minimum), so it is run several times with k-means++ initialization. k-medoids (PAM) uses actual data points as centers and is more robust and works with any distance, fuzzy c-means gives each point a degree of membership in every cluster, and Gaussian mixture models (fit by expectation–maximization) model clusters as ellipsoids with soft assignments and allow model selection by BIC.

Density-based and other methods

DBSCAN defines clusters as dense regions: a point with at least minPts neighbors within radius ε is a core point, clusters grow from connected core points, and points in sparse regions are labeled noise. It finds clusters of arbitrary shape and identifies outliers, but struggles when densities vary (HDBSCAN addresses this). Spectral clustering uses the eigenvectors of a graph Laplacian built from a similarity graph, and can separate non-convex shapes such as concentric rings. Self-organizing maps, community detection in networks (Louvain, Leiden), biclustering (rows and columns together) and consensus clustering (stability across resamples) are used in specialized settings.

Choosing the number of clusters and validating

Internal criteria use only the data: the elbow method (plot J against k and look for the bend), the silhouette coefficient s(i) = [b(i) − a(i)] / max[a(i), b(i)], where a is the mean distance to points in the same cluster and b is the mean distance to the nearest other cluster (close to 1 means well clustered, near 0 on a boundary, negative suggests misassignment; averages above 0.5 indicate reasonable structure), the gap statistic (compares J with that from random reference data), Calinski–Harabasz and Davies–Bouldin indices, and BIC for mixture models. External criteria compare with known labels (adjusted Rand index, normalized mutual information) when available. Stability is judged by repeating on bootstrap samples or different initializations; unstable clusters are probably not real. Clustering always returns clusters, even for uniform random data, so test whether structure exists (Hopkins statistic) and examine meaning: profile each cluster by variable means and check with domain knowledge.

Typical applications and limits

Applications include market segmentation, gene and sample clustering in bioinformatics, image segmentation, material and spectral classification, and anomaly detection. Limits: dependence on algorithm and parameters, difficulty with clusters of different density and shape, sensitivity to scaling and outliers, and results that can be hard to interpret or reproduce.

Basic operation

  1. Select and prepare variables. Choose relevant features, handle missing data and outliers, and standardize.
  2. Pick a distance and method. Match them to the data type and expected cluster shape.
  3. Run the algorithm. For k-means, test multiple k values and initializations; for hierarchical, choose linkage.
  4. Choose the number of clusters. Use the elbow, silhouette, gap statistic or dendrogram height.
  5. Validate. Check stability by resampling and compare methods.
  6. Profile the clusters. Summarize each cluster’s characteristics and visualize with PCA or UMAP plots.
  7. Interpret in context. Name and check clusters against domain knowledge.
  8. Collect the output. You obtain cluster assignments and profiles; algorithm comparison and validation indices are covered in the video.

Good practice

  • Scale the variables. Otherwise units dictate the result.
  • Try more than one method. Compare the outcomes.
  • Test stability. Unstable clusters are likely artifacts.
  • Do not over-interpret. Algorithms find clusters even in noise.
  • Set seeds and record parameters. Clustering often involves randomness.
  • Be careful with labels. Clusters of people can reinforce bias or stereotypes, so avoid using them for sensitive decisions without review, and protect personal data.

Key terms

  • Cluster — a group of objects that are more similar to each other than to others.
  • Dendrogram — a tree diagram of hierarchical merges.
  • Linkage — the rule for measuring distance between clusters.
  • Centroid — the mean position of the points in a cluster.
  • Silhouette coefficient — a measure of how well each point fits its cluster.
  • DBSCAN — a density-based algorithm that finds clusters of any shape and marks noise.
  • Unsupervised learning — finding structure in data without known labels.

Video coming soon

About five minutes on Cluster analysis: how it works in detail, how the data is acquired, and how it is analyzed.

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping