Skip to Content
OchreML
Clustering, Neighbors & NB

SpectralClustering

Graph Laplacian spectral clustering with RBF or nearest-neighbors affinity and K-Means embedding assignment.

Overview & Backend Support

SpectralClustering constructs an affinity graph over samples (using either an RBF kernel or a k-nearest-neighbors graph), computes the leading eigenvectors of the normalized graph Laplacian, and partitions the resulting spectral embedding with KMeans. It is well suited for non-convex cluster geometries such as concentric rings or moons.

  • Backend primitives: N3 (pairwise_l2_sq), N4 (topk_argmin), N5 (small_sym_eig_svd), and KMeans across CPU (Rayon), CUDA, wgpu, and Google TPU.
  • Incremental learning (partial_fit): Not supported (transductive clustering via fit / fit_predict).
  • Persistence: Supports binary .dit serialization via save(path), SpectralClustering.load(path, device=...), ochreml.save(), and ochreml.load().

Import & Signature

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

SpectralClustering(
    n_clusters: int = 8,
    affinity: str = "rbf",
    gamma: float = 1.0,
    n_neighbors: int = 10,
    n_init: int = 10,
    random_state: int | None = None,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
n_clustersint8Dimension of the spectral projection subspace and number of clusters to find.
affinity{"rbf", "nearest_neighbors"}"rbf"Method used to construct the pairwise affinity matrix.
gammafloat1.0Kernel coefficient for the "rbf" affinity kernel (exp(-gamma * d^2)).
n_neighborsint10Number of neighbors used when affinity="nearest_neighbors".
n_initint10Number of K-Means runs with different centroid seeds on the spectral embedding.
random_stateint | NoneNoneRandom seed controlling K-Means initialization.
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 index (int64) assigned to each training sample.
affinity_matrix_ndarray of shape (n_samples, n_samples) | NoneSymmetric affinity matrix constructed during fit.
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) -> SpectralClusteringConstruct the affinity matrix and perform spectral clustering on X.
fit_predictfit_predict(X, y=None) -> ndarrayPerform spectral clustering on X and return int64 cluster labels.
savesave(path: str) -> NoneSave the fitted estimator to a binary .dit file.
loadSpectralClustering.load(path: str, device: str | None = None) -> SpectralClusteringLoad a fitted estimator from a binary .dit file.
toto(device: str) -> SpectralClusteringMove estimator compute target to another hardware device.

Example

import numpy as np
from ochreml.cluster import SpectralClustering

X = np.array([
    [0.0, 0.0], [0.2, 0.1], [0.1, 0.3], [-0.1, 0.2],
    [4.0, 4.0], [4.2, 4.1], [4.1, 3.8], [3.9, 4.2],
], dtype=np.float64)

sc = SpectralClustering(n_clusters=2, affinity="rbf", gamma=1.0, random_state=42)
labels = sc.fit_predict(X)

print("Labels:", labels)
print("Affinity matrix shape:", sc.affinity_matrix_.shape)

On this page