Leanpub Header

Skip to main content

Basics of Clustering Using k-Means

Find the groups, choose how many, and know when to trust them

This book is 100% completeLast updated on 2026-08-13

A machine can hand you a grouping that is confident, stable, internally consistent, and seven times worse than the one it found a moment earlier. Nothing in the output says so.

This book works k-means through completely on twelve bakery customers, small enough that every number is printed and checkable. You will build the method from nothing, see exactly where it succeeds, and see exactly where it fails silently.

Fifty-eight figures, every one captured from a running implementation.

Free With Membership

With Membership

Free!

$4.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

Twelve customers. Two columns. Every number printed on the page.

A bakery owner wants to treat different kinds of regular differently. She has measurements for all twelve of her regulars and no categories, because nobody has ever sorted them. That is the whole setup, and it is enough to build clustering from nothing.

This book works k-means through completely on data small enough that you can check every calculation yourself. Not a survey. Not a tour of the literature. One method, one dataset, followed until it works and then pushed until it breaks.

What you will actually see:

  • Why no straight line separates these twelve customers correctly, established by testing roughly six million of them rather than asserting it
  • What "alike" means when a machine has to compute it, and where a group is located when it has four members and no address
  • k-means run from a deliberately poor start, one step at a time, until nothing moves
  • Two quality scores disagreeing about the same grouping, one calling it an improvement and the other a deterioration, and why the disagreement is the useful part
  • The run that converges to an answer seven times worse than the best available, reports success, and gives you no warning
  • How recording spend in cents instead of dollars hands 100 percent of the decision to one column and silently changes every group

    By the end you will know how to run k-means, how to choose k, and, more usefully, how to tell when the answer it hands you should not be trusted.

No statistics background assumed. Arithmetic and the rule about right triangles are the only mathematics used.

Author

About the Author

Ritesh Modi

Ritesh Modi is Head of AI at MarketOnce and a former Forward Deployed Engineer at Microsoft. He has spent more than a decade building and shipping production systems across cloud, distributed computing, and applied machine learning, working with organizations ranging from global enterprises to fast-moving startups. His recent work focuses on applied large language models, designing systems that turn pretrained models into reliable, task-specific tools.

Ritesh has authored multiple technology books and speaks regularly at industry conferences on AI, cloud architecture, and software engineering. His writing philosophy rests on a simple belief: the best technical books are written by practitioners who still remember what it felt like to not understand something, not by experts who have forgotten. Every explanation in this book was tested against that standard, if it would not have made sense to him when he was first learning this material, it was rewritten until it did.

He writes, shares ideas, and connects with readers at www.riteshmodi.com. When he is not writing or building AI systems, he can be found mentoring engineers, exploring new architectures, or debugging a training run that should have converged three hours ago.

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub