# k-means clustering

> Canonical: https://skillalmanac.com/glossary/k-means | Last verified: 2026-11-25 | Index status: index-eligible

k-means is an unsupervised algorithm that splits unlabelled data into k groups by repeatedly assigning each point to its nearest group centre, called a centroid, and moving each centre to the mean of its points.

Also called: k-means, k means clustering, kmeans, k-means algorithm.

## In plain language

k-means sorts a pile of unlabelled items into a number of groups that you choose in advance. Think of dropping k pins on a map of shop locations, assigning each shop to its nearest pin, then moving each pin to the middle of its shops and repeating until the pins stop moving. It is fast and simple, which is why it is often a first try at finding groups, such as types of customer. Its limits are that you must pick k yourself, and different starting positions can give different groups.

## Technical definition

k-means aims to choose k centroids that minimise inertia, the within-cluster sum of squares. Lloyd's algorithm alternates between assigning each sample to its nearest centroid and recomputing every centroid as the mean of its assigned samples, stopping when centroid movement is below a tolerance. It converges, but possibly to an initialization-dependent local minimum rather than the globally smallest inertia, so multiple initializations are commonly tried. k-means++ generally places initial centroids far apart. Inertia favours convex, isotropic clusters and performs poorly on elongated or irregular shapes.

## How it shows up when you use agents

k-means is a classic [unsupervised learning](/glossary/unsupervised-learning) method: it finds groups without labels. An agent may reach for it when you ask Claude Code, Codex or Cursor to segment customers, group support tickets by their [embeddings](/glossary/embedding) or look for possible groupings in a table. It works on numbers, so text must first be turned into vectors.

The examples cover data inspection and evaluation rather than training a clustering model. What breaks when it is misunderstood is the belief that the groups are real. Requesting a partition with a chosen k does not establish that k meaningful real-world categories exist. Different starting points can give different results, and the scikit-learn guide notes that Euclidean distances can become inflated in very high-dimensional spaces, where [dimensionality reduction](/glossary/dimensionality-reduction) such as [principal component analysis](/glossary/principal-component-analysis) before clustering can be tried. Ask the agent how k was chosen, whether features were scaled and whether the result was stable across runs.

## Common confusions

- **k-means versus k-nearest neighbors.** k-means is unsupervised and groups unlabelled data. [k-nearest neighbors](/glossary/k-nearest-neighbors) is supervised and uses labelled examples. The k is a different number in each.
- **Clusters versus classes.** A cluster is a group the algorithm found. A class is a label a person defined. A cluster need not match a meaningful category.
- **Centroid versus real data point.** A centroid is the mean of a group's points and need not be an actual item in the data.
- **Right k versus chosen k.** The elbow method plots within-cluster sum of squares against k and looks for where improvement levels off. IBM says it produces a range for a candidate optimum and warns that it is not necessarily the best method, especially for high-dimensional or irregular data. It is a selection heuristic, not proof of a globally correct k.

## Worked examples in this catalogue

### [data-analysis-eda](https://skillalmanac.com/skills/skill-almanac/data-analysis-eda)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: A fixed exploratory-analysis procedure and a CSV profiler, with findings kept descriptive, evidenced and caveated.

Why it exercises the concept: Profiling a table first shows the column types, gaps and spread that decide whether this method is a sensible choice for the data.

### [mock-record-generator](https://skillalmanac.com/skills/skill-almanac/mock-record-generator)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Seeded mock records written as CSV or JSON from a JSON field list or a simple TypeScript interface.

Why it exercises the concept: Seeded mock rows give you a small, repeatable table to try a scikit-learn example on without touching real data.

### [huggingface-datasets](https://skillalmanac.com/skills/huggingface/huggingface-datasets)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Metadata, rows, search and filters for a Hugging Face dataset, read through the Dataset Viewer API.

Why it exercises the concept: This read-only skill can inspect public dataset rows before numeric features are prepared for clustering; the rows need not be labelled, and the skill does not run k-means.

## Related terms

- [Unsupervised learning](https://skillalmanac.com/glossary/unsupervised-learning.md)
- [Machine learning (ML)](https://skillalmanac.com/glossary/machine-learning.md)
- [Embedding](https://skillalmanac.com/glossary/embedding.md)
- [Dimensionality reduction](https://skillalmanac.com/glossary/dimensionality-reduction.md)
- [Principal component analysis (PCA)](https://skillalmanac.com/glossary/principal-component-analysis.md)

## Sources

- [2.3. Clustering](https://scikit-learn.org/stable/modules/clustering.html), scikit-learn (accessed 2026-11-25)
- [What is k-means clustering?](https://www.ibm.com/think/topics/k-means-clustering), IBM (accessed 2026-11-25)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
