DBSCAN From Scratch

Quick Overview

Implement DBSCAN clustering with NumPy and SciPy so its cluster labels and core-point mask match scikit-learn exactly, noise included. Tests the neighbourhood and core-point definitions, cluster expansion order, border-point tie handling and the cost of pairwise distances.

DBSCAN From Scratch

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `dbscan(X, eps, min_samples)` for an `(n, d)` array using Euclidean distance. Return `(labels, core_mask)`: integer cluster labels of shape `(n,)` with noise as `-1`, and a boolean array marking core points. Results must match `sklearn.cluster.DBSCAN`. The implementation must follow these rules: - The neighbourhood of a point is every point with `dist <= eps`, including the point itself. A point is core if it has at least `min_samples` neighbours. - Scan points in index order. Each unlabelled core point starts a new cluster (IDs `0, 1, 2, ...`) expanded via BFS or DFS through core points. - A border point reachable from several clusters keeps the label of the first cluster that reaches it. - Points reachable from no core point are labelled `-1`. - Do not mutate `X`. Return integer `labels` and a `bool` `core_mask`. Example: ```python X = [[0, 0], [1, 0], [2, 0], [10, 0], [11, 0], [12, 0], [6, 0]] dbscan(X, eps=1.0, min_samples=3) # labels -> [0, 0, 0, 1, 1, 1, -1] # core_mask -> [False, True, False, False, True, False, False] ``` The points at 1 and 11 each have three neighbours (themselves and the two adjacent points), so they are the only core points. The points beside them are border points of their clusters, and the point at 6 has nothing within distance 1, so it is noise. ```hint Build the neighbourhoods first Every decision depends on each point's neighbour list and core flag. Compute both before you assign any label, then ask which points are allowed to extend a cluster. ``` ```hint Trace a shared border point Picture a non-core point within `eps` of core points from two different clusters. Decide which label it should end with, and find the line in your loop that guarantees it. ``` ### Constraints & Assumptions - `1 <= n <= 500`, `eps > 0`, `min_samples >= 1`. - Do not call `sklearn` in your solution. `numpy` and `scipy` are available. ### What a Strong Answer Covers - The inclusive neighbourhood definition (`dist <= eps`, the point itself counted) and the core test. - An index-order scan that seeds a new cluster at each unlabelled core point, with IDs starting at 0. - An expansion that labels border points but continues only through core points and never overwrites a label. - Why the labels, the cluster numbering and the border tie rule agree with sklearn. - The time and memory cost of the pairwise distance matrix. ### Follow-up Questions - How would you avoid the $O(n^2)$ distance matrix for large datasets? - Why is border point assignment order-dependent, and does it matter? - How would you choose `eps` and `min_samples`?

Overview: Implement DBSCAN clustering with NumPy and SciPy so its cluster labels and core-point mask match scikit-learn exactly, noise included. Tests the neighbourhood and core-point definitions, cluster expansion order, border-point tie handling and the cost of pairwise distances.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement dbscan(X, eps, min_samples) for an (n, d) array using Euclidean distance. Return (labels, core_mask): integer cluster labels of shape (n,) with noise as -1, and a boolean array marking core points. Results must match sklearn.cluster.DBSCAN.

The implementation must follow these rules:

  • The neighbourhood of a point is every point with dist <= eps , including the point itself. A point is core if it has at least min_samples neighbours.
  • Scan points in index order. Each unlabelled core point starts a new cluster (IDs 0, 1, 2, ... ) expanded via BFS or DFS through core points.
  • A border point reachable from several clusters keeps the label of the first cluster that reaches it.
  • Points reachable from no core point are labelled -1 .
  • Do not mutate X . Return integer labels and a bool core_mask .

Example:

X = [[0, 0], [1, 0], [2, 0], [10, 0], [11, 0], [12, 0], [6, 0]]
dbscan(X, eps=1.0, min_samples=3)
# labels    -> [0, 0, 0, 1, 1, 1, -1]
# core_mask -> [False, True, False, False, True, False, False]

The points at 1 and 11 each have three neighbours (themselves and the two adjacent points), so they are the only core points. The points beside them are border points of their clusters, and the point at 6 has nothing within distance 1, so it is noise.

Constraints & Assumptions

  • 1 <= n <= 500 , eps > 0 , min_samples >= 1 .
  • Do not call sklearn in your solution. numpy and scipy are available.

What a Strong Answer Covers Guidance

  • The inclusive neighbourhood definition ( dist <= eps , the point itself counted) and the core test.
  • An index-order scan that seeds a new cluster at each unlabelled core point, with IDs starting at 0.
  • An expansion that labels border points but continues only through core points and never overwrites a label.
  • Why the labels, the cluster numbering and the border tie rule agree with sklearn.
  • The time and memory cost of the pairwise distance matrix.

Follow-up Questions Guidance

  • How would you avoid the O(n2)O(n^2) distance matrix for large datasets?
  • Why is border point assignment order-dependent, and does it matter?
  • How would you choose eps and min_samples ?