AI Glossary / Statistics and probability / Advanced

k-means

Definition

K-means is a clustering method that groups numerical observations around k centers, minimizing the sum of squared Euclidean distances between observations and their assigned centers.

What it means

The analyst chooses the number k. Each cluster center is the mean of its assigned observations and need not be an observed point. The resulting labels describe groups in the chosen feature space; they do not supply meanings or verified categories by themselves.

How it works

A common algorithm initializes centers, assigns each observation to its nearest center, recalculates the means, and repeats. Different initializations can produce different local solutions. Multiple starts and k-means++ initialization can help. Feature scaling matters, and squared distances make extreme observations influential. Compact groups are usually a better fit than curved or elongated structures.

Three practical examples

  • A fictional support team groups requests represented by two numerical workload measures, then reads examples before naming the groups.
  • A fictional image exercise replaces pixel colors with their assigned cluster-center colors, comparing detail lost at different k values.
  • A fictional operations team clusters daily activity profiles and checks whether the groups remain useful during a later month.

Think of it this way

Imagine placing meeting points along a road and assigning each household to its closest point. Moving each point to the average position of its assigned households reduces squared travel distances for that assignment. This analogy describes the calculation, not a guarantee that the locations satisfy real travel needs.

Worked example

Take six original one-dimensional observations: 1, 2, 3, 8, 9, and 10. Set k to two and start with centers at 1 and 3. These are invented measurements in a common unit.
Assign a point exactly halfway between centers to the lower-numbered center. The first group therefore contains 1 and 2; the second contains 3, 8, 9, and 10. Their updated means are 1.5 and 7.5.
The new midpoint is 4.5. Reassignment now puts 1, 2, and 3 in the first group and 8, 9, and 10 in the second. Their means become 2 and 9. The next assignment is unchanged, so this run has stabilized.
For the first final group, squared deviations from 2 are 1, 0, and 1. For the second, squared deviations from 9 are also 1, 0, and 1. Total within-cluster squared error is therefore four.
With only one center, the mean is 5.5. Squared deviations are 20.25, 12.25, 6.25, 6.25, 12.25, and 20.25, totaling 77.5. Moving from one to two centers reduces this example's objective by 73.5, but that improvement alone does not establish two meaningful real-world categories.
A three-group partition of {1, 2}, {3}, and {8, 9, 10} has centers 1.5, 3, and 9 and total squared error 0.5 plus zero plus two = 2.5. Six singleton groups achieve zero. Consequently, selecting the number of groups only by seeking the smallest raw training error would favor excessive splitting in this example.
Consider a separate invented rectangle with observations A=(0, 0), B=(0, 2), C=(10, 0), and D=(10, 2), again with k equal to two.
Grouping A with B and C with D gives centers (0, 1) and (10, 1). Each point contributes one, so total squared error is four.
Grouping A with C and B with D instead gives centers (5, 0) and (5, 2). Each point contributes 25, making total error 100. Nevertheless, this second arrangement is stable under ordinary nearest-center reassignment: every point is closer to its existing horizontal-group center than to the other one. It demonstrates a stable solution that is worse than another available partition.
Now multiply the rectangle's second coordinate by ten. The points become (0, 0), (0, 20), (10, 0), and (10, 20). The vertical-pair partition now has error 400, while the horizontal-pair partition has error 100. Changing relative feature scale reverses which of these two partitions fits better. Whether such rescaling is appropriate depends on what the coordinates represent.
Finally, a new one-dimensional observation of 5.5 is equally distant from the fitted centers 2 and 9. A tie-breaking rule can assign it consistently, but the resulting label does not express certainty. These calculations are original illustrations of the objective, initialization, scaling, and interpretation.

Illustrative case study

Fictional company Cedar Operations uses the six-value exercise to teach analysts about clustering before applying the method to workload records. Its team records the chosen features, transformations, k, and initialization settings.
Reviewers compare several runs and examine representative records from each group. They retain descriptive labels only when the records support those descriptions. The fictional project treats a low squared-error score as one piece of evidence about a grouping, alongside stability and operational usefulness.

Common misconceptions and mistakes

A cluster number is not an importance ranking.
The example with error 100 shows why a stable run need not be the best available solution.
The rescaled rectangle shows why measurement choices affect the result.
The singleton example shows why lower training error alone cannot settle k.

Business use

For a proposed segmentation, document the intended decision before interpreting group labels. In the fictional workload setting, a useful review would ask whether different groups actually require different staffing arrangements and whether new records fit the same descriptions.

People also ask

Must centers be actual observations?

No.

Does k-means choose k automatically?

The basic method requires it as an input.

Can two runs disagree?

Yes, including because of their starting centers.

Check your understanding

What is the final two-group error in the six-value example?

Answer: Four.

Why is the rectangle's error-100 solution noteworthy?

Answer: It is stable despite a better partition being available.

What changed the preferred rectangle grouping?

Answer: Rescaling the second coordinate.

Related terms

  • Clustering
  • Centroid
  • Euclidean distance
  • Mini Batch K-Means
  • inertia
  • k-means++
  • feature scaling
  • silhouette score

Learn next

Reproduce the rectangle assignments, then compare clustering stability across initializations and feature transformations.

  • Clustering

More in Statistics and probability

  • Joint probability distribution - A joint probability distribution describes how two or more random variables behave together.
  • Jonckheere's trend test - Jonckheere's trend test, also called the Jonckheere-Terpstra test, is a nonparametric test for an ordered alternative across independent groups.
  • Judgment sample - A judgment sample is a non-probability sample whose members are deliberately selected using a researcher's or expert's assessment of their relevance or...
  • k-median - K-median is a clustering approach that seeks k representative centers while minimizing the total distance from observations to their assigned centers.
  • k-nearest neighbors regression - K-nearest neighbors regression predicts a numerical target by combining the target values of the k training observations closest to a new input.
  • Kaiser - Meyer - Olkin test - The Kaiser - Meyer - Olkin measure, commonly called the KMO test, is a diagnostic index used when assessing whether a correlation matrix is suitable for...

Sources

Scikit-learn, Clustering, K-means: https://scikit-learn.org/stable/modules/clustering.html#k-means . Numerical datasets, comparisons, and company scenario are original.

Previous: K-line (artificial intelligence) Next: k-median

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer