Skip to content

machine_learning: add numeric doctests to k_means_clust - #15285

Merged
cclauss merged 1 commit into
TheAlgorithms:masterfrom
priya-sundaram-dev:kmeans-doctests
Sep 12, 2026
Merged

machine_learning: add numeric doctests to k_means_clust#15285
cclauss merged 1 commit into
TheAlgorithms:masterfrom
priya-sundaram-dev:kmeans-doctests

Conversation

@priya-sundaram-dev

@priya-sundaram-dev priya-sundaram-dev commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #13919 (per that discussion, one focused per-file PR rather than a broad umbrella change).

machine_learning/k_means_clust.py had no doctests, so nothing pinned its numeric behaviour. This adds doctests to the deterministic core functions using a tiny fixed 2-cluster dataset:

  • assign_clusters — nearest-centroid labels → [0, 0, 1, 1]
  • revise_centroids — per-cluster means → [[0.0, 0.5], [10.0, 10.5]]
  • compute_heterogeneity — sum of squared distances to assigned centroid → 1.0
  • kmeans — end-to-end on fixed initial centroids, pinning both returned centroids and assignments

The dataset is chosen so the expected values are easy to verify by hand, and outputs are converted with .tolist() / float() so the doctests are exact and platform-independent. All doctests pass locally (python -m doctest / testmod), and ruff check/ruff format are clean.

No behaviour changes — documentation/tests only.

Fixes #13919

@cclauss
cclauss merged commit 3e34e8e into TheAlgorithms:master Sep 12, 2026
6 checks passed
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.

Improve Test Coverage and Documentation for Machine Learning Algorithms

2 participants