learning-it by example

Back to the video

Clustering: k-means and When It Fails, in Python

k-means matched 299 of 300 points on round clouds, but turned a small cloud of 40 into a cluster of 102.

Video #193 ยท Machine learning and data science

Steps in the video

  1. 0:00 k-means: 299 of 300 right, then two clean failures
  2. 0:32 The idea
  3. 1:06 Setup
  4. 1:24 Clouds and rings
  5. 1:47 A big cloud and a small one
  6. 2:03 Make it repeatable
  7. 2:32 Three cases and a fix
  8. 2:54 Big and small
  9. 3:50 What this does not show
  10. 4:31 Mistakes to avoid
  11. 5:15 My take and next steps
  12. 5:41 Recap

Files

  • check.py (23 lines)
  • data.py (26 lines)
  • run.py (24 lines)

Download the code (zip)

Tested with

Python 3.12.15 | numpy==2.5.4, scikit-learn==1.9.1, scipy==1.18.1, joblib==1.6.0, narwhals==2.27.1, threadpoolctl==3.7.0, cloudpickle==3.1.2

What this does not show

Made-up two-dimensional points with a known answer; choosing k, scaling and other clustering methods are not shown.

Sources

Learning IT by Example.