Skip to content
ML Visualization

K-Means vs DBSCAN

K-means requires you to choose the number of clusters up front and assigns every point to its nearest centroid; DBSCAN discovers the number of clusters from the data's density and can label sparse points as noise. That difference in contract drives everything else. K-means partitions space into convex, roughly spherical regions by minimizing within-cluster variance, so every point gets a cluster whether it belongs anywhere or not. DBSCAN grows clusters outward from dense cores, so clusters can be any shape, and isolated points are honestly reported as outliers instead of being forced into the nearest blob.

Their failure modes are mirror images. K-means fails on non-convex shapes, the famous crescent and ring datasets, on clusters of very different sizes or densities, and whenever the true group count is unknown. DBSCAN fails when densities vary across the dataset, because a single eps radius cannot be right for both a tight cluster and a diffuse one, and its parameters are harder to intuit than a simple k. K-means also scales to millions of points with mini-batch variants, while DBSCAN's neighborhood queries make it costlier, though index structures help.

Both are distance-based, so feature scaling is mandatory for each: an unscaled feature with a large range silently dominates the metric. And both benefit from the same honest workflow this site teaches everywhere: plot your data first, run the algorithm, then perturb it. Drag a centroid, shrink eps, and watch where each method breaks before trusting it on data you cannot see.

Side by side

Core idea

K-Means Clustering

Alternate between assigning points to the nearest centroid and moving each centroid to its members' mean, minimizing within-cluster variance.

DBSCAN

Grow clusters from core points that have at least min_samples neighbors within radius eps, connecting dense regions and labeling the rest noise.

Number of clusters

K-Means Clustering

Fixed in advance; you must supply k, typically chosen with the elbow method or silhouette scores.

DBSCAN

Discovered from density; the algorithm finds however many dense regions exist at your chosen eps.

Cluster shapes

K-Means Clustering

Convex and roughly spherical; boundaries are straight-line bisectors between centroids, so crescents and rings get cut apart.

DBSCAN

Arbitrary; any connected dense region qualifies, so crescents, rings, and snaking shapes come out intact.

Outlier handling

K-Means Clustering

None; every point is assigned to some cluster, so outliers drag centroids toward themselves.

DBSCAN

Built in; points that are neither cores nor within reach of one are labeled noise, which doubles as anomaly detection.

Key hyperparameters

K-Means Clustering

k, the number of clusters, plus initialization; k-means++ seeding and multiple restarts are standard.

DBSCAN

eps, the neighborhood radius, and min_samples, the density threshold; a k-distance plot helps choose eps.

Varying density

K-Means Clustering

Struggles when clusters have very different spreads, since variance minimization favors similar-sized blobs.

DBSCAN

Its classic weakness; one global eps cannot fit both dense and sparse clusters, splitting some and merging others.

Scalability

K-Means Clustering

Excellent; linear-time iterations and mini-batch variants handle millions of points routinely.

DBSCAN

Moderate; neighborhood queries dominate cost, roughly n log n with spatial indexes but degrading in high dimensions.

Determinism

K-Means Clustering

Depends on random initialization; different seeds can give different partitions, so run multiple restarts.

DBSCAN

Essentially deterministic for core assignments given eps and min_samples; only some border-point ties depend on visit order.

When it fails

K-Means Clustering

Non-convex shapes, unknown k, unequal cluster sizes and densities, and datasets salted with outliers.

DBSCAN

Datasets with widely varying density, high-dimensional spaces where distances concentrate, and poorly chosen eps.

Typical use cases

K-Means Clustering

Customer segmentation, vector quantization, color palette compression, and fast preprocessing at scale.

DBSCAN

Spatial and geographic clustering, anomaly detection, and any data where shapes are irregular and noise is expected.

When to use K-Means Clustering

  • You have a business reason to want exactly k groups, such as building five customer segments for five campaigns.
  • Your clusters look like compact, similar-sized blobs in a PCA or scatter plot, the geometry k-means is built for.
  • The dataset is large, into the millions of rows, where mini-batch k-means stays fast and DBSCAN gets expensive.
  • You need centroids as outputs, for vector quantization, prototypes, or nearest-center assignment of future points.
  • Every point must receive a label; downstream systems cannot handle a noise category.

When to use DBSCAN

  • Your clusters are non-convex, crescents, rings, or snaking spatial traces, which centroid methods will slice apart.
  • You do not know how many clusters exist and do not want to guess k before seeing results.
  • Outliers are expected and meaningful, and you want them flagged as noise rather than absorbed into clusters.
  • You are clustering spatial data such as GPS points, where a physical radius makes eps easy to justify.
  • Cluster densities are roughly comparable across the dataset, the regime where a single eps works well.

The bottom line

Let the geometry decide. If your data forms compact, similar-sized blobs, you can defend a specific k, and speed at scale matters, k-means is the right default: simple, fast, and predictable. If clusters are irregularly shaped, the count is unknown, or noise points are a real category you care about, DBSCAN is worth its trickier parameters. Scale your features either way, and spend five minutes plotting before committing: a single scatter plot, or a PCA projection for high-dimensional data, usually reveals immediately whether you are in blob-world or shape-world. When densities vary a lot across clusters, consider HDBSCAN, which relaxes DBSCAN's single-radius assumption.

Frequently asked questions

Why does k-means fail on crescent or ring shapes?
K-means assigns each point to its nearest centroid, which partitions space with straight-line boundaries into convex cells. A crescent or ring is non-convex: its ends curve around another cluster's territory, so the nearest-centroid rule inevitably slices it apart and glues the pieces to the wrong neighbors. DBSCAN follows the dense band itself, so the shape survives.
How do I choose eps and min_samples for DBSCAN?
A common recipe: set min_samples to roughly twice the number of dimensions or at least 4, then plot each point's distance to its k-th nearest neighbor sorted in increasing order and pick eps near the elbow where distances shoot up. Then perturb both values and check the clustering is stable; results that flip with tiny eps changes should not be trusted.
How do I choose k for k-means?
Use the elbow method, plotting within-cluster variance against k and looking for the bend, or silhouette scores, which reward tight, well-separated clusters. Neither is decisive, so combine them with domain knowledge: if the business needs five segments, five is a legitimate answer. Also run several random restarts at each k, since single runs can land in poor local optima.
What about Gaussian mixture models or hierarchical clustering instead?
A Gaussian mixture model is a softer, more flexible k-means: it fits ellipsoidal clusters with different shapes and sizes and returns membership probabilities, though you still pick k. Hierarchical clustering builds a dendrogram of nested merges, letting you inspect structure at every granularity without fixing k first, but it scales poorly. Both sit between k-means and DBSCAN in flexibility.
Can DBSCAN assign a cluster to new points after fitting?
Not natively; DBSCAN is a one-shot analysis of the dataset it was given, with no predict step in most libraries. The common workaround is nearest-neighbor assignment: give a new point the label of its nearest core point if it lies within eps, otherwise call it noise. K-means, by contrast, assigns new points naturally via the nearest centroid.