Skip to main content

minibatch-kmeans

PyPI Python License

minibatch-kmeans is mini-batch k-means clustering for PyTorch tensors. The package follows Sculley's Web-Scale K-Means Clustering (WWW 2010). scikit-learn's MiniBatchKMeans uses the same algorithm, but it runs on CPU only.

Features

  • CPU and GPU support. The model works directly on PyTorch tensors, on any device.
  • Training on new data. partial_fit updates the model one batch at a time, so your full dataset never has to fit in memory.
  • Save and load. You can save a model and continue training it later.

You can read the full API reference at mb-14.github.io/minibatch-kmeans.

How it works

Each training step picks a small random batch of points. Each point is assigned to its nearest center, and then each center moves toward the points assigned to it. Every center keeps a count of the points it has seen. Its learning rate is 1 divided by that count, so the center is always the average of all the points it has received, and it moves less as training goes on.

Algorithm 1, mini-batch k-means, adapted from Sculley (2010).

Adapted from Algorithm 1 in D. Sculley, Web-Scale K-Means Clustering, WWW 2010.

Install

You need Python 3.10 or newer. Install a PyTorch 2.x build for your machine first, using the selector on pytorch.org. Then install the package:

pip install minibatch-kmeans

Quick start

Fit a tensor

import torch
from minibatch_kmeans import MiniBatchKMeans

X = torch.randn(5000, 4, dtype=torch.float32)

km = MiniBatchKMeans(n_clusters=8, random_state=torch.Generator().manual_seed(42))
km.fit(X, batch_size=256, max_iter=20)
labels = km.predict(X)

print(labels.shape)              # torch.Size([5000])
print(km.cluster_centers.shape)  # torch.Size([8, 4])

fit returns the centers, and predict returns one label per row. fit_predict does both steps in one call. The input dtype must match the model's dtype, which is torch.float32 by default. Pass a seeded CPU torch.Generator as random_state when you need the same result on every run.

Run on a GPU

Set device on the model and move your data to the same device:

km = MiniBatchKMeans(n_clusters=8, device=torch.device("cuda"))
labels = km.fit_predict(X.cuda(), batch_size=256)

Train on new batches

partial_fit updates the centers with every row of the batch you pass in. Use it when data arrives as a stream or does not fit in memory.

km = MiniBatchKMeans(n_clusters=8)
for batch in X.split(256):
    km.partial_fit(batch)
labels = km.predict(X)

The notebook notebooks/partial_fit_demo.ipynb walks through this step by step.

Save and load a model

km.save("kmeans.pt")

km2 = MiniBatchKMeans.load("kmeans.pt")                                  # loads on CPU
km_gpu = MiniBatchKMeans.load("kmeans.pt", device=torch.device("cuda"))  # loads on a GPU

Benchmarks

See evaluation/README.md.

Development

Install the package in editable mode with the dev extra, then run the tests:

conda run -n kmeans-bench pip install -e ".[dev]"
conda run -n kmeans-bench python -m unittest discover -s tests -v

To build the API docs locally into pdoc_html/, run:

conda run -n kmeans-bench bash scripts/gen_api_docs.sh

License

This project uses the MIT License.

Metadata

Release files for minibatch-kmeans 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for minibatch-kmeans 0.1.2
File Size Uploaded Provenance
minibatch_kmeans-0.1.2.tar.gz 33.0 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for minibatch-kmeans 0.1.2
File Interpreter ABI Platform
minibatch_kmeans-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 64.5 kB

Release files / minibatch_kmeans-0.1.2.tar.gz

Download URL minibatch_kmeans-0.1.2.tar.gz
Size 33.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ff9557de54bfcdf5add54b87ef15cda9a9bbf64409a1176b7d297349675523f8
BLAKE2b-256 checksum
How to use checksums
1600b7f9f3dfe31b6486ea7fbae58a491176ade9637bb8845b4afbb9f2b0e155
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.

Transparency log

Release files / minibatch_kmeans-0.1.2-py3-none-any.whl

Download URL minibatch_kmeans-0.1.2-py3-none-any.whl
Size 31.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ca39f5aeeef73285aa8989cda3cded281e393594e191e60a05af7eac35efc00
BLAKE2b-256 checksum
How to use checksums
334a1e2ef71ce5aa567dc3207d459d6d9d776c1a8edf4623c70e0c630e5a373e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.

Transparency log

Security for minibatch-kmeans 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for minibatch_kmeans-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page