Inferensys

Glossary

K-Means Clustering

A centroid-based unsupervised learning algorithm that partitions n observations into k clusters, where each observation belongs to the cluster with the nearest mean (centroid), minimizing within-cluster variance.
Knowledge engineer constructing knowledge base on laptop, document hierarchy visible, casual office setup.
CENTROID-BASED PARTITIONING

What is K-Means Clustering?

K-Means clustering is a foundational unsupervised machine learning algorithm that partitions a patient population into a predefined number (k) of distinct, non-overlapping subgroups by iteratively minimizing the within-cluster sum of squared distances from each data point to its assigned cluster centroid.

The algorithm operates by first initializing k centroids randomly in the feature space. In an iterative expectation-maximization loop, each patient is assigned to the nearest centroid based on a distance metric, typically Euclidean distance. The centroid is then recomputed as the mean of all points assigned to that cluster. This process repeats until centroid positions stabilize and cluster assignments converge, effectively grouping patients with similar molecular or clinical profiles.

In patient stratification, K-Means is widely used to discover molecular subtypes from gene expression or proteomic data. The primary challenge lies in selecting the optimal k, often determined by the elbow method or silhouette score. While computationally efficient and highly interpretable, the algorithm assumes spherical cluster shapes and equal variance, making it sensitive to feature scaling and outliers in high-dimensional biomedical datasets.

ALGORITHM PROPERTIES

Key Characteristics of K-Means

K-Means clustering partitions patient populations into k pre-defined subgroups by iteratively minimizing the within-cluster sum of squares. Understanding its core characteristics is essential for valid clinical stratification.

01

Centroid-Based Partitioning

K-Means defines each cluster by a centroid—the arithmetic mean of all patients assigned to that group. The algorithm iteratively refines these centroids to minimize the within-cluster variance, also known as inertia. This makes K-Means a hard clustering method, assigning each patient exclusively to the nearest centroid based on a distance metric, typically Euclidean distance.

02

Predefined Cluster Count (k)

The number of clusters, k, must be specified a priori. This is a critical hyperparameter that directly dictates the granularity of patient stratification. Selecting an inappropriate k can obscure true biological subtypes or create artificial divisions. Common heuristics for determining k include:

  • Elbow Method: Plotting inertia against k to find the point of diminishing returns.
  • Silhouette Score: Measuring how similar a patient is to its own cluster versus others.
  • Gap Statistic: Comparing the total within-cluster variation for different values of k against a null reference distribution.
03

Spherical Cluster Assumption

K-Means implicitly assumes that clusters are isotropic (spherical) and have similar variance in all directions. The algorithm partitions the feature space using linear hyperplanes, which means it performs poorly when true patient subgroups are elongated, non-convex, or have highly unequal densities. For complex, non-linear disease trajectories, methods like DBSCAN or Gaussian Mixture Models are often more appropriate.

04

Sensitivity to Initialization

The final clustering result is highly sensitive to the initial placement of centroids. Poor initialization can lead to convergence on a local, suboptimal minimum. The standard mitigation strategy is the K-Means++ initialization algorithm, which spreads initial centroids across the data space probabilistically, improving both the speed and quality of convergence. Multiple random restarts (n_init) are also standard practice to select the run with the lowest inertia.

05

Feature Scale Dependency

K-Means relies on a distance metric, making it critically sensitive to the scale of input features. A biomarker measured in milligrams (range 0-1000) will dominate a feature measured as a percentage (range 0-1) if data is not normalized. Standardization (z-score normalization) or min-max scaling is a mandatory preprocessing step to ensure each clinical variable contributes equally to the distance calculation and resulting patient clusters.

06

Computational Efficiency

Lloyd's algorithm, the standard implementation of K-Means, has a time complexity of O(n * k * d * i), where n is the number of patients, k is the number of clusters, d is the number of dimensions, and i is the number of iterations. This linear relationship with the number of patients makes it highly scalable and computationally efficient for large electronic health record datasets, especially when compared to hierarchical clustering methods with quadratic complexity.

ALGORITHM SELECTION GUIDE

K-Means vs. Other Clustering Algorithms

Comparative analysis of centroid-based, density-based, hierarchical, and probabilistic clustering methods for patient stratification tasks.

FeatureK-MeansDBSCANHierarchicalGaussian Mixture

Cluster shape assumption

Spherical, equal size

Arbitrary shape

Depends on linkage

Elliptical

Predefined cluster count required

Handles noise/outliers

Soft cluster assignment

Scalability (n > 100K)

Deterministic output

Interpretability

High (centroids)

Moderate

High (dendrogram)

Moderate (covariance)

Typical runtime complexity

O(n·k·d·i)

O(n·log n)

O(n²·log n)

O(n·k·d²·i)

K-MEANS CLUSTERING

Frequently Asked Questions

Clear, technical answers to the most common questions about centroid-based patient stratification, covering initialization, distance metrics, and validation.

K-Means clustering is a centroid-based unsupervised learning algorithm that partitions a dataset into a predefined number (k) of distinct, non-overlapping subgroups. The algorithm works iteratively: first, it randomly initializes k centroids in the feature space. It then alternates between two steps—assignment, where each data point is assigned to the nearest centroid based on a distance metric (typically Euclidean), and update, where centroids are recalculated as the mean of all points assigned to them. This process repeats until convergence, defined as the point where cluster assignments no longer change or a maximum iteration limit is reached. The objective function minimized is the within-cluster sum of squares (WCSS), also known as inertia, which quantifies the compactness of the resulting clusters.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.