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), andKMeansacross CPU (Rayon), CUDA,wgpu, and Google TPU. - Incremental learning (
partial_fit): Not supported (transductive clustering viafit/fit_predict). - Persistence: Supports binary
.ditserialization viasave(path),SpectralClustering.load(path, device=...),ochreml.save(), andochreml.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
| Parameter | Type | Default | Description |
|---|---|---|---|
n_clusters | int | 8 | Dimension of the spectral projection subspace and number of clusters to find. |
affinity | {"rbf", "nearest_neighbors"} | "rbf" | Method used to construct the pairwise affinity matrix. |
gamma | float | 1.0 | Kernel coefficient for the "rbf" affinity kernel (exp(-gamma * d^2)). |
n_neighbors | int | 10 | Number of neighbors used when affinity="nearest_neighbors". |
n_init | int | 10 | Number of K-Means runs with different centroid seeds on the spectral embedding. |
random_state | int | None | None | Random seed controlling K-Means initialization. |
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 index (int64) assigned to each training sample. |
affinity_matrix_ | ndarray of shape (n_samples, n_samples) | None | Symmetric affinity matrix constructed during fit. |
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) -> SpectralClustering | Construct the affinity matrix and perform spectral clustering on X. |
fit_predict | fit_predict(X, y=None) -> ndarray | Perform spectral clustering on X and return int64 cluster labels. |
save | save(path: str) -> None | Save the fitted estimator to a binary .dit file. |
load | SpectralClustering.load(path: str, device: str | None = None) -> SpectralClustering | Load a fitted estimator from a binary .dit file. |
to | to(device: str) -> SpectralClustering | Move 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)