Module 1: ML vocabulary and learning paradigms 30 min6 min quick readClassical ML
Learning paradigms
Tell supervised, unsupervised, self-supervised, semi-supervised, and reinforcement learning apart by the feedback they learn from, and choose one for a real problem.
Before this: The language of ML
Quick read: the intuition, interactive visualization, summary, and interview questions. The math, implementation, engineering, and knowledge-check sections are hidden.
Mental model. The paradigm is decided by the feedback signal in your data. Correct answers for every example: supervised. No answers: unsupervised. Answers hidden inside the data itself: self-supervised. A few answers: semi-supervised. Rewards for actions: reinforcement learning.
You will learn to
- Define each paradigm by its data signal and goal.
- Choose a paradigm for a concrete problem, and explain what would change the choice.
- Show how self-supervised learning creates labels from raw data (the basis of LLM pretraining).
- Recognize when a clustering result should not be treated as a real category.
- Apply the problem-formulation checklist before picking any algorithm.
Why it matters. Picking an algorithm before framing the problem is the most common way ML projects fail. The paradigm decides what data you need to collect, what you can measure, and what "working" even means. Problem formulation usually matters more than model novelty.
1. Intuition
Think about how people learn.
- A student with an answer key checks each practice problem and corrects mistakes. That is supervised learning: examples paired with correct answers (labels).
- Someone sorting a pile of unlabeled photos into piles that "look alike" has no answer key. That is unsupervised learning: find structure without being told what it is.
- A child learns language by hearing sentences and guessing the next word, then hearing the actual word. Nobody wrote labels; the data checks itself. That is self-supervised learning, and it is how large language models are pretrained.
- A student with a few worked examples and a big stack of unanswered problems learns from both. That is semi-supervised learning.
- Learning to ride a bike: no one labels each muscle movement; you get feedback (balance, falling) after sequences of actions. That is reinforcement learning.
2. Visualization
Which learning paradigm fits?
Answer three questions, or load one of the seven scenarios.
Scenarios
Recommendation
Answer question 1
Try it (predict first, then check):
- Before loading a scenario, decide which paradigm fits fraud detection. Then load it and compare.
- Change only the first answer (the feedback signal) and watch the recommendation change. Why does the signal matter more than the output type?
3. The math
Symbols
| Symbol | Meaning |
|---|---|
| input features of one example | |
| its label (target) | |
| a model with parameters | |
| per-example loss comparing prediction and label | |
| the dataset | |
| state, action, reward (reinforcement learning) | |
| a policy: a rule mapping states to actions | |
| discount factor in |
Each paradigm as an objective
Supervised: labeled pairs . Minimize average loss.
Regression when is a number; classification when is a category.
Unsupervised: inputs only, . Optimize a structural objective instead of agreement with labels, for example K-means' within-cluster distance , or PCA's retained variance.
Self-supervised: build pairs from unlabeled data with a rule. For language, the target at position is token and the input is everything before it:
The math is supervised; the labels are free.
Semi-supervised: a small labeled set plus a large unlabeled set . One simple recipe (self-training): fit on , predict on , add confident predictions as pseudo-labels, refit.
Reinforcement learning: an agent in state takes action , receives reward , and moves to . Find a policy that maximizes expected discounted return:
Worked example: making self-supervised labels
From the sentence "the model predicts the next token from the previous tokens" (10 words), next-word prediction creates 9 training pairs with no human labeling:
| Input (context) | Target |
|---|---|
| the | model |
| the model | predicts |
| the model predicts | the |
| the model predicts the | next |
| … | … |
| the model predicts the next token from the previous | tokens |
A sentence of tokens yields examples. A trillion-token corpus yields about a trillion examples, which is why self-supervision scales so far beyond human labeling.
Worked example: choosing a paradigm
Problem: flag fraudulent card transactions.
- Decision: block, review, or approve each transaction.
- Prediction unit: one transaction.
- Trustworthy target? Yes, but delayed: chargebacks confirm fraud weeks later. About 0.1% are fraud.
- Information at prediction time: amount, merchant, device, recent account history, but not the chargeback outcome.
- Metric: precision and recall at a threshold chosen from the cost of a missed fraud versus a blocked good customer; not accuracy (99.9% for "never fraud").
- Baseline: rules such as "amount > 5× the customer's median."
Choice: supervised classification on confirmed labels, with a time-based split. Add an unsupervised anomaly score as a feature to catch new fraud patterns that have no labels yet.
4. Implementation
Build it from scratch
The point of this build: the same 200 points (two synthetic Gaussian blobs) are handled four ways, and the only thing that changes is the feedback signal. Each learner is a few lines of NumPy.
Supervised. Every row has a label, so we can minimize a loss against it. Logistic regression by gradient descent on log loss: X is , a bias column makes it , and w is .
def fit_logistic(X, y, steps=2000, lr=0.1):
"""Supervised: gradient descent on log loss. X is [n, 2], y is [n] of 0/1."""
Xb = np.c_[X, np.ones(len(X))] # add a bias column -> [n, 3]
w = np.zeros(3)
for _ in range(steps):
p = 1 / (1 + np.exp(-Xb @ w)) # predicted P(y = 1)
w -= lr * Xb.T @ (p - y) / len(y) # gradient of mean log loss
return wPredict: the line w -= lr * Xb.T @ (p - y) ... uses y. Which line would you have to delete if the labels did not exist?
Unsupervised. No y anywhere. K-means optimizes a structural objective (within-cluster distance) by alternating two steps. The [n, k] distance matrix is built by broadcasting [n, 1, 2] against [1, k, 2].
def kmeans(X, k, rng, iters=50):
"""Unsupervised: alternate 'assign each point to nearest centre' and 'move centre to its points' mean'."""
centres = X[rng.choice(len(X), k, replace=False)]
for _ in range(iters):
d = ((X[:, None, :] - centres[None, :, :]) ** 2).sum(axis=2) # [n, k] squared distances
assign = d.argmin(axis=1)
centres = np.array([X[assign == c].mean(axis=0) for c in range(k)])
return assign, centresPredict: K-means returns cluster ids 0 and 1. Is cluster 0 guaranteed to be the blob with label 0? How should you compare clusters with labels?
Semi-supervised. Only 6 rows are labeled (-1 marks the rest). Self-training fits on the labeled rows. It then promotes unlabeled rows the model is at least 90% sure about to pseudo-labels, and refits.
def self_train(X, y_partial, threshold=0.9, rounds=10):
"""Semi-supervised: fit on labeled rows, pseudo-label confident unlabeled rows, refit. -1 = unlabeled."""
y = y_partial.copy()
for _ in range(rounds):
known = y != -1
w = fit_logistic(X[known], y[known])
p = predict_proba(w, X)
confident = (~known) & ((p > threshold) | (p < 1 - threshold))
if not confident.any():
break
y[confident] = (p[confident] > 0.5).astype(int)
return wPredict: if the first model is confidently wrong on some points, what happens to those mistakes in later rounds?
Self-supervised. The labels come from the data itself: each word's target is the next word. A bigram counter is the smallest "language model" trained this way.
def next_word_pairs(text):
"""Self-supervised: every position becomes a (previous word, next word) example. No human labels."""
words = text.split()
return list(zip(words[:-1], words[1:]))
def fit_bigram(pairs):
counts = defaultdict(Counter)
for prev, nxt in pairs:
counts[prev][nxt] += 1
return {prev: c.most_common(1)[0][0] for prev, c in counts.items()}Predict: a 33-word text gives how many training pairs? After training, fit_bigram is a supervised model. What made the setup self-supervised?
Use the library
The scikit-learn versions do the same jobs with better solvers (L-BFGS for logistic regression, k-means++ initialization with several restarts for K-means).
from sklearn.cluster import KMeans
from sklearn.linear_model import LogisticRegression
from sklearn.semi_supervised import SelfTrainingClassifier
# Supervised: features and labels
clf = LogisticRegression().fit(X, y)
# Unsupervised: features only
clusters = KMeans(n_clusters=2, n_init=10).fit_predict(X)
# Semi-supervised: -1 marks unlabeled rows
semi = SelfTrainingClassifier(LogisticRegression(), threshold=0.9).fit(X, y_partial)
# Self-supervised: build (context, next-token) pairs from raw text
tokens = text.split()
pairs = [(tokens[:i], tokens[i]) for i in range(1, len(tokens))]Run it
From guide/:
uv run code/01-ml-vocabulary/paradigms_scratch.pyKey output:
Supervised, 200 labels: accuracy 0.965
Unsupervised, 0 labels: clusters match hidden labels 0.970
6 labels, supervised on those 6: accuracy 0.965
6 labels, self-training on 200: accuracy 0.975
Self-supervised: 33 words -> 32 (input, target) pairs, e.g. [('the', 'cat'), ('cat', 'sat'), ('sat', 'on')]
Learned next word: sat -> 'on', on -> 'the', cat -> 'sat'
scikit-learn, same data:
LogisticRegression, 200 labels: accuracy 0.970
KMeans, 0 labels: match 0.970
SelfTrainingClassifier, 6 labels: accuracy 0.975All four paradigms land near 97% here, because the two blobs are well separated. That is the honest lesson: when the structure in already lines up with the labels, few or no labels go a long way. On overlapping classes, clusters and pseudo-labels drift away from the truth, and labels matter much more. (Accuracies are on the same 200 training points, to compare paradigms, not to estimate generalization.) The figure comes from code/01-ml-vocabulary/paradigms.py.
5. Engineering
Labels are the expensive part. Supervised learning is the most predictable paradigm if labels exist and are trustworthy. Ask: who produces the labels, how noisy are they, how delayed, and do they drift? Weak labels (heuristics), active learning (label the most informative examples), and pretrained representations all reduce labeling cost.
Self-supervision as a first stage. Modern practice often pretrains a representation with self-supervision on huge unlabeled data, then adapts it with a small labeled set (fine-tuning) or uses it as-is (embeddings, prompting). That is the LLM recipe.
Reinforcement learning is expensive. It needs an environment or simulator, many interactions, and careful reward design (agents exploit loopholes in rewards). Many "RL problems" are better framed as supervised learning on logged decisions first. In LLMs, RL appears in preference optimization (RLHF), where a reward model is trained on human preference labels.
Paradigms combine. Recommendation systems mix self-supervised embeddings, supervised ranking on clicks, and sometimes RL-style exploration. Anomaly detection is unsupervised until labeled incidents accumulate, then becomes supervised.
Key distinctionA discovered cluster is not a verified category. Unsupervised methods always return structure, even in random data. Treat clusters as hypotheses to validate with domain experts and downstream outcomes.
Watch outFailure modes. Labels that leak the future (a "was refunded" feature in a fraud model); proxy labels that measure the wrong thing (clicks instead of satisfaction); pseudo-labels that amplify the model's own mistakes in semi-supervised training; reward hacking in RL.
Common mistakes
- Choosing an algorithm before defining the target, the prediction unit, and the metric.
- Calling a problem "unsupervised" when labels could be collected cheaply.
- Evaluating an unsupervised model only by its own objective (inertia), not by usefulness.
- Assuming self-supervised pretraining removes the need for evaluation data. You still need labeled examples to measure the task.
6. Knowledge check
Quiz: Learning paradigms
Match each problem to its most natural paradigm.
Practice exercise. For each problem, name the paradigm and the feedback signal: (a) predicting tomorrow's electricity demand from past demand and weather; (b) grouping support tickets into topics nobody has defined; (c) training an image model to predict a hidden patch of each image from the visible patches.
Solution
(a) Supervised regression; the label is the actual demand, which arrives the next day. Use a time split. (b) Unsupervised clustering (or topic modeling); no labels. Validate topics with the support team. (c) Self-supervised; the hidden patch is the label, created from the image itself.
Implementation challenge. Using the script's synthetic data, vary the number of labeled points from 2 to 50 and plot accuracy for "train on labeled points only" versus SelfTrainingClassifier. Then make the blobs overlap more (cluster_std=2.5) and see whether self-training still helps.
Summary
- The feedback signal decides the paradigm: labels, no labels, self-made labels, few labels, or rewards.
- Supervised learning minimizes loss against labels; unsupervised optimizes a structural objective; self-supervised manufactures labels from raw data.
- LLMs are pretrained with self-supervision (next-token prediction) and then adapted with supervised and preference-based methods.
- Frame the problem first: decision, prediction unit, trustworthy target, available information, metric, baseline.
- Clusters are hypotheses, not facts.
Next: Workflow, splits, and leakage
Related: K-means · Tokenization and pretraining · The language of ML
Interview angle
In what sense is LLM pretraining "self-supervised," and how does it differ from supervised learning?
The objective is mathematically supervised: maximize , a cross-entropy over the vocabulary at every position. The difference is where labels come from: they are manufactured from the data itself, with each token serving as the target for the context before it. A sequence of tokens yields training pairs with no human annotation, so a trillion-token corpus gives about a trillion examples. Label cost drops to zero, so the dataset scales with available text instead of an annotation budget, and to predict well the model has to learn syntax, facts, and some reasoning. Other self-supervised tasks include masked-token prediction (BERT), masked image patches, and contrastive learning between augmented views. Pretraining is then followed by supervised fine-tuning and preference optimization to make the model useful.
You have 500 labeled support tickets and 200,000 unlabeled ones. What approaches would you consider, and in what order?
First a baseline: TF-IDF plus logistic regression on the 500 labels, scored with cross-validation, so every later idea has a number to beat. Then, usually the biggest win: pretrained representations. Embed every ticket with a sentence-embedding model and train a light classifier on top, or fine-tune a small pretrained model; much of the unlabeled data's value is already captured by pretraining. Self-training: predict on the unlabeled pool, add confident pseudo-labels, refit; it helps when classes are well separated, but confirmation bias can amplify the model's own errors. Active learning: label the tickets the model is least sure about, so each labeling hour counts more. Weak supervision: keyword rules or LLM zero-shot labels as noisy training data, checked against a labeled sample. Whatever you do, keep a clean, human-labeled test set for the final evaluation.
A team ran K-means on customers and launched a campaign targeting "cluster 3, our high-value customers." Results disappointed. What likely went wrong?
They treated a cluster as a verified category. K-means always returns groups, even in data with no real group structure, and it minimizes within-cluster distance, not anything about value. Questions to ask: were features scaled, or did one large-unit feature such as total spend define the clusters? Is cluster 3 stable across random seeds and subsamples, or does its membership change every run (cluster numbers are arbitrary and get renumbered on refit)? Was it validated against an outcome, such as future revenue or response to past campaigns? Was "high-value" read off the centroid, which describes the average but not most members? Better framing: if the goal is campaign response, that's a supervised problem. Run a small randomized campaign, collect responses, and model who responds. Use clusters as hypotheses and descriptions, not targeting rules.
When would you frame a recommendation problem as reinforcement learning rather than supervised learning?
When the model's actions change the data it later learns from and the reward arrives after sequences of decisions. Recommendation partly fits: what you show determines what users can click, so logged clicks are biased toward past choices, and long-term satisfaction depends on many recommendations, not one. Even so, most production systems start supervised: train a ranker on logged impressions and clicks, which is stable, cheap, and debuggable. Then add exploration (epsilon-greedy or contextual bandits) to collect less biased feedback, and use off-policy evaluation with propensity weighting to estimate how a new policy would do from old logs. Full RL is justified with a simulator or very high interaction volume and a well-defined reward; otherwise reward design invites hacking, such as optimizing clickbait. Rule of thumb: supervised on logs first, bandits second, full RL last.