Clustering · Beginner

K-Means

Assign points, move centroids, and inspect inertia through real iterations.

K-Means alternates between assigning samples to the nearest centroid and moving each centroid to the mean of its assigned cluster. ModelOrigen records the real assignment and update phases.

Step by step

  1. Choose K and initialize K centroids.
  2. Measure each point-to-centroid distance.
  3. Assign every point to its closest centroid.
  4. Recompute each centroid as the mean of its assigned points.
  5. Repeat until assignments stabilize or the iteration limit is reached.

Core formulas

Assignment

cᵢ = argminₖ ||xᵢ − μₖ||²

Each sample chooses the nearest current centroid.

Centroid update

μₖ = (1/|Cₖ|) Σ xᵢ

A centroid moves to the mean of its assigned samples.

Inertia

Σ ||xᵢ − μcᵢ||²

Within-cluster squared distance; it is not classification accuracy.

When to use K-Means

  • You want compact groups without labels.
  • Clusters are reasonably compact around centers.
  • A fast exploratory clustering baseline is useful.

Related interactive lessons

Primary references

scikit-learn KMeans