K-means vs Gaussian Mixture Models (GMM)
K-means partitions data into non-overlapping clusters using hard assignments based on Euclidean distance to centroids, implicitly assuming spherical clusters of similar variance. Gaussian Mixture Models (GMMs) model data as a mixture of multivariate Gaussian distributions via Expectation-Maximization, providing soft probabilistic assignments and full covariance matrices to capture elliptical clusters of varying sizes and orientations.
K-means vs Gaussian Mixture Models (GMM)
Direct Answer
K-means and Gaussian Mixture Models (GMM) are both centroid-based clustering methods, but they differ in probabilistic assumptions and cluster geometry. K-means performs hard clustering, assigning every point to a single nearest centroid under Euclidean distance, which produces linear Voronoi boundaries and favors roughly spherical clusters of similar variance.
GMM is a generative probabilistic model that assumes data is drawn from a mixture of multivariate Gaussian distributions:
Fitted with the Expectation-Maximization (EM) algorithm, GMM yields soft clustering via posterior responsibilities and models elliptical clusters of varying sizes and orientations through covariance matrices . In fact, K-means can be viewed as the hard-assignment limiting case of GMM with equal mixing weights and shared isotropic covariance as .
Key Terms
- Hard Clustering: Data points are assigned deterministically to exactly one cluster.
- Soft Clustering: Data points receive posterior probabilities of belonging to each cluster, quantifying assignment uncertainty.
- Covariance Matrix (): Defines the spread, elongation, and orientation of each Gaussian component across dimensions.
- Expectation-Maximization (EM): Iterative algorithm alternating between computing expected cluster responsibilities (E-step) and updating mixture weights , means , and covariances (M-step) to maximize log-likelihood.
Example
Consider customer segmentation in e-commerce. K-means groups customers into disjoint segments such as “high spenders,” “occasional buyers,” and “bargain hunters,” assigning each user to one cluster. GMM instead estimates probabilities for each customer—for example, 80% “high spender,” 15% “occasional buyer,” and 5% “bargain hunter.”
If purchase amount and purchase frequency form two elongated, differently oriented clusters (such as users making a few large purchases versus users making many small purchases), GMM’s covariance matrices can separate their distinct shapes and correlations, whereas K-means tends to force spherical partitions across them.
Code Implementation
import numpy as np
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
import matplotlib.pyplot as plt
# Generate sample data with varying spread
np.random.seed(0)
X = np.concatenate([
np.random.normal(-2, 1, size=(300, 2)),
np.random.normal(2, 1, size=(300, 2)),
np.random.normal(0, 3, size=(300, 2)),
])
# K-means clustering (hard assignments)
kmeans = KMeans(n_clusters=3, random_state=0, n_init='auto')
kmeans_labels = kmeans.fit_predict(X)
# Gaussian Mixture Model (soft assignments + covariance modeling)
gmm = GaussianMixture(n_components=3, covariance_type='full', random_state=0)
gmm_labels = gmm.fit_predict(X)
gmm_probs = gmm.predict_proba(X)
# Plotting
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.scatter(X[:, 0], X[:, 1], c=kmeans_labels, cmap='viridis', alpha=0.7)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=200, c='red', marker='X', label='Centroids')
plt.title('K-means Clustering')
plt.legend()
plt.subplot(1, 2, 2)
plt.scatter(X[:, 0], X[:, 1], c=gmm_labels, cmap='viridis', alpha=0.7)
plt.scatter(gmm.means_[:, 0], gmm.means_[:, 1], s=200, c='red', marker='X', label='Means')
plt.title('Gaussian Mixture Model Clustering')
plt.legend()
plt.tight_layout()
plt.show()
# Print the posterior probabilities of the first 5 points belonging to each component
print("GMM Probabilities for first 5 points:\n", gmm_probs[:5])
Related Concepts
- Clustering Evaluation Metrics: For hard clustering, metrics like Silhouette Score and Davies-Bouldin index are common; for GMMs, held-out log-likelihood, AIC, and BIC help select and
covariance_type(full,tied,diag,spherical). - Dimensionality Reduction: Techniques like PCA can be applied before GMM to avoid singular or ill-conditioned covariance estimates when dimensionality is large relative to sample count .
- Density-Based Clustering (DBSCAN): Discovers arbitrarily shaped clusters and explicitly flags noise points without assuming Gaussian components or pre-specifying .
- Hierarchical Clustering: Builds a dendrogram of nested clusters (agglomerative or divisive) without requiring upfront, trading off higher – computational cost.