DBSCAN and OPTICS Density-Based Clustering

MetaCyberGuru Academy

IntermediateEstimated learning effort: 90 minutesFree, no sign-up requiredPublished by Muhammad AzharCourse version: August 2026

Back to Clustering and Dimensionality Reduction

Density methods can discover irregular shapes and label sparse points as noise. Their power comes with a hard question: what density should count as a cluster across the whole dataset?

Reason about density before tuning eps

You will fit DBSCAN, inspect core and noise points, then use OPTICS when one global neighbourhood radius is too restrictive.

  • Define core, border and noise points from eps and min_samples.
  • Explain how scaling and dimensionality change density.
  • Use neighbour-distance plots and domain scale to propose parameters.
  • Distinguish DBSCAN’s global density threshold from OPTICS reachability structure.

Density is local evidence under a chosen scale

A DBSCAN core point has at least min_samples points, including itself, within radius eps. Border points fall within a core point’s neighbourhood but do not have enough neighbours to be core themselves. Remaining points are labelled noise.

A small eps fragments data and labels many points noise. A large eps can merge distinct groups. min_samples controls how much local support is required. Higher-dimensional spaces often need more evidence, while distances become less discriminating. Feature selection and scaling are part of parameter selection.

DBSCAN does not require the number of clusters and can capture non-convex shapes. It struggles when valid groups have very different density because one eps must serve all of them.

OPTICS orders points and records reachability over a range of neighbourhood scales. Its reachability plot exposes dense valleys, and scikit-learn can extract clusters. It is not a parameter-free proof of structure; min_samples, extraction method and distance still matter.

Grid-based methods such as STING aggregate space into cells, while CLIQUE searches dense units in subspaces. They address scale and high-dimensional structure differently. Treat them as design alternatives, not drop-in replacements for DBSCAN.

Separate two dense groups and one isolated point

The program reports label -1 as noise and confirms how many core samples support the two groups.

Install the lesson dependencies

python -m pip install numpy scikit-learn

Fit DBSCAN after scaling

import numpy as np
from sklearn.cluster import DBSCAN, OPTICS
from sklearn.preprocessing import StandardScaler

X = np.array([
    [1.0, 1.0], [1.1, 0.9], [0.9, 1.1], [1.2, 1.0],
    [5.0, 5.0], [5.1, 4.9], [4.9, 5.1], [5.2, 5.0],
    [9.0, 1.0],
])
X_ready = StandardScaler().fit_transform(X)

dbscan = DBSCAN(eps=0.22, min_samples=3).fit(X_ready)
print("DBSCAN labels:", dbscan.labels_.tolist())
print("core samples:", len(dbscan.core_sample_indices_))
print("noise points:", int(np.sum(dbscan.labels_ == -1)))

optics = OPTICS(min_samples=3, max_eps=1.5).fit(X_ready)
print("OPTICS ordering length:", len(optics.ordering_))

Expected structure

DBSCAN labels: <four matching labels, four matching labels, -1>
core samples: 8
noise points: 1
OPTICS ordering length: 9

Small library-version or floating-point differences can change points near eps. Do not publish a parameter that depends on a fragile equality without a sensitivity test.

When almost everything is noise or one cluster

This is usually a scale, representation or parameter diagnosis, not a reason to hide the result.

  • Print neighbour-distance quantiles before changing eps.
  • Check whether one feature or unit dominates distance.
  • Inspect duplicate points because they can create artificial dense regions.
  • Run sensitivity across nearby eps and min_samples values and report unstable records.

Create a density sensitivity map

Generate two moon-shaped groups plus noise. Fit DBSCAN across a small parameter grid and compare with k-means.

  • Record cluster count, noise share and silhouette on non-noise points.
  • Plot core, border and noise points distinctly.
  • Run OPTICS and interpret one reachability valley.
  • Explain why the chosen geometry matches the data-generating story.

What the report should show

  • Parameter grid results.
  • Core-border-noise diagram.
  • One point whose assignment is unstable near the threshold.

Knowledge check

1. What label does scikit-learn DBSCAN use for noise?
Check your reasoning

Cluster labels are non-negative while noise is represented by -1.

2. Why can DBSCAN struggle with variable density?
Check your reasoning

One neighbourhood radius may be too small for a sparse cluster and too large for a dense one.

3. What does a core point have?
Check your reasoning

Core support is defined locally by eps and min_samples.

Official references and further reading

Review note for DBSCAN and OPTICS Density-Based Clustering: recheck the linked documentation after a dependency changes the relevant API, metric or modelling assumption, then record the tested version beside your result.

Save your place

Completion is stored only in this browser on this device.

Share this page

Share this page with the people who will use it next.

X Facebook LinkedIn WhatsApp Email

Discussion

No comments yet. Add the first useful question or observation.