Clustering, Neighbors & NB
AgglomerativeClustering
Hierarchical bottom-up agglomerative clustering using Lance-Williams linkage updates and pairwise distance primitives.
Overview & Backend Support
AgglomerativeClustering recursively merges pairs of clusters from the bottom up according to a linkage criterion ("ward", "complete", "average", or "single") until n_clusters remain, recording the full merge tree in children_.
- Backend primitives:
N3(pairwise_l2_sq) for initial pairwise Euclidean distances across CPU (Rayon), CUDA,wgpu, and Google TPU, followed by Lance-Williams linkage updates."manhattan"and"cosine"metrics are also supported for non-Ward linkages. - Incremental learning (
partial_fit): Not supported (transductive clustering viafit/fit_predict). - Persistence: Supports binary
.ditserialization viasave(path),AgglomerativeClustering.load(path, device=...),ochreml.save(), andochreml.load().
Import & Signature
from ochreml.cluster import AgglomerativeClustering
# also available from:
from ochreml.cluster_advanced import AgglomerativeClustering
from ochreml import AgglomerativeClustering
AgglomerativeClustering(
n_clusters: int = 2,
metric: str = "euclidean",
linkage: str = "ward",
device: str | None = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
n_clusters | int | 2 | Number of clusters to find. |
metric | {"euclidean", "manhattan", "cosine"} | "euclidean" | Distance metric used for linkage. When linkage="ward", only "euclidean" is accepted. |
linkage | {"ward", "complete", "average", "single"} | "ward" | Linkage criterion determining which clusters to merge at each step. |
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 sample. |
n_leaves_ | int | Number of leaves in the hierarchical tree (n_samples). |
n_connected_components_ | int | Estimated number of connected components in the graph. |
children_ | ndarray of shape (n_samples - 1, 2) | None | Child node indices (int64) merged at each step of the hierarchy. |
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) -> AgglomerativeClustering | Fit hierarchical agglomerative clustering on X. |
fit_predict | fit_predict(X, y=None) -> ndarray | Fit the model on X and return int64 cluster labels. |
save | save(path: str) -> None | Save the fitted estimator to a binary .dit file. |
load | AgglomerativeClustering.load(path: str, device: str | None = None) -> AgglomerativeClustering | Load a fitted estimator from a binary .dit file. |
to | to(device: str) -> AgglomerativeClustering | Move estimator compute target to another hardware device. |
Example
import numpy as np
from ochreml.cluster import AgglomerativeClustering
X = np.array([
[1.0, 2.0], [1.1, 1.9], [0.9, 2.1],
[6.0, 7.0], [6.2, 6.8], [5.9, 7.1],
], dtype=np.float64)
agg = AgglomerativeClustering(n_clusters=2, metric="euclidean", linkage="ward")
labels = agg.fit_predict(X)
print("Labels:", labels)
print("Leaves:", agg.n_leaves_)
print("Merge tree (children_):\n", agg.children_)