devnotes.

K-Means Clustering

K-means groups unlabeled data points into k clusters based purely on similarity — no target variable needed, since there's nothing to predict, only structure to discover.

from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

X_scaled = StandardScaler().fit_transform(X)   # scaling matters a lot for distance-based clustering

kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
clusters = kmeans.fit_predict(X_scaled)          # assigns each point a cluster label: 0, 1, or 2

df["cluster"] = clusters
print(df.groupby("cluster").mean())               # characterize each cluster by its average feature values

# Finding a good value for k with the "elbow method":
inertias = []
for k in range(1, 10):
    km = KMeans(n_clusters=k, random_state=42, n_init=10).fit(X_scaled)
    inertias.append(km.inertia_)   # sum of squared distances to each point's assigned cluster center

plt.plot(range(1, 10), inertias, marker="o")
plt.xlabel("k"); plt.ylabel("Inertia")

K-means works by iteratively placing k cluster centers, assigning each data point to its nearest center, then recomputing each center as the average of its assigned points — repeating until the assignments stop changing. Because it relies entirely on distance between points, scaling features first is essential (covered in the preprocessing chapter) — without it, a feature on a much larger numeric scale would dominate the distance calculation regardless of its actual relevance.

Choosing k (the number of clusters) requires judgment, since there's no single "correct" answer the algorithm can determine on its own — the elbow method plots inertia (how tightly points cluster around their center) against different values of k, looking for the point where adding more clusters stops meaningfully reducing inertia, forming a visual "elbow" in the curve. After clustering, examining each cluster's average feature values (as with df.groupby("cluster").mean()) is how the resulting groups are actually interpreted and given meaning — the algorithm finds the structure, but a human still has to determine what that structure represents in the real-world context.

K-Means Clustering in scikit-learn | DevNotes