Machine LearningLesson 14 of 22

Clustering with k-Means

Clustering finds hidden groups without labels. You give the model customer features and ask for k groups. It places k centers and moves them until points are close to their center.

Clustering finds hidden groups without labels. You give the model customer features and ask for k groups. It places k centers and moves them until points are close to their center.

Choosing k is the hard part. The elbow method plots inertia vs k and looks for a bend. Silhouette scores help too. There is no single true k, only useful ones.

k-Means assumes spherical clusters and needs scaling. If your data has weird shapes, try DBSCAN.

Elbow and scaling

Scale first, then try several k and plot inertia. Pick the bend.

PYTHON
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
X_scaled = StandardScaler().fit_transform(X)
for k in [2,3,4,5,6]:
    km = KMeans(n_clusters=k, n_init=10, random_state=42).fit(X_scaled)
    print(k, km.inertia_)

No labels, no accuracy

Clustering has no y, so no accuracy. Use silhouette or inspect groups manually. Do not create fake labels from clusters then train a classifier and report 99% — it is circular.

PYTHON
from sklearn.metrics import silhouette_score
print(silhouette_score(X_scaled, km.labels_))

Syntax

PYTHON
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=3, n_init=10, random_state=42)
kmeans.fit(X_scaled)
print(kmeans.labels_[:10])

k-Means assumes circles

If your clusters are crescents or rings, k-Means will cut them wrong. Use DBSCAN for odd shapes. This Reddit r/MachineLearning post with 2k upvotes shows k-Means failing on moons dataset.

PYTHON
from sklearn.datasets import make_moons
from sklearn.cluster import DBSCAN
X,_ = make_moons(n_samples=300, noise=0.08)

Good to know

  • k-Means needs scaling and a chosen k.
  • Elbow and silhouette help pick k.
  • No labels means no accuracy — inspect results.

Keep building your ML models

Copy the code, run it locally with pip install scikit-learn pandas, and continue to the next lesson.

→ Next: Dimensionality Reduction with PCA