milia.cluster.models.dbscan#

DBSCAN, with eps chosen from the k-distance elbow.

Functions

elbow(X, min_samples)

Eps at the elbow of the sorted k-distance curve (Ester et al., 1996).

Classes

DBSCAN([eps, min_samples, metric])

Density-based clustering; points in no dense region are noise (-1).

DBSCANConfig(*[, name, kind, space, eps, ...])

Validated hyperparameters of the DBSCAN clusterer.

milia.cluster.models.dbscan.elbow(X, min_samples)#

Eps at the elbow of the sorted k-distance curve (Ester et al., 1996).

The k-distance of each point is its distance to its min_samples-th neighbour; the elbow is the point of largest second difference of the sorted curve.

Parameters:
  • X (ndarray) – Data.

  • min_samples (int) – Neighbour rank used for the k-distance.

Return type:

float

Returns:

A positive eps.

Examples

>>> import numpy as np
>>> X = np.r_[np.zeros((20, 2)), np.full((20, 2), 9.0)] + np.random.default_rng(0).normal(
...     scale=0.2, size=(40, 2)
... )
>>> 0 < elbow(X, 5) < 9
True
class milia.cluster.models.dbscan.DBSCAN(eps=None, min_samples=5, metric='euclidean')#

Bases: BaseClusterer

Density-based clustering; points in no dense region are noise (-1).

Parameters:
  • eps (float | None) – Neighbourhood radius; None picks it from the k-distance elbow (the chosen value is eps_).

  • min_samples (int) – Points needed to form a dense region.

  • metric (str) – Distance metric.

Examples

>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> X = np.repeat(np.array([[0.0, 0.0], [6.0, 6.0], [0.0, 6.0]]), 20, axis=0) + rng.normal(
...     scale=0.4, size=(60, 2)
... )
>>> DBSCAN(min_samples=5).fit(X).n_clusters_
3
class milia.cluster.models.dbscan.DBSCANConfig(*, name='dbscan', kind='clusterer', space=SearchSpace(min_samples=(3, 50)), eps=None, min_samples=5, metric='euclidean')#

Bases: BaseModelConfig

Validated hyperparameters of the DBSCAN clusterer.

Fields mirror DBSCAN’s constructor; space is the default Optuna search space. Build the clusterer with make("clusterer", "dbscan", ...) or DBSCAN(**config.params()).

Examples

>>> DBSCANConfig().name
'dbscan'