AI GLOSSARY · AI CONCEPTS

k-means clustering

Markdown version · Wikidata Q310401

Definition

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.

Worked examples in this catalogue

Catalogue examples point at a skill, guide or coding tool listed on this site, with what to run and what you see. An example marked illustrative describes the item from its own documentation; it was not executed for this page. Platform Scores are our assessment under a published method, not a safety guarantee.

  1. Skill by Skill Almanac · Illustrative, not executed

    data-analysis-eda 56/100 · Provisional, not yet measuredScored · Method v0.2 · How we score

    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.
  2. Skill by Skill Almanac · Illustrative, not executed

    mock-record-generator 42/100 · Provisional, not yet measuredScored · Method v0.2 · How we score

    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.
  3. Skill by Hugging Face · Illustrative, not executed

    huggingface-datasets 25/100 · Provisional, not yet measuredScored · Method v0.2 · How we score

    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.

How it shows up when you use agents

k-means is a classic 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 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 such as 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 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.

Terms that mention k-means clustering

2 terms mention this one

More in AI concepts

Next steps

  1. data-analysis-eda A skill that exercises it.
  2. Embedding Related term.
  3. Unsupervised learning Related term.
  4. All glossary terms A to Z and by family.

Sources

Definitions are written by Skill Almanac from these sources and were checked against them on 2026-11-25. Wikipedia is never the only source.

Review record

Last verified

Drafted by claude-sonnet-5-5; sources and example targets checked by Independent automated source review (OpenAI Codex); not human peer review on 2026-11-25. Last verified .

Change log (5)
  • : Independent automated factual/example and synonym review completed; cached US query estimate aligned to receipt. Final technical gate separate; publication disabled.
  • : First version.
  • : Verifier pass: applied wording corrections from the verification list.
  • : OpenAI5.6/Codex revision: Clarifies local convergence, repairs the dataset example and accurately limits the elbow heuristic. Independent re-review pending.
  • : Automated review completed; approved for the staged public release. Search indexing is controlled by the release configuration.