Skip to content

Add K-Means & K-Medians clustering algorithm - #7552

Open
Ashish2343 wants to merge 1 commit into
TheAlgorithms:masterfrom
Ashish2343:clustring
Open

Add K-Means & K-Medians clustering algorithm#7552
Ashish2343 wants to merge 1 commit into
TheAlgorithms:masterfrom
Ashish2343:clustring

Conversation

@Ashish2343

Copy link
Copy Markdown

Add K-Means and K-Medians clustering (machinelearning package)

Adds a new machinelearning package with Clustring, providing two centroid-based clustering algorithms that share one iterative core (assign → update → check convergence):

  • K-Means — squared Euclidean distance, centers = mean
  • K-Medians — Manhattan distance, centers = median (more robust to outliers)

Both support deterministic initial centers (for reproducible tests) or seeded random init. Includes input validation (empty dataset, invalid k, ragged dimensions, mismatched center count) and a full JUnit 5 test suite (ClustringTest) covering both algorithms, edge cases (k=1, k=n), and an outlier-robustness comparison.

Closes #7534

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.28571% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.51%. Comparing base (7c934ad) to head (fde018a).

Files with missing lines Patch % Lines
.../com/thealgorithms/machinelearning/Clustering.java 94.28% 2 Missing and 6 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7552      +/-   ##
============================================
+ Coverage     80.42%   80.51%   +0.09%     
- Complexity     7457     7507      +50     
============================================
  Files           815      816       +1     
  Lines         24055    24195     +140     
  Branches       4732     4763      +31     
============================================
+ Hits          19346    19481     +135     
- Misses         3945     3946       +1     
- Partials        764      768       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alxkm

alxkm commented Aug 3, 2026

Copy link
Copy Markdown
Member

@Ashish2343, before a detailed review, the Clang formatter needs to be fixed.

And few quick suggestions regarding code.

  • Typo in the test class name: class ClustringTest inside ClusteringTest.java
  • Missing Javadoc on one overload: kMeans(double[][], int k, long seed, int, double) - the other three have it. Copy the wording from the seeded kMedians.
  • The empty-cluster branch in updateCenters isn't covered. One test is enough.
  • Variables centroids vs centers. Right now k-means takes initialCentroids, k-medians takes initialCenters, the shared internals say centers, yet the helpers are named validateAndCopyCentroids / randomInitialCentroids even though both paths call them. Terminologically "centroid" means the mean, so it's wrong for medians. Pick centers as the shared vocabulary (internals + helpers) and keep centroids only in the public k-means signature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[OTHER]

3 participants