Module 5 of 6 · 30 minutes

Unsupervised learning

Not every problem has labels. Unsupervised learning finds groups and oddities in your data — and turns them into action.

Learning objectives

  • Explain how k-means clustering works
  • Scale features before distance-based methods
  • Choose k with silhouette scores, the elbow method and business sense
  • Use PCA to visualise high-dimensional data
  • Flag anomalies for investigation

5.1 Finding structure without labels

Unsupervised learning finds patterns in data without a target. The most common business use is segmentation: grouping customers, products or documents that behave similarly, so you can tailor actions to each group.

tenure (scaled) →monthly spend (scaled)+new, low spend+loyal, high spend+frustrated: many tickets
Clustering groups similar customers without any labels. The + marks each cluster's centre; naming the segments is a human job.

5.2 k-means clustering

k-means places k centre points, assigns each example to its nearest centre, moves each centre to the average of its members, and repeats until stable. Because it uses distances, features must be scaled — otherwise a feature measured in hundreds swamps one measured in single digits.

Python · customer segmentationfrom sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

seg_features = ["tenure_months", "monthly_spend", "support_tickets"]
Xs = StandardScaler().fit_transform(customers[seg_features])

km = KMeans(n_clusters=4, n_init=10, random_state=0).fit(Xs)
customers["segment"] = km.labels_
profile = customers.groupby("segment")[seg_features + ["churned"]].mean().round(1)
profile["size"] = customers["segment"].value_counts().sort_index()
print(profile)

Now name the segments from their profiles — "new, low-spend", "loyal high-spenders", "frustrated (many tickets)" — and check the names with people who know the customers.

Place kcentresAssign each pointto nearest centreMove centres tomembers' averageStable?Stoprepeat until centres stop moving
The k-means loop. Because it measures distances, scale features first.

5.3 How many clusters?

from sklearn.metrics import silhouette_score

for k in range(2, 8):
    labels = KMeans(n_clusters=k, n_init=10, random_state=0).fit_predict(Xs)
    print(k, round(silhouette_score(Xs, labels), 3))

The silhouette score (−1 to 1) measures how well each point fits its own cluster compared with the nearest other cluster. Combine it with the elbow method (where adding clusters stops reducing within-cluster distance much) — and above all with usefulness: can the business act differently on each segment?

5.4 Dimensionality reduction with PCA

Principal component analysis (PCA) compresses many features into a few new ones that keep as much variation as possible. It's useful for visualising high-dimensional data in 2D and for removing redundancy.

from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

pca = PCA(n_components=2).fit(Xs)
xy = pca.transform(Xs)
print("Variance explained:", pca.explained_variance_ratio_.round(2))
plt.scatter(xy[:, 0], xy[:, 1], c=customers["segment"], s=6, cmap="tab10")
plt.title("Customer segments (PCA projection)")
plt.show()

5.5 Anomaly detection

Unsupervised methods can also flag unusual examples — potential fraud, faults or data errors. scikit-learn's IsolationForest is a common starting point. As always, flagged items are leads for human investigation, not verdicts.

from sklearn.ensemble import IsolationForest

iso = IsolationForest(contamination=0.01, random_state=0).fit(Xs)
customers["unusual"] = iso.predict(Xs) == -1
print(customers.loc[customers["unusual"], seg_features].head())

Key takeaways

  • k-means assigns points to the nearest of k centres and updates centres iteratively.
  • Scale features before clustering.
  • Choose k using metrics and usefulness.
  • PCA compresses features while keeping variation.
  • Anomalies are leads for investigation, not verdicts.