Skip to Content
OchreML
Clustering, Neighbors & NB

DBSCAN

Density-Based Spatial Clustering of Applications with Noise backed by chunked pairwise distance primitives.

Overview & Backend Support

DBSCAN discovers arbitrarily shaped clusters as contiguous high-density regions separated by low-density areas, automatically marking isolated points as noise (-1). It does not require specifying the number of clusters in advance.

  • Backend primitives: N3 (pairwise_l2_sq) with chunked distance evaluation across CPU (Rayon), CUDA, wgpu, and Google TPU for "euclidean" distance; "manhattan" and "cosine" metrics are also supported.
  • Incremental learning (partial_fit): Not supported (transductive clustering via fit / fit_predict).
  • Persistence: Supports binary .dit serialization via save(path), DBSCAN.load(path, device=...), ochreml.save(), and ochreml.load().

Import & Signature

from ochreml.cluster import DBSCAN
# also available from:
from ochreml.cluster_advanced import DBSCAN
from ochreml import DBSCAN

DBSCAN(
    eps: float = 0.5,
    min_samples: int = 5,
    metric: str = "euclidean",
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
epsfloat0.5Maximum distance between two samples for one to be considered in the neighborhood of the other.
min_samplesint5Minimum number of samples in an eps-neighborhood (including the point itself) to qualify as a core point.
metric{"euclidean", "manhattan", "cosine"}"euclidean"Distance metric used to compute pairwise distances.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
labels_ndarray of shape (n_samples,) | NoneCluster labels (int64) for each point in the training dataset. Noise points are labeled -1.
core_sample_indices_ndarray of shape (n_core_samples,) | NoneIndices (int64) of core samples found during fit.
components_ndarray of shape (n_core_samples, n_features) | NoneCopy of each core sample found during fit (empty (0, n_features) array if no core samples exist).
n_features_in_int | NoneNumber of features seen during fit.
device_str | NoneResolved hardware device string used by the underlying Rust model.

Methods

MethodSignatureDescription
fitfit(X, y=None) -> DBSCANPerform DBSCAN clustering on X.
fit_predictfit_predict(X, y=None) -> ndarrayFit DBSCAN on X and return int64 cluster labels (-1 for noise).
savesave(path: str) -> NoneSave the fitted estimator to a binary .dit file.
loadDBSCAN.load(path: str, device: str | None = None) -> DBSCANLoad a fitted estimator from a binary .dit file.
toto(device: str) -> DBSCANMove estimator compute target to another hardware device.

Example

import numpy as np
from ochreml.cluster import DBSCAN

X = np.array([
    [0.0, 0.0], [0.1, 0.1], [0.0, 0.2], [0.2, 0.0],
    [5.0, 5.0], [5.1, 5.1], [5.0, 5.2], [5.2, 5.0],
    [20.0, 20.0],  # outlier
], dtype=np.float64)

model = DBSCAN(eps=0.5, min_samples=3, metric="euclidean")
labels = model.fit_predict(X)

print("Labels:", labels)
print("Core indices:", model.core_sample_indices_)

On this page