Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

MiniBatchKMeans

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
n_clustersint, default=8Number 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_iterint, default=100Maximum iterations over the complete dataset; default 100.
batch_sizeint, default=1024Number of samples in each mini-batch; default 1024.
verboseint, default=0Display verbosity; default 0.
compute_labelsbool, default=TrueWhether to compute complete-dataset labels and inertia after fitting; default True.
random_stateint, RandomState instance or None, default=NoneControls random number generation; default None.
tolfloat, default=0.0Relative tolerance used for convergence; default 0.0.
max_no_improvementint, default=10Maximum consecutive mini-batches without improvement before early stopping; default 10.
init_sizeint, default=NoneNumber of samples used to accelerate initialization; default None.
n_initint or 'auto', default='auto'Number of initializations; default 'auto'.
reassignment_ratiofloat, default=0.01Controls the fraction of points that trigger centroid reassignment; default 0.01.

Returns

A fitted MiniBatchKMeans estimator; fitted attributes include cluster_centers_, labels_, inertia_, and n_iter_.

Key points

  • The class performs clustering with the Mini-Batch K-Means algorithm.
  • n_clusters determines both the requested cluster count and the number of centroids.
  • batch_size sets how many samples are used in each mini-batch.
  • With compute_labels enabled, fit computes assignments and inertia for the complete dataset after optimization.
  • max_iter limits the number of complete-dataset passes independently of heuristic early stopping.
  • init_size can provide a randomly sampled subset to accelerate initialization.

Example: Fit Mini-Batch K-Means

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.

When to use it

Scalable clustering

Large datasets where full-batch K-Means is costly.

Incremental fitting

Streaming or chunked training workflows.

Document and feature clustering

Large sparse text or feature representations.

Interview questions

QHow does MiniBatchKMeans differ from KMeans?

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.

QWhat does n_clusters control?

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.

QWhat is the role of batch_size and stopping criteria?

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.

  • KMeans
  • Birch
  • MiniBatchKMeans.fit
  • MiniBatchKMeans.partial_fit
  • MiniBatchKMeans.predict
  • MiniBatchKMeans.transform

Source: MiniBatchKMeans — scikit-learn 1.9.1 documentation. Checked 2026-10-15.