Art of DS

Appearance

Palette
Theme
Unlock $20 one time
Sign in, or create an account

Sign in to Art of DSCreate your Art of DS account

We email you a link and a six-digit code. Either one signs you in; there is no password.Enter your email. We send a link and a six-digit code to confirm it — no password to remember. The account is free.

  • You come back to this page, right where you were.
  • Already have an account? The same email simply signs you in.

By continuing you accept the Terms and the Privacy policy.

Your account

Free accountLifetime access

difficulty 2/5

K-means

Definition

Pick k centres, assign every point to the nearest, move each centre to the mean of what it caught, repeat. It always converges and it can converge somewhere arbitrarily bad.

Formula
min⁡C1,…,Ck∑j=1k∑x∈Cj∥x−μj∥2,μj=1∣Cj∣∑x∈Cjx\min_{C_1,\dots,C_k} \sum_{j=1}^{k} \sum_{x \in C_j} \lVert x - \mu_j \rVert^2, \qquad \mu_j = \frac{1}{|C_j|}\sum_{x \in C_j} x

Intuition

You have points and no labels, and a suspicion they fall into groups. K-means turns that suspicion into an objective: choose k centres so that the total squared distance from every point to its nearest centre is as small as possible [2, §2.3.2].

Then it solves that objective badly, on purpose, because solving it properly is intractable. The algorithm alternates two steps that each obviously improve the score — put every point with its nearest centre, then move every centre to the middle of its points — and stops when neither changes anything.

That guarantees it stops. It guarantees nothing about where. The whole character of k-means follows from that gap: it is fast, it is easy, it always returns an answer, and the answer can be ten times worse than the best one without anything in the output saying so.

Formal definition

How it works

Where it's used

Why & when

Worked example

Pitfalls

Check yourself

3 questions

The questions on this page come with access. They are graded on the server, so the answers stay there until you have given yours.

Unlock all quizzes

Sources

  1. Blömer, Lammersen, Schmidt, Sohler. Theoretical Analysis of the k-Means Algorithm — A Survey (2016)paperhttps://ar5iv.labs.arxiv.org/html/1602.08254
  2. scikit-learn User Guide — Clusteringdocs · retrieved 2026-09-06https://scikit-learn.org/stable/modules/clustering.html
  3. scikit-learn — KMeansdocs · retrieved 2026-10-07https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html

Have a promo code?

A code that takes 100% off unlocks everything at once. A smaller one takes its share off the price in the payment window: enter the same code there.

Write to support

A question, a problem with access, a refund? Write to us and we answer by email.

What is it about?

Your address is used only to answer you.

Message sent

Thank you. We will reply to the address you gave, by email.

Or write to us directly

support@artofds.com

Message sent Thank you. We will reply to the address you gave, by email.

Check the email address: we need it to reply.

That is our own address. Enter yours, so the reply reaches you.

This address cannot receive email. Check it for a typo.

Write a little more: at least 10 characters.

Too many messages in a short time. Try again in a few minutes.

The message could not be sent. Please write to us directly: support@artofds.com