Scalable clustering
Large datasets where full-batch K-Means is costly.
Scikit-Learn · Class
MiniBatchKMeans performs K-Means clustering with randomly sampled mini-batches instead of repeatedly processing the full dataset. It usually reduces computation time while accepting a possible decrease in clustering quality.
MiniBatchKMeans approximates K-Means by repeatedly assigning randomly sampled mini-batches to their nearest centroids and updating those centroids incrementally. It typically converges faster than standard K-Means, trading some clustering quality for lower computation time.
class sklearn.cluster.MiniBatchKMeans(n_clusters=8, *, init='k-means++', max_iter=100, batch_size=1024, verbose=0, compute_labels=True, random_state=None, tol=0.0, max_no_improvement=10, init_size=None, n_init='auto', reassignment_ratio=0.01)| Name | Type | Description |
|---|---|---|
n_clusters | int, default=8 | Number of clusters and centroids to generate; default 8. |
init | {'k-means++', 'random'}, callable or array-like of shape (n_clusters, n_features), default='k-means++' | Centroid initialization strategy; default 'k-means++'. |
max_iter | int, default=100 | Maximum iterations over the complete dataset; default 100. |
batch_size | int, default=1024 | Number of samples in each mini-batch; default 1024. |
verbose | int, default=0 | Display verbosity; default 0. |
compute_labels | bool, default=True | Whether to compute complete-dataset labels and inertia after fitting; default True. |
random_state | int, RandomState instance or None, default=None | Controls random number generation; default None. |
tol | float, default=0.0 | Relative tolerance used for convergence; default 0.0. |
max_no_improvement | int, default=10 | Maximum consecutive mini-batches without improvement before early stopping; default 10. |
init_size | int, default=None | Number of samples used to accelerate initialization; default None. |
n_init | int or 'auto', default='auto' | Number of initializations; default 'auto'. |
reassignment_ratio | float, default=0.01 | Controls the fraction of points that trigger centroid reassignment; default 0.01. |
A fitted MiniBatchKMeans estimator; fitted attributes include cluster_centers_, labels_, inertia_, and n_iter_.
from sklearn.cluster import MiniBatchKMeans
X = [[1.0, 1.0], [1.2, 0.8], [8.0, 8.0], [8.2, 7.8]]
model = MiniBatchKMeans(n_clusters=2, batch_size=2, n_init="auto", random_state=0)
model.fit(X)
print(model.labels_)
print(model.cluster_centers_)Creates two clusters, fits them using small randomly sampled batches, and prints the resulting assignments and centroids.
Large datasets where full-batch K-Means is costly.
Streaming or chunked training workflows.
Large sparse text or feature representations.
MiniBatchKMeans is a K-Means variant that trains on randomly sampled subsets rather than the complete dataset at each iteration. Each batch is assigned to its nearest centroid, and centroids are updated incrementally using the samples assigned to them.
Follow-up: Why can it be faster? Mini-batches reduce the computation required in each iteration, although the resulting solution can be slightly worse.
n_clusters specifies both the number of clusters to form and the number of centroids to generate. The default is 8, so it should be set to a value appropriate for the intended grouping.
Follow-up: What happens if the chosen value is unsuitable? The estimator still optimizes that requested number of centroids, but the resulting partition may not represent the desired structure.
batch_size controls the number of samples processed in each mini-batch and defaults to 1024. Larger batches can change the computation-versus-update trade-off while preserving the mini-batch training approach.
Follow-up: How is the algorithm stopped? It stops on convergence or after the configured maximum number of iterations, with early-stopping heuristics also available through parameters such as max_no_improvement.
KMeansBirchMiniBatchKMeans.fitMiniBatchKMeans.partial_fitMiniBatchKMeans.predictMiniBatchKMeans.transform