Add Hamerly's accelerated K-means algorithm to linfa-clustering - #439
Merged
Conversation
relf
reviewed
May 6, 2026
relf
left a comment
Member
There was a problem hiding this comment.
Thanks for this contribution which looks good to me. Could you also rebase your PR on the current master? (some new lints have been fixed)
Implement K-means Hamerly's triangle-inequality optimization as an alternative to Lloyd's algorithm for K-means clustering. For each observation the algorithm maintains upper/lower distance bounds and skips centroid comparisons that cannot change the assignment, yielding the same results as Lloyd but with significantly fewer distance computations when clusters are well separated. Key changes: - The new Hamerly K-means algorithm - Add KMeansAlgorithm enum (Lloyd | Hamerly) and .algorithm() builder method - Reject Hamerly for incremental fit_with - Comprehensive tests
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #439 +/- ##
==========================================
+ Coverage 76.98% 77.54% +0.55%
==========================================
Files 106 106
Lines 7405 7579 +174
==========================================
+ Hits 5701 5877 +176
+ Misses 1704 1702 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
Author
relf
approved these changes
May 11, 2026
jagan-nuvai
added a commit
to Nuvai/linfa
that referenced
this pull request
Jul 14, 2026
Pulls in: - e69cd94 linting - 8167a62 fix docs action - 99d8afd Hamerly accelerated K-means (rust-ml#439) - 793a8a8 WASM feature support (rust-ml#438) - ad45826 remove debug trace - 60382a3 Wasserstein distance / Earth Mover's distance (rust-ml#440) - 7fe5c86 log-sum-exp overflow fix (rust-ml#444) Adapted upstream's Hamerly code to work with our ndarray 0.17 / rand 0.9 upgrade. Key changes: - Added F: Weight bound where upstream used F: Float (rand 0.9 API) - Fixed Uniform::new() calls to .unwrap() (rand 0.9 returns Result) - Kept our upgraded dependency versions in Cargo.toml - Added upstream's KMeansAlgorithm enum and Hamerly structs
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.

Implement K-means Hamerly's triangle-inequality optimization as an alternative to Lloyd's algorithm for K-means clustering for speed optimization. For each observation the algorithm maintains upper/lower distance bounds and skips centroid comparisons that cannot change the assignment, yielding the same results as Lloyd but with significantly fewer distance computations when clusters are well separated.
Key changes:
Here are the benchmarks between Lloyd and Hamerly