MoonSay /Tutorials/Machine Learning Interview #3
Contents
  1. 01Introduction
  2. 02What clustering is
  3. 03K-means
  4. 04Choosing K, and where K-means fails
  5. 05Gaussian mixture models and the EM algorithm
  6. 06Dirichlet process mixtures
  7. 07DBSCAN
  8. 08Hierarchical clustering
  9. 09Spectral clustering
  10. 10Putting it all together
  11. 11Interview questions and model answers
  12. 12Summary
  13. References
Clustering Algorithms/Chapter 07 of 12

07DBSCAN: a cluster is a dense region

7.1Core, border and noise points

DBSCAN (Ester et al., 1996) defines a cluster as a region where points are densely packed, separated from other clusters by sparse space. It has two parameters, a radius \varepsilon and a count minPts. The \varepsilon-neighbourhood of a point x is N_\varepsilon(x)=\{x':\|x-x'\|\le\varepsilon\}, and it includes x itself (the convention of scikit-learn’s min_samples). Then:

Definition 1. A point x is a core point if |N_\varepsilon(x)|\ge\text{minPts}. A point y is directly density-reachable from a core point x if y\in N_\varepsilon(x), and density-reachable from x if a chain of points leads from x to y in which each point is directly density-reachable from the previous one. A cluster is a maximal set of points that are density-reachable from one core point; a non-core point in a cluster is a border point; a point in no cluster is noise.

In graph language: connect every pair of core points closer than \varepsilon; each connected component of this graph is the skeleton of a cluster, and each non-core point within \varepsilon of a core point is attached to it as a border point. Ester et al. (1996) prove that a cluster is determined by any of its core points (their Lemma 2), so the result does not depend on the order in which points are visited, except that a border point within reach of two clusters goes to the one found first.

Worked example 6. Points on a line, 1,2,3,4,7,11,12,13, with \varepsilon=1.5 and minPts =3. The neighbourhoods are \{1,2\}, \{1,2,3\}, \{2,3,4\}, \{3,4\}, \{7\}, \{11,12\}, \{11,12,13\} and \{12,13\}, so the core points are 2, 3 and 12. The core points 2 and 3 are within \varepsilon of each other, so they form one cluster, to which the border points 1 and 4 attach: \{1,2,3,4\}. The core point 12 gathers the border points 11 and 13: \{11,12,13\}. The point 7 is within \varepsilon of no core point: it is noise. Note that 11 and 13 are not core points, yet they belong to a cluster; and that DBSCAN found two clusters without being told K.

7.2The algorithm and its cost

DBSCAN visits the points in turn. When it finds an unvisited core point it starts a new cluster and grows it by breadth-first search: every point in the neighbourhood of a core point in the cluster joins it, and the neighbourhoods of newly added core points are explored in turn. Each point needs one neighbourhood query. With a spatial index (such as a k-d tree or an R*-tree) a query costs about O(\log n) in low dimensions, which gives the often-quoted O(n\log n) (Ester et al., 1996); without an index, or in high dimensions where indexes stop helping, every query scans all points and the total is O(n^2). On the 12{,}512 geographic landmarks of their largest test, DBSCAN took 41.7 seconds against 80{,}638 seconds for the k-medoids method CLARANS (Ester et al., 1996, Table 1).

7.3Choosing \varepsilon and minPts

For a core point in a cluster, the distance to its k-th nearest neighbour is small and changes little with k; for a noise point it is large. Ester et al. (1996) therefore plot every point’s k-distance in decreasing order (the sorted k-distance graph) and put \varepsilon at the knee where the curve flattens: points to the left are noise, points to the right are in clusters. They found that k=4 works for two-dimensional data and fixed minPts =4; for more dimensions or noisier data a larger minPts (a common rule of thumb is about 2d) makes the result more robust. In Figure 6(b) the distance to the fifth neighbour (minPts =6 counting the point itself) crosses \varepsilon=0.19 near the knee: 31 points lie above it and are not core points.

The video’s example. Figure 6(a) shows DBSCAN with \varepsilon=0.19 and minPts =6 on 312 points: two moons (230 points), a small dense blob (60) and 22 points scattered uniformly. It finds three clusters of 116, 119 and 60 points, with 281 core points and 14 border points, and labels 17 points as noise, all of them from the scattered set (the other five scattered points happen to fall within reach of a cluster). Curved shapes, no K and explicit outliers are exactly what K-means cannot offer. The same idea finds pickup hotspots in the GPS records of a ride-sharing app, or regions of unusual activity on a map, which is the kind of spatial data DBSCAN was designed for.

DBSCAN. (a) \varepsilon=0.19 and minPts =6 on two moons, a dense blob and scattered points: three clusters, core points (filled), border points (open) and noise (crosses); the dashed circle is the \varepsilon-neighbourhood of one core point. (b) The sorted distance to the fifth nearest neighbour, used to choose \varepsilon. Data: computed for this tutorial, with the same data and parameters as the companion video; panel (b) is new.
Figure 6. DBSCAN. (a) \varepsilon=0.19 and minPts =6 on two moons, a dense blob and scattered points: three clusters, core points (filled), border points (open) and noise (crosses); the dashed circle is the \varepsilon-neighbourhood of one core point. (b) The sorted distance to the fifth nearest neighbour, used to choose \varepsilon. Data: computed for this tutorial, with the same data and parameters as the companion video; panel (b) is new.
Try itTune DBSCAN's radius and count

The 312 points of Figure 6. DBSCAN runs live as you move \varepsilon and minPts: filled dots are core points (at least minPts points within \varepsilon, counting themselves), open circles are border points, crosses are noise. The video's setting is \varepsilon=0.19, minPts =6.

7.4One density for all clusters, and the link to single linkage

One global \varepsilon means one density threshold. If one cluster is much sparser than another, an \varepsilon small enough to separate the dense clusters breaks the sparse one into noise, and an \varepsilon large enough for the sparse one merges the dense ones. Think of islands at a given sea level: DBSCAN fixes the sea level, and the islands are the clusters. DBSCAN is closely related to single-linkage clustering (Section 8): with minPts =2 every point that has a neighbour within \varepsilon is a core point, and DBSCAN’s clusters are exactly those obtained by cutting the single-linkage dendrogram at height \varepsilon, with the singletons labelled noise. A larger minPts makes this “single linkage at one height” robust to the thin bridges that cause chaining. Looking at every sea level at once gives a hierarchy again, which is the idea of OPTICS (Ankerst et al., 1999) and HDBSCAN (Campello et al., 2013): they consider all values of \varepsilon together, so clusters of different densities can be found in one run.

Pause and think. What happens to DBSCAN as \varepsilon\to0 and as \varepsilon\to\infty (with minPts fixed)?

Show the answer

For tiny \varepsilon no point has enough neighbours, so there are no core points and everything is noise. For huge \varepsilon every point is a core point and all are density-reachable from each other, so everything is one cluster. Useful values lie in between, near the knee of the k-distance graph.