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
