{
  "aliases": null,
  "backlinks": null,
  "body": "\n## Definition\n\nLet $x_1, \\ldots, x_n \\in \\R^d$ and let $1 \\le k \\le n$.\nFor a [[partition|08QMKTWF9T]] of $\\set{1, \\ldots, n}$ into non-empty sets $S_1, \\ldots, S_k$, let $m_j = \\frac{1}{\\abs{S_j}} \\sum_{i \\in S_j} x_i$ be the mean of cluster $j$.\nThe *$k$-means problem* is to find a partition that minimises the within-cluster sum of squares\n$$W(S_1, \\ldots, S_k) = \\sum_{j=1}^k \\sum_{i \\in S_j} \\abs{x_i - m_j}^2,$$\nwhere $\\abs{\\cdot}$ is the [[Euclidean norm|0NPPGHQNXQ]].\n*$k$-means clustering* is the [[clustering|D9Y3A9RH6J]] given by a solution, or by an approximate solution, of this problem.\n\n## Remarks\n\nLloyd's algorithm starts from $k$ centres and repeats two steps: assign each $x_i$ to a nearest centre, then replace each centre by the mean of its assigned points.\nNeither step increases $W$, and there are finitely many partitions, so the algorithm stops after finitely many steps, at a local minimum that need not be global.\nExample: for $0, 1, 10, 11 \\in \\R$ and $k = 2$, the partition $\\set{0, 1}, \\set{10, 11}$ has means $0.5$ and $10.5$ and $W = 4 \\cdot 0.25 = 1$.\nNon-example: the partition $\\set{0}, \\set{1, 10, 11}$ has means $0$ and $22/3$ and $W = 546/9 \\approx 60.7$, so it is not a solution.\nThe mean is used because, for fixed clusters, $m_j$ is the point $c$ that minimises $\\sum_{i \\in S_j} \\abs{x_i - c}^2$.\n",
  "commit": "ff8a40749dc16545e3d2fc637f6b0196e81d0bfa",
  "front": {
    "defines": "D5PN3G41D6",
    "kind": "definition",
    "msc": [
      "62H30"
    ],
    "name": "k-means clustering",
    "sources": [
      {
        "url": "https://en.wikipedia.org/wiki/K-means_clustering"
      }
    ],
    "tag": "PNF97STBAW",
    "uses": [
      "D9Y3A9RH6J",
      "08QMKTWF9T",
      "0NPPGHQNXQ"
    ]
  },
  "kind": "definition",
  "link": "https://tamnd.github.io/math2/node/PNF97STBAW/",
  "links": [
    {
      "field": "defines",
      "target": "D5PN3G41D6"
    },
    {
      "field": "uses",
      "target": "D9Y3A9RH6J"
    },
    {
      "field": "uses",
      "target": "08QMKTWF9T"
    },
    {
      "field": "uses",
      "target": "0NPPGHQNXQ"
    }
  ],
  "markdown": "https://tamnd.github.io/math2/node/PNF97STBAW.md",
  "msc": [
    "62H30"
  ],
  "name": "k-means clustering",
  "prov": null,
  "role": "",
  "source": "https://github.com/tamnd/math2/blob/main/nodes/PNF/PNF97STBAW.md",
  "status": null,
  "tag": "PNF97STBAW",
  "uri": "m2://PNF97STBAW",
  "url": "https://tamnd.github.io/math2/wiki/k-means-clustering-definition/",
  "writing": {
    "tag": "PNF97STBAW",
    "kind": "definition",
    "level": "W0",
    "next": [
      {
        "what": "an inventory row",
        "note": "the row of the inventory that the node accounts for",
        "have": 0,
        "want": 1
      },
      {
        "what": "a source with a locator",
        "have": 0,
        "want": 1
      }
    ]
  }
}
