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 viafit/fit_predict). - Persistence: Supports binary
.ditserialization viasave(path),DBSCAN.load(path, device=...),ochreml.save(), andochreml.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
| Parameter | Type | Default | Description |
|---|---|---|---|
eps | float | 0.5 | Maximum distance between two samples for one to be considered in the neighborhood of the other. |
min_samples | int | 5 | Minimum 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. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
labels_ | ndarray of shape (n_samples,) | None | Cluster labels (int64) for each point in the training dataset. Noise points are labeled -1. |
core_sample_indices_ | ndarray of shape (n_core_samples,) | None | Indices (int64) of core samples found during fit. |
components_ | ndarray of shape (n_core_samples, n_features) | None | Copy of each core sample found during fit (empty (0, n_features) array if no core samples exist). |
n_features_in_ | int | None | Number of features seen during fit. |
device_ | str | None | Resolved hardware device string used by the underlying Rust model. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y=None) -> DBSCAN | Perform DBSCAN clustering on X. |
fit_predict | fit_predict(X, y=None) -> ndarray | Fit DBSCAN on X and return int64 cluster labels (-1 for noise). |
save | save(path: str) -> None | Save the fitted estimator to a binary .dit file. |
load | DBSCAN.load(path: str, device: str | None = None) -> DBSCAN | Load a fitted estimator from a binary .dit file. |
to | to(device: str) -> DBSCAN | Move 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_)