Concepts / DBSCAN and Density-Based Clustering

DBSCAN and Density-Based Clustering

ARI and NMI compare a clustering against a ground truth clustering and give a quantitative measure between 0 and 1, but they are usually only practical for developing algorithms because real applications rarely have ground truth.

  • Programming

A Belief to Test Before Reading On

Once you have run a clustering algorithm, you need some way to judge the result. A tempting shortcut is to assume that the algorithm with the highest score has found the best grouping of the data. This article tests that assumption, and it uses DBSCAN as the running example. Most of the article is about judging a clustering, but the second half returns to DBSCAN itself, because a clustering run that looks wrong is where you learn the most about its parameters.

What do you think happens?

If one algorithm gets the highest silhouette score on a dataset, has it found the best grouping of the data?

  • Yes, a higher silhouette score always means a better clustering

  • No, a high silhouette score can favor a grouping that is not the most useful one

Reveal answer
Answer:

No. A high silhouette score can favor a grouping that is not the most useful one.

The silhouette coefficient rewards compactness, meaning points sit close to others in their own cluster. Compactness does not allow for complex shapes, so an elongated or curved grouping can score lower than a set of tight, roundish blobs even when the curved grouping matches the structure of the data.

Two Families of Clustering Measures

There are two broad families of measures for judging a clustering. The first compares the clustering against a ground truth clustering, which is a reference grouping already known to be correct. The second scores the clustering using only the data itself. Each family answers a different question. Measures in the first family ask whether the algorithm recovered a known answer. Measures in the second ask whether the groups look good by their own internal standard.

FamilyNeeds ground truth?Example measuresQuestion it answers
Compared against ground truthYesARI, NMIDid the algorithm recover the known grouping?
Scored from the data aloneNoSilhouette coefficientAre the groups compact by their own standard?
Robustness-basedNoCompare outcomes after noise or parameter changesDoes the grouping stay stable when the data or settings are nudged?

Three ways to judge a clustering, and what each one needs

What ARI and NMI Measure

The adjusted rand index (ARI) and normalized mutual information (NMI) are the two most important metrics for assessing the outcome of a clustering algorithm relative to a ground truth clustering. Both give a quantitative measure between 0 and 1. Their role is to compare the predicted clustering against a reference clustering that is already known. Because the score is a comparison, the ground truth is an input to the metric, not something the metric discovers.

A useful way to read an ARI or NMI value is to see it as an agreement score between two labelings of the same points. The algorithm produces one labeling. The ground truth provides the other. The metric asks how well the two agree, and it is calibrated so that a value near 0 corresponds to agreement no better than chance, while a value near 1 corresponds to close agreement with the reference.

The source uses ARI to compare k-means, agglomerative clustering, and DBSCAN, and it adds a random assignment to two clusters as a baseline. Seeing a random assignment alongside the real algorithms helps you judge whether an algorithm has found any structure at all. A real algorithm should clearly beat chance. If it scores about the same as a random assignment, it has not captured the structure in the data.

Why Ground Truth Is Usually Missing

The reason ARI and NMI are of limited use in practice is simple. When you apply a clustering algorithm to a real problem, there is usually no ground truth to compare the results against. This is why the summary of the source says these metrics are usually only practical for developing algorithms, where a reference grouping is available for testing.

This leads to the central question for the rest of the article. If you cannot compare against a ground truth, what can you measure? The source turns to scoring metrics that do not need one, and it explains where they fall short.

The Silhouette Coefficient and Its Blind Spot

The silhouette coefficient is a scoring metric for clustering that does not require ground truth. It measures the compactness of clusters. Higher is better, and a perfect score is 1. A high score means that points sit close to other points in their own cluster, which is what compact clusters look like.

The limitation is that compactness does not allow for complex shapes. The source notes that silhouette scores often do not work well in practice for this reason. A method that finds elongated or curved clusters can have a lower silhouette score than a method that cuts the same data into tight, roundish blobs, even when the curved grouping is the one that matches the structure of the data. The score is rewarding a shape preference, not the structure itself.

FeatureTight roundish blobsCurved or elongated grouping
CompactnessHigh: points sit close to others in the same clusterCan be lower, because the group stretches along a curve
Silhouette scoreTends to be higherCan be lower even when it matches the data's structure
Matches the data's structure?Only if the structure really is roundish blobsYes, when the structure really is curved

Why a high silhouette score does not guarantee a useful clustering

Robustness-Based Metrics

A slightly better strategy for evaluating clusters is to use robustness-based clustering metrics. These metrics run an algorithm after adding some noise to the data, or using different parameter settings, and then compare the outcomes. The idea is that if many algorithm parameters and many perturbations of the data return the same result, the result is likely to be trustworthy. If the grouping changes whenever the data is nudged slightly, it is less likely to reflect real structure.

What No Metric Can Confirm

Even a very robust clustering or a very high silhouette score does not tell you whether the clusters carry any meaning. The source illustrates this with face images. We might hope to find groups such as men and women, older and younger people, or people with and without beards. Suppose all the algorithms agree on which points belong together. The clusters could still reflect side views versus front views, pictures taken at night versus during the day, or pictures taken with one brand of phone versus another.

The practical lesson is that no metric can confirm that a cluster has semantic meaning. Manual analysis of the clusters is still needed. You must look at what the points in each group actually have in common before you attach a name to it.

Tracing a DBSCAN Run

The source applies k-means, DBSCAN, and agglomerative clustering to the Labeled Faces in the Wild dataset. Before clustering, it represents the images with eigenfaces, produced by PCA with whiten=True and 100 components. This representation is more semantic than raw pixels and makes the computation faster. The DBSCAN run begins with default parameters, and that first run is the one to trace closely. The trace below follows what the labels tell you at each step.

Default eps andmin_samplesstarting parametersRun DBSCANon eigenface featuresLabel setonly -1 returnedEvery image noiseno cluster formedRaise epswidens each neighborhoodLower min_samplessmaller groups can countCheck label setcompare with expectationapplyinspectonly -1adjustadjustrerunrepeat if still noise
At which step does the label set diverge from what was expected, and how do changes to eps and min_samples alter that step?

The divergence happens at the label set step. The expectation is that a dataset of many faces should produce some groups, but the output contains only -1. In DBSCAN output as the source presents it, -1 marks noise, so every single image has been classified as noise and no cluster was formed at all. The labels have diverged from what you expected at this exact point, and everything after it is a question of which parameter caused the problem.

Worked example

Diagnosing an all-noise DBSCAN result

You run DBSCAN with default parameters on the eigenface features and inspect the labels. The only label returned is -1. What should you change?

  1. 1
    Read the output

    The only label is -1, which means every image is noise. No cluster exists in the result, so there is nothing to evaluate yet.

  2. 2
    Identify the two levers

    The source names eps, which sets the size of each point's neighborhood, and min_samples, which sets how many points a group needs before it counts as a cluster.

  3. 3
    Ask which lever to move first

    Making eps higher widens each neighborhood, so points have more neighbors. Making min_samples lower lets smaller groups count as clusters. Both changes make it easier for a group to form.

  4. 4
    Change one lever and rerun

    Raise eps, rerun, and inspect the labels again. If the label set is still only -1, lower min_samples and rerun. Change one parameter at a time so you can see which change had an effect.

  5. 5
    Check the new labels

    Compare the label set with what you expect from the data. Whether clusters now appear, and whether they match the structure you expected, decides whether the parameters are close enough.

Result

The all-noise result points to eps and min_samples as the place to adjust. Raising eps and lowering min_samples both make group formation easier, and you verify the change by inspecting the labels after each rerun.

How eps and min_samples Change the Clusters

The two parameters pull in different directions in your search for a useful result. eps controls how far a point looks for neighbors, so it governs how large a neighborhood is. min_samples controls how many points a group must contain before it counts as a cluster. Because they interact, you should adjust them deliberately and then inspect the labels after each change rather than assuming the effect.

eps defaultsmall neighborhoodseps raisedwider neighborhoodsmin_samples defaultlarger groups requiredmin_samples loweredsmaller groups allowedLabel set: -1 onlyevery image noiseLabel set recheckedcompare with expectedgroupsleads toleads toinspectinspect
How do the cluster assignments change when eps or min_samples is increased or decreased on the same dataset?

The before-and-after view shows the same dataset under two parameter settings. The default settings produce a label set containing only -1. Raising eps widens each neighborhood, and lowering min_samples lets smaller groups count as clusters. Whether clusters actually appear, and how many, depends on the data, so the after state is something you check by reading the labels rather than something you can assume from the parameter change alone.

Mistakes Beginners Make When Judging DBSCAN

  • Common mistake

    Treating a high silhouette score as proof of the best grouping

    What it looks like

    An elongated, curved grouping scores lower than tight roundish blobs, so the silhouette score prefers the blobs.

    Why it fails

    The silhouette coefficient measures compactness, and compactness does not allow for complex shapes.

    Do this instead

    Use the silhouette score as one signal, and inspect the clusters to see whether their shape matches the data.

  • Common mistake

    Expecting ARI or NMI on a real, unlabeled problem

    What it looks like

    Computing ARI on a new customer dataset that has no reference grouping.

    Why it fails

    ARI and NMI compare against a ground truth clustering, and real applications usually have none.

    Do this instead

    Use scoring metrics that need no ground truth, plus manual analysis, and reserve ARI and NMI for algorithm development with a known reference.

  • Common mistake

    Assuming a stable-looking result is meaningful

    What it looks like

    A grouping that changes when the data is nudged slightly is accepted as real structure.

    Why it fails

    A grouping that changes under small perturbations is less likely to reflect real structure.

    Do this instead

    Rerun with small noise or different settings and check whether the grouping holds steady.

  • Common mistake

    Reading -1 as a cluster instead of noise

    What it looks like

    Seeing only -1 in the labels and reporting one large cluster.

    Why it fails

    In the source's DBSCAN output, -1 marks points classified as noise.

    Do this instead

    Count how many labels are -1 and whether any other labels appear before drawing conclusions.

  • Common mistake

    Changing both parameters at once

    What it looks like

    Raising eps and lowering min_samples in one step, then not knowing which change produced the new labels.

    Why it fails

    Both levers affect group formation, so combined changes hide which one had an effect.

    Do this instead

    Change one parameter, rerun, and inspect the labels before changing the other.

Practice: Diagnosing a Clustering Run

MEDIUM

You run DBSCAN on a dataset with default parameters and get only -1 labels. You then raise eps and get the same result. Describe your next step, and explain why you would not conclude that the data has no structure after just these two runs.

Hints
  • Recall what the source says eps and min_samples control.
  • Think about changing one parameter at a time and inspecting the label set after each rerun.
  • A single all-noise result does not show that no groups exist in the data.
MEDIUM

Write a short plan for judging a DBSCAN clustering on a dataset with no ground truth. Name at least one score that does not need ground truth, one robustness check you would perform by hand, and one manual check on the meaning of the clusters.

Hints
  • The silhouette coefficient needs no ground truth.
  • Robustness checks rerun the algorithm with noise or different parameter settings.
  • No metric can confirm that a cluster carries semantic meaning.

Key Takeaways

  • ARI and NMI compare a clustering against a ground truth clustering and give a value between 0 and 1, but real applications rarely have ground truth to compare against.
  • The silhouette coefficient needs no ground truth and rewards compactness, so it can rank tight roundish blobs above a curved grouping that matches the data's structure.
  • Robustness-based metrics check whether a result stays stable under noise or parameter changes, and they are not built into scikit-learn at the time of writing.
  • In DBSCAN, a label set containing only -1 means every point is noise; eps and min_samples are the levers to inspect, and you should change one at a time and check the labels after each rerun.
  • No metric can confirm that a cluster has semantic meaning, so manual analysis of the clusters is still required.

Related concepts

More concepts starting with D