liveBook · Manning

Inertia

As explained in The Well-Grounded Data Analyst

Inertia is a clustering metric that quantifies within-cluster variation, typically as the sum of squared distances from observations to their assigned cluster centers.

Inertia quantifies how closely observations lie to their assigned cluster centers. Lower values indicate tighter clusters, but adding clusters always lowers inertia, so the raw score is not sufficient for judging quality. section 13.1.4

Use inertia to support a practical choice of cluster count:

  • Run k-means across several values of k rather than selecting one cluster count immediately. section 13.1.4
  • Plot each inertia value against its number of clusters and look for an elbow, where additional clusters produce only small reductions. section 13.1.4
  • Check whether the resulting groups make sense for the use case, because subjective domain judgment matters more than optimizing inertia alone. section 13.1.4

Choosing seven clusters

A k-means analysis tested k values from 3 through 14, plotted the associated inertia scores, and found that increasing k from seven to eight barely improved the score. Seven clusters became the starting choice, after which assignments and cluster centers were inspected. section 13.1.4

from sklearn.cluster import KMeans

k_values = range(3, 15)
inertia_values = []

for k in k_values:
    kmeans = KMeans(n_clusters=k, random_state=42)
    kmeans.fit(X_scaled)
    inertia_values.append(kmeans.inertia_)

fig, axis = plt.subplots()

axis.plot(k_values, inertia_values)

_ = axis.set(
    title="'Inertia' values for different values of k",
    xlabel="Number of clusters (the 'k' in k-means)",
    ylabel="Inertia"
)

axis.set_ybound(0, 900)

plt.show()

Inspect the executable k-means loop and plot used to compare inertia across candidate cluster counts. section 13.1.4

How The Well-Grounded Data Analyst covers it

How deep it goes
Defines tightness around cluster centers and demonstrates elbow selection, but defers advanced metrics such as silhouette scores.
Examples
Uses a k-means loop over k values 3–14, an inertia plot, and a seven-cluster mobile-user segmentation.
What it assumes
Assumes clustering and preprocessing basics, including transforming algorithm inputs onto a common scale.
What you can do after
Apply it via scikit-learn to compare candidate cluster counts and inform an initial k-means choice.
How it connects
Connects k-means, clustering, the elbow method, standardization, and silhouette scores.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Data Analyst. The section links point to the parts of the book it draws on.