Memory-efficient clustering
Large datasets that cannot be conveniently retained as individual points during clustering.
Scikit-Learn · Class
Birch is a memory-efficient, online clustering estimator that incrementally builds a Clustering Feature Tree. It can produce final clusters or compress data into subclusters for a later clustering algorithm.
Birch incrementally compresses samples into a Clustering Feature Tree. It merges each incoming sample into the nearest eligible subcluster, updates that subcluster’s summary statistics, and splits nodes when their subcluster count exceeds the branching factor; leaf subclusters can then be used directly or passed to a final clustering estimator.
class sklearn.cluster.Birch(*, threshold=0.5, branching_factor=50, n_clusters=3, compute_labels=True)| Name | Type | Description |
|---|---|---|
threshold | float | Radius limit for the subcluster formed by merging a sample with its closest subcluster; default 0.5. |
branching_factor | int | Maximum number of CF subclusters per node; default 50. |
n_clusters | int, clustering estimator, or None | Number of clusters for the final step, a clustering estimator, or None; default 3. |
compute_labels | bool | Whether to compute labels during fitting; default True. |
A fitted Birch estimator; fit_predict returns cluster labels, while transform returns distances to cluster centroids.
from sklearn.cluster import Birch
from sklearn.datasets import make_blobs
X, _ = make_blobs(n_samples=300, centers=3, random_state=0)
model = Birch(n_clusters=3)
labels = model.fit_predict(X)
print(labels.shape)
This fits a BIRCH clustering model to synthetic data and returns one cluster label per sample.
Large datasets that cannot be conveniently retained as individual points during clustering.
Streaming or chunked input processed through repeated partial_fit calls.
Reducing many observations to representative leaf subclusters before another clustering method.
Datasets with relatively few features where a scalable alternative to MiniBatchKMeans is suitable.
Birch builds a memory-efficient Clustering Feature Tree rather than retaining every input point. Its leaf subclusters provide compressed representatives that can be used as final clusters or supplied to another clustering algorithm.
Follow-up: How would you process data in chunks and choose the final cluster count? Initialize with n_clusters=None, call partial_fit repeatedly, set n_clusters with set_params, then call partial_fit once without arguments.
threshold controls the permitted radius when a new sample is merged into its closest subcluster. branching_factor caps the number of CF subclusters in each node; exceeding it triggers a node split.
Follow-up: What practical effect does lowering threshold have? It promotes splitting and therefore generally creates more, smaller subclusters.
n_clusters specifies the number of clusters in the final clustering step, and it may be an integer, a clustering estimator, or None. compute_labels controls whether labels are calculated during fitting.
Follow-up: When is Birch useful as preprocessing? Set n_clusters=None to reduce many samples to leaf subclusters, then cluster those representatives separately.
MiniBatchKMeansAgglomerativeClusteringpartial_fitfitpredicttransform