Unsupervised LearningCentroid-based ClusteringClustering

K-Means / K-Means++

Primary task · Clustering

K-Means is a centroid-based unsupervised clustering algorithm that partitions observations into a user-specified number of compact groups. K-Means++ improves initialization so the starting centroids are spread out and usually converge to better solutions.

← Directory
Visual intuition

From data to learned behaviour

Clustering searches for structure without target labels. The algorithm defines what “similar” means—distance, density, probability or connectivity—and groups observations that satisfy that structural rule.

Infographic
1Unlabelled data2Similarity3Group update4Converge5ClustersTraining transforms evidence into a reusable model state
Conceptual simulation

Watch the learning mechanism form

The structure below is synchronized with the same training state used by the prediction simulation.

Mechanism view
Training control centre

Control both simulations together

Reset regenerates the synthetic data and model state. Train animates to completion. Pause freezes the animation. Train Step advances one learning stage.

Step 0 / 8
Model simulation

Inspect the learned prediction / representation

Synthetic data are generated locally in your browser.

Model description

Understand K-Means / K-Means++ after watching it learn

This section connects the animation to the actual statistical or computational idea behind the model.

Deep description

K-Means / K-Means++ K-Means is a centroid-based unsupervised clustering algorithm that partitions observations into a user-specified number of compact groups. K-Means++ improves initialization so the starting centroids are spread out and usually converge to better solutions.

What is learned. During training, the algorithm builds or adjusts the parameters and internal representation used by K-Means / K-Means++. The core learning mechanism is: Iteratively alternates between assigning points to the nearest centroid and updating centroids to the mean of assigned points (K-Means++ optimizes initialization).

How training becomes inference. Represent observations → measure similarity/density → form or update candidate groups → iterate assignments/structure → stop at convergence → inspect cluster quality and stability. Once training stops, the fitted state is reused on unseen inputs rather than being reconstructed from scratch. The resulting output is: Cluster assignments, cluster memberships, densities or fitted mixture responsibilities.

Why practitioners use it. Fast, scalable O(n*k*i), simple to implement and understand. Typical fits include Customer segmentation, image color quantization, document topic grouping.

What to verify before trusting it. Requires pre-specifying k; assumes spherical, equally sized clusters; highly sensitive to outliers. The visual simulation is intentionally simplified, so real use should still validate preprocessing, data independence, hyperparameters, uncertainty and task-appropriate metrics.

Internal statethe parameters and internal representation used by K-Means / K-Means++
Typical outputCluster assignments, cluster memberships, densities or fitted mixture responsibilities.
Good fitCustomer segmentation, image color quantization, document topic grouping.
Main cautionRequires pre-specifying k; assumes spherical, equally sized clusters; highly sensitive to outliers.
1Training data→
2Learning objective→
3Internal model state→
4Prediction / representation→
5Evaluation
Intuition

What the model is trying to learn

Clustering searches for structure without target labels. The algorithm defines what “similar” means—distance, density, probability or connectivity—and groups observations that satisfy that structural rule.

Mathematical lens

Core logic

The optimisation objective depends on the family: within-cluster distortion for centroid methods, density connectivity for DBSCAN-like methods, or likelihood for mixture models. Scaling and distance geometry can strongly change the discovered groups.

Training sequence

How learning progresses

Represent observations → measure similarity/density → form or update candidate groups → iterate assignments/structure → stop at convergence → inspect cluster quality and stability.

Original mechanism

Taxonomy description

Iteratively alternates between assigning points to the nearest centroid and updating centroids to the mean of assigned points (K-Means++ optimizes initialization).

Evaluation guide

How to evaluate this model responsibly

ValidationChoose validation that matches the independence assumptions of the data.
MetricsUse task-specific primary and complementary metrics.
HPOEstablish a baseline first, then search the parameters that materially change capacity.
Post-processingValidate any downstream transformation on held-out data.
Hyperparameters

Key parameters

n_clustersTypical: 3

Number of centroids/clusters.

initTypical: k-means++

Centroid initialization strategy.

n_initTypical: auto / 10

Number of independent restarts.

max_iterTypical: 300

Maximum Lloyd iterations.

tolTypical: 1e-4

Centroid movement tolerance for convergence.

Use & trade-offs

Where it fits

Typical applications

Customer segmentation, image color quantization, document topic grouping.

Strengths

Fast, scalable O(n*k*i), simple to implement and understand.

Limitations

Requires pre-specifying k; assumes spherical, equally sized clusters; highly sensitive to outliers.

Code example

Minimal Python implementation

# Purpose: demonstrate K-Means / K-Means++ with a small, inspectable example.
# Follow the comments and printed stages to connect each operation with its result.
# Import the library or helper used in this example.
import numpy as np
# Import the library or helper used in this example.
from sklearn.cluster import KMeans
# Import the library or helper used in this example.
from sklearn.datasets import make_blobs

# Print this intermediate result so you can verify the workflow step by step.
print("STEP 1 · Prepare the miniature example")
# Store this intermediate value with a descriptive name for the next step.
X, _ = make_blobs(
    n_samples=12, centers=3,
    cluster_std=0.75, random_state=42
)

# Configure the estimator or pipeline with the chosen settings.
model = KMeans(n_clusters=3, init="k-means++", n_init=10, random_state=42)
# Print this intermediate result so you can verify the workflow step by step.
print("STEP 2 · Fit / train the model")
# Store this intermediate value with a descriptive name for the next step.
labels = model.fit_predict(X)

# Print this intermediate result so you can verify the workflow step by step.
print("STEP 3 · Inspect predictions / metrics")
# Print this intermediate result so you can verify the workflow step by step.
print(np.round(model.cluster_centers_, 2))
# Print this intermediate result so you can verify the workflow step by step.
print(np.bincount(labels))
Expected / representative output
STEP 1 · Prepare the miniature example
STEP 2 · Fit / train the model
STEP 3 · Inspect predictions / metrics
[[-2.34  8.81]
 [-7.30 -7.11]
 [ 4.23  1.62]]
[4 4 4]