Clustering, Neighbors & NB
OPTICS
Ordering Points To Identify the Clustering Structure for variable-density clustering with xi reachability extraction.
Overview & Backend Support
OPTICS generalizes DBSCAN to handle clusters of varying densities by ordering samples according to their reachability distances and extracting clusters via the xi steepness criterion.
- Backend primitives:
N3(pairwise_l2_sq) andN4(topk_argmin) 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),OPTICS.load(path, device=...),ochreml.save(), andochreml.load().
Import & Signature
from ochreml.cluster import OPTICS
# also available from:
from ochreml.cluster_advanced import OPTICS
from ochreml import OPTICS
OPTICS(
min_samples: int = 5,
max_eps: float = math.inf,
metric: str = "euclidean",
xi: float = 0.05,
min_cluster_size: int | None = None,
device: str | None = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
min_samples | int | 5 | Number of samples in a neighborhood for a point to be considered a core point. |
max_eps | float | inf | Maximum distance between two samples for one to be considered in the neighborhood of the other. |
metric | {"euclidean", "manhattan", "cosine"} | "euclidean" | Distance metric used for pairwise distance calculation. |
xi | float | 0.05 | Minimum steepness on the reachability plot that constitutes a cluster boundary. |
min_cluster_size | int | None | None | Minimum number of samples required to form an OPTICS cluster. |
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) extracted via xi; noise points are labeled -1. |
reachability_ | ndarray of shape (n_samples,) | None | Reachability distances (float64) per sample, indexed by original sample order. |
core_distances_ | ndarray of shape (n_samples,) | None | Distance (float64) at which each sample becomes a core point (inf if none within max_eps). |
ordering_ | ndarray of shape (n_samples,) | None | Cluster-ordered list of sample indices (int64). |
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) -> OPTICS | Perform OPTICS ordering and cluster extraction on X. |
fit_predict | fit_predict(X, y=None) -> ndarray | Fit OPTICS 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 | OPTICS.load(path: str, device: str | None = None) -> OPTICS | Load a fitted estimator from a binary .dit file. |
to | to(device: str) -> OPTICS | Move estimator compute target to another hardware device. |
Example
import numpy as np
from ochreml.cluster import OPTICS
rng = np.random.default_rng(0)
X = np.vstack([
rng.normal(0.0, 0.2, (15, 2)),
rng.normal(5.0, 0.5, (15, 2)),
])
optics = OPTICS(min_samples=4, xi=0.05, min_cluster_size=5)
labels = optics.fit_predict(X)
print("Labels:", labels)
print("Ordering (first 10):", optics.ordering_[:10])
print("Reachability (first 5):", optics.reachability_[:5])