K-means groups observations into a chosen number of clusters by placing nearby points together, producing internally homogeneous groups that differ from one another. section 13.1.4
A practical clustering workflow includes:
- Scale continuous features so differences in measurement ranges do not dominate distances. section 13.1.4
- Try candidate values of k and inspect inertia, choosing the point where additional clusters provide diminishing benefit. section 13.1.4
- Assign each observation to a cluster, count cluster sizes, and inspect centers in the original feature units. section 13.1.4
Mobile-user segmentation
User features such as app counts, session counts, session length, usage timing, and social-media behavior produce segments including heavy users, casual users, night owls, and usage outliers. 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()
Run k-means across candidate cluster counts and plot inertia to support a practical choice of k. section 13.1.4






