Unsupervised Learning
Beginner 10 min read Unsupervised Learning

Unsupervised Learning Overview

Structure in unlabeled data

Without labels, learning means finding structure: clustering groups similar rows, dimensionality reduction compresses the feature space, and anomaly detection flags unusual rows.

unsupervised learningclusteringdimensionality reductionanomaly detectionunlabeled data
Prerequisites: ML Introduction
Before we start

What you will learn

  • Define the three unsupervised families: clustering, reduction, anomaly detection
  • Explain when unsupervised answers business questions (no ground truth)
  • Judge unsupervised methods by interpretability and stability, not accuracy
Introduction

What is this?

Unsupervised learning finds useful structure in data with no y column. Clustering groups similar rows (KMeans, DBSCAN, hierarchical); dimensionality reduction compresses many features into fewer meaningful coordinates (PCA, t-SNE, embeddings); anomaly detection identifies rows that deviate from the norm (z-score, IsolationForest, one-class SVM). The output is judged by coherence, stability and usefulness — never by a held-out label.

Motivation

Why do you need it?

  • Labels are expensive and rare; most raw data is unlabeled and still full of structure.
  • Clusters power segmentation, cold-start recommendations and exploratory taxonomies.
  • PCA/embeddings make huge feature sets tractable and visualizable.
  • Anomaly detection catches fraud, drift and failures before anyone labels them.
Think about it

Mental model

Imagine

Sorting a mixed Lego bin without the instruction booklet

Think of

Clustering and organization without labels

You group by color, size, function as best you can; the quality of the sorting isn't 'right/wrong' but 'useful for building what comes next'.

Mechanics

How it works

  • Cluster: pick a similarity notion (distance), an algorithm (centroid/density/hierarchical), and validate cohesion/separation.
  • Reduce: linear project (PCA) or non-linear embedding (t-SNE/UMAP) retaining the most informative axes.
  • Anomaly: model the normal region (density or distance), flag rows far from it.
  • Evaluate: no labels means silhouette, stability across random seeds, and downstream usefulness.
  • Iterate with domain experts: clusters only matter if the business understands them.

The unsupervised trunk and branches.

Clustering
Reduction
Anomaly
Validate
Hands-on

Examples

One unlabeled set, three lenses

One unlabeled set, three lenses
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.ensemble import IsolationForest
X, _ = make_blobs(n_samples=400, centers=3, n_features=8, random_state=0,
cluster_std=2.2)
km = KMeans(n_clusters=3, n_init=10, random_state=0).fit(X)
print("cluster sizes:", np.bincount(km.labels_))
pca = PCA(n_components=2).fit(X)
print("variance kept by 2 PCs:",
f"{pca.explained_variance_ratio_.sum():.2f}")
iso = IsolationForest(random_state=0).fit(X)
print("anomaly fraction:", f"{(iso.predict(X) == -1).mean():.3f}")

The same unlabeled matrix gets clusters, a compact 2D projection, and an anomaly score — each answering a different business question with no labels.

Real world

In production

  • Retail segmentation: KMeans on spend/recency/frequency powers persona targeting.
  • Document ops: PCA + embeddings compress feature stores before retrieval.
  • Security: IsolationForest flags novel attacker behavior a label-poor SOC can't yet classify.
Guidelines

Best practices

  • Scale before distance-based methods; validate stability by re-running with different seeds; sanity-check clusters with a domain expert before calling them segments; always state what 'good' means in the business's terms.
Watch out

Common mistakes

  • Judging clusters by an invented label; using Euclidean distance on unscaled features; treating PCA components as interpretable causes; declaring clusters 'found' when they're an artifact of random seeds.
When things break

Debugging tips

  • Re-run the same clustering with several random seeds — wildly different centroids = unstable structure.
  • Inspect cluster sizes: a 90/5/5 split smells like a scale artifact, not a real segment.
  • Downstream test: do the clusters predict something the business cares about? If not, revisit.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

Which task has NO labels by construction?

2

PCA is primarily used for:

Go deeper

Official documentation