K-Means and K-Medoids Clustering with Python

MetaCyberGuru Academy

IntermediateEstimated learning effort: 85 minutesFree, no sign-up requiredPublished by Muhammad AzharCourse version: August 2026

Back to Clustering and Dimensionality Reduction

K-means always returns groups when asked. That does not mean the groups exist in a useful sense. You need to understand its geometry, rerun stability and whether the centroids describe anything a domain user recognizes.

Know what the objective is optimizing

You will fit k-means at several values of k, inspect inertia and silhouette, then compare the role of a centroid with a medoid.

  • Explain assignment and update steps in Lloyd’s k-means algorithm.
  • Recognise the spherical, variance and Euclidean assumptions behind the objective.
  • Describe PAM and CLARA as medoid-based alternatives.
  • Evaluate initialization, scaling and run-to-run stability.

A centroid is a useful fiction

K-means alternates between assigning each point to its nearest centroid and updating each centroid to the mean of its assigned points. It minimizes within-cluster squared Euclidean distance. The mean can lie where no actual record exists, and one extreme point can move it substantially.

K-medoids represents a cluster with an observed record. PAM swaps candidate medoids to reduce total dissimilarity. It supports broader distance measures and is more robust to extremes, but it is more expensive. CLARA draws samples, applies PAM and evaluates representative candidates for larger data.

Scaling defines the geometry. Standardize fields only when equal standard-deviation movement is meaningful. A binary flag and a continuous spending field may need domain weighting rather than generic scaling.

K-means needs k in advance and favours compact groups of roughly comparable variance. Elongated, nested or variable-density patterns can be partitioned misleadingly. The elbow in inertia is often subjective. Silhouette adds evidence but shares geometric assumptions.

Run multiple initializations and seeds. If cluster assignments change sharply, the segmentation is unstable. Align labels before comparison because cluster number 0 has no fixed meaning.

Fit several values of k and inspect the records

The synthetic points form three compact groups. The example prints both inertia and silhouette so you can see why using only a decreasing objective is insufficient.

Install the lesson dependencies

python -m pip install numpy scikit-learn

Compare candidate cluster counts

import numpy as np
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler

X = np.array([
    [1.0, 1.1], [1.2, 0.9], [0.8, 1.0],
    [5.0, 5.1], [5.2, 4.8], [4.8, 5.0],
    [9.0, 1.1], [9.2, 0.9], [8.8, 1.0],
])
X_ready = StandardScaler().fit_transform(X)

for k in [2, 3, 4]:
    model = KMeans(n_clusters=k, n_init=20, random_state=42)
    labels = model.fit_predict(X_ready)
    print(k,
          "inertia=", round(model.inertia_, 3),
          "silhouette=", round(silhouette_score(X_ready, labels), 3),
          "sizes=", np.bincount(labels).tolist())

Expected pattern

2 inertia= <value> silhouette= <value> sizes= <two counts>
3 inertia= <lower value> silhouette= <highest or strong value> sizes= [3, 3, 3] in label-dependent order
4 inertia= <still lower value> silhouette= <typically weaker value> sizes= <four counts>

Inertia always decreases as k grows, eventually reaching zero when every point is its own cluster. It cannot select k alone.

K-means debugging questions

Inspect cluster sizes, centroids in original units and representative records before naming any segment.

  • One huge cluster and several tiny clusters may signal skew, outliers or the wrong geometry.
  • A cluster defined by one high-scale feature suggests preprocessing dominates the result.
  • Different seeds producing different solutions indicates weak structure or local minima.
  • Empty or nearly empty clusters at high k show the requested partition is too fine for the data.

Test stability, not just one solution

Fit k-means under at least ten seeds and compare cluster co-assignment: how often each pair of records stays together.

  • Return centroids to original units.
  • Find the closest observed record to each centroid as a possible representative.
  • Compare with a medoid implementation on a sample.
  • Ask whether the segmentation supports a real action without stereotyping people.

Clustering evidence

  • Candidate-k table.
  • Seed-stability matrix.
  • Segment descriptions based on measured distributions, not invented personas.

Knowledge check

1. What does k-means minimize?
Check your reasoning

The standard objective is within-cluster sum of squared Euclidean distances.

2. What is a medoid?
Check your reasoning

Unlike a centroid, the medoid is one of the actual observations.

3. Why cannot inertia alone choose k?
Check your reasoning

Additional clusters can only maintain or reduce within-cluster squared distance.

Official references and further reading

Review note for K-Means and K-Medoids Clustering with Python: recheck the linked documentation after a dependency changes the relevant API, metric or modelling assumption, then record the tested version beside your result.

Save your place

Completion is stored only in this browser on this device.

Share this page

Share this page with the people who will use it next.

X Facebook LinkedIn WhatsApp Email

Discussion

No comments yet. Add the first useful question or observation.