Post

AI - k-Means Clustering

K-Means Clustering is an unsupervised machine learning algorithm used to group data into *K distinct clusters* based on feature similarity. It’s one of the most popular clustering techniques because it’s conceptually simple and computationally efficient.

The algorithm aims to minimize the intra-cluster variance (distance between data points and their cluster’s centroid) while maximizing the inter-cluster variance (distance between different cluster centroids).


How It Works

  1. Choose the number of clusters ($K$).
  2. Initialize $K$ centroids randomly within the data space.
  3. Assign each data point to the nearest centroid.
  4. Recalculate centroids as the mean of all points assigned to each cluster.
  5. Repeat steps 3–4 until the centroids no longer change significantly (convergence).

This iterative process is often called the Expectation–Maximization approach:

  • Expectation step: Assign points to the nearest centroid.
  • Maximization step: Recalculate the centroid positions.

Mathematical Objective

K-Means minimizes the following objective function:

\[J = \sum_{i=1}^{K} \sum_{x_j \in C_i} ||x_j - \mu_i||^2\]

Where:

  • $K$ = number of clusters
  • $C_i$ = set of points in cluster $i$
  • $\mu_i$ = mean (centroid) of cluster $i$

Choosing the Right K

A common way to find an appropriate number of clusters is the Elbow Method:

  • Run K-Means with different values of $K$.
  • Plot the total within-cluster sum of squares (WCSS) vs. $K$.
  • The “elbow” point (where improvement slows) suggests a good choice of $K$.

K-Means Clustering Demonstration

This post is licensed under CC BY 4.0 by the author.