DBSCANΒΆ

The DBSCAN clustering algorithm looks for groups of data points that are densely packed. It makes no assumptions about the number of clusters.

Instead, you define two hyperparameters:

  • the minimum number of points per cluster

  • a cutoff distance

The DBSCAN algorithm determines the clusters from that. Each data point with at least the minimum number of points within the cutoff distance is assigned to the same cluster as the nearby points.

Thus, the two hyperparameters control the number of clusters indirectly.

Data points that do not have sufficient neighbors, are marked as outliers, as shown by this code example:


from sklearn.datasets import make_moons
import numpy as np
import matplotlib.pyplot as plt

from sklearn.cluster import DBSCAN

X, _ = make_moons(1000, noise=0.2)
outliers = np.random.normal(0.0, 1.0, (10, 2))

x = np.concatenate([X, outliers])


m = DBSCAN(eps=0.3)
m.fit(x)

c = m.fit_predict(x)

cols = np.array(['blue', 'red'])
c = m.labels_.astype(np.int)

plt.scatter(x[:,0], x[:,1], c=cols[c])
plt.show()

Hint

DBSCAN can be used for outlier detection!