Skip to Content
OchreML
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) and N4 (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 via fit / fit_predict).
  • Persistence: Supports binary .dit serialization via save(path), OPTICS.load(path, device=...), ochreml.save(), and ochreml.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

ParameterTypeDefaultDescription
min_samplesint5Number of samples in a neighborhood for a point to be considered a core point.
max_epsfloatinfMaximum 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.
xifloat0.05Minimum steepness on the reachability plot that constitutes a cluster boundary.
min_cluster_sizeint | NoneNoneMinimum number of samples required to form an OPTICS cluster.
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) extracted via xi; noise points are labeled -1.
reachability_ndarray of shape (n_samples,) | NoneReachability distances (float64) per sample, indexed by original sample order.
core_distances_ndarray of shape (n_samples,) | NoneDistance (float64) at which each sample becomes a core point (inf if none within max_eps).
ordering_ndarray of shape (n_samples,) | NoneCluster-ordered list of sample indices (int64).
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) -> OPTICSPerform OPTICS ordering and cluster extraction on X.
fit_predictfit_predict(X, y=None) -> ndarrayFit OPTICS on X and return int64 cluster labels (-1 for noise).
savesave(path: str) -> NoneSave the fitted estimator to a binary .dit file.
loadOPTICS.load(path: str, device: str | None = None) -> OPTICSLoad a fitted estimator from a binary .dit file.
toto(device: str) -> OPTICSMove 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])

On this page