Repository navigation
Conversation
Implementation of e-means (Bessa et al., 2026, arXiv:2611.09501), following the reference implementation in faiss/contrib/e_means: mini-batch k-means where each center is the ratio of exponential moving averages of the sums and weights of its samples, with a cosine decay of the learning rate, a batch size and an initial learning rate set by scaling laws, and reassignment of unused clusters by splitting. Co-Authored-By: Claude <[email protected]>
Assign the samples of a mini-batch to their closest center and sum them by cluster in Cython, from the indices of the mini-batch, instead of slicing the data and using a sparse matrix product at each step. The exponential moving average update of the centers is also done in Cython. The samples are assigned in parallel and summed sequentially, in the order of the mini-batch, so that the result does not depend on the number of threads. Co-Authored-By: Claude <[email protected]>
Keep the arithmetic of the exponential moving average update in the floating type of the data, as in the reference implementation. With this, the Cython kernel gives the same centers, bit for bit, as the previous NumPy implementation on dense data. Co-Authored-By: Claude <[email protected]>
Compare the centers with those computed by NumpyEmeans from faiss.contrib.e_means on a small problem, from the same initial centers and with the same random numbers. Co-Authored-By: Claude <[email protected]>
Add a gallery example comparing the inertia reached by EMeans, KMeans and MiniBatchKMeans for a given number of passes over the data, and state the time and memory complexity, the scalability and the parallelism of EMeans in the user guide. Co-Authored-By: Claude <[email protected]>
Owner
Author
Keep the first version of EMeans minimal: fit only accepts dense data, without sample weights nor verbose output. They can be added later without breaking anything. fit_predict and fit_transform are overridden since those of the base class pass sample_weight to fit. Co-Authored-By: Claude <[email protected]>
Remove learning_rate_init, reassignment_ratio and the "quality" and "throughput" values of batch_size, along with the learning_rate_init_ attribute. The initial learning rate, the choice of the batch size law and the reassignment of unused clusters are those of the reference implementation. Co-Authored-By: Claude <[email protected]>
The mini-batches were only assigned with several threads above an amount of work tuned on one machine. Use at most one thread per chunk of 256 samples instead, as for an iteration of Lloyd's algorithm. Also compute the squared norms of the samples only for the k-means++ initialization, which is the only place where they are used: this removes a single-threaded pass over the data from fit. Co-Authored-By: Claude <[email protected]>
Remove the user guide section and the gallery example of EMeans from this branch, along with the links to them in the docstring, to keep the first version small. They are kept on a separate branch. Co-Authored-By: Claude <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Opened inside this fork for a first look and for comparison with another prototype (
cakedev0/scikit-learn#46), as asked on the feature requestscikit-learn/scikit-learn#35383. It is not a submission to scikit-learn: that request is still labelled "Needs Decision - Include Feature".Trimmed after a first review: the parameters are down to
n_clusters,init,n_epochs,batch_sizeandrandom_state,fittakes dense data only, and the user guide section and the example moved to the branchemeans-docs.What this adds
sklearn.cluster.EMeans, an implementation of e-means from Bessa et al., "e-means: outperforming k-means (in one-tenth the epochs)", 2026. All credit for the method goes to its authors. The algorithm and its fitted constants follow their reference implementation in faiss (contrib/e_means, MIT license). No faiss code is copied.E-means is mini-batch k-means where each center is the ratio of exponential moving averages of the sums and numbers of its samples. It adds a cosine decay of the learning rate, a batch size and an initial learning rate set by scaling laws of
n_samples,n_clustersandn_epochs, a reassignment of unused clusters by splitting every 5 steps, and a closing step that sets each center to the mean of its samples of the last epoch.API:
EMeans(n_clusters=8, *, init="random", n_epochs=5, batch_size="auto", random_state=None), withfit(X, y=None). Fitted attributes:cluster_centers_,labels_,inertia_,batch_size_,n_steps_.sklearn/cluster/_e_means.py: the estimator, on_BaseKMeans, sopredict,transform,scoreand float32 behave as inKMeans.sklearn/cluster/_e_means_minibatch.pyx: one mini-batch step. The samples are assigned in parallel over chunks of 256, as in an iteration of Lloyd, then summed by cluster sequentially, in the order of the mini-batch, so that the result does not depend on the number of threads.Not included: sparse input,
sample_weight,verbose, parameters for the learning rate and for the reassignment threshold, the spherical variant,partial_fit, GPU and distributed versions, faiss'smax_points_per_centroidsubsampling, and the changelog fragment.Where to look first
NumpyEmeansbit for bit (12 fits over 6 problems, 285 reassignments, re-run after the trimming). Checked before the trimming only: the centers and running weights after every step (1,013 steps over 4 problems), and the realfaiss.knn(faiss-cpu 1.15.1), with which 8 of 14 fits are identical and the others diverge at a float32 assignment tie and end within 6e-5 relative inertia. This covers float32 data.n_features <= 2. On Yolanda it leaves e-means 1.25% above the best inertia found (see the benchmark comment).labels_andinertia_. The reference returns the assignments of the last epoch. Here, as inMiniBatchKMeans, an extra pass assigns each sample to its closest final center.Benchmarks
Fronts of inertia against fit time for
KMeans,MiniBatchKMeansandEMeansat matched budgets are in the first comment. They were measured before the trimming and have not been re-measured: the trimming removes a pass over the data fromfitand changes the number of threads used on small mini-batches. The sparse datasets and thereassignment_ratio=0.01runs of that comment use features that are no longer in this branch.This pull request includes code written with the assistance of AI.
The code has been reviewed by a human, the author of this pull request.
🤖 Generated with Claude Code