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

ParameterTypeDefaultDescription
n_clustersint2Number 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.
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 sample.
n_leaves_intNumber of leaves in the hierarchical tree (n_samples).
n_connected_components_intEstimated number of connected components in the graph.
children_ndarray of shape (n_samples - 1, 2) | NoneChild node indices (int64) merged at each step of the hierarchy.
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) -> AgglomerativeClusteringFit hierarchical agglomerative clustering on X.
fit_predictfit_predict(X, y=None) -> ndarrayFit the model on X and return int64 cluster labels.
savesave(path: str) -> NoneSave the fitted estimator to a binary .dit file.
loadAgglomerativeClustering.load(path: str, device: str | None = None) -> AgglomerativeClusteringLoad a fitted estimator from a binary .dit file.
toto(device: str) -> AgglomerativeClusteringMove 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_)

On this page