Handbooks  /  ML Fundamentals
Handbook~28 min readBeginner → Intermediate
Deep Dive

The seven ML ideas
everyone confuses.

Most machine learning confusion isn't about math — it's about seven distinctions that sound similar and mean opposite things. Here they are in the order they actually matter: how a model learns, what it predicts, how it fails, how you measure it, how you validate it, how you combine it, and what it models about probability. Each one with real numbers.

00

The map

Every one of these seven pairs answers a different question, and they stack. You can't sensibly pick a metric before you know whether you're classifying or regressing, and you can't diagnose an ensemble before you can tell bias from variance. Read them in order and each one hands you the vocabulary for the next.

Seven distinctions, in the order they bite you
01Supervised / unsupervised / RL — how the model gets its learning signal
02Classification / regression — what shape the answer takes
03Bias / variance — the two ways a model can be wrong
04Precision / recall — the two ways a classifier can be wrong
05Hold-out / cross-validation — how much to trust a score
06Bagging / boosting — two opposite ways to combine models
07Discriminative / generative — boundary, or distribution
Sections 03 and 06 are the same axis seen twice — diagnosis, then treatment.
01

The three ways a machine learns

Labeled flashcards, an unsorted toy box, or a game with a score.

Ask five people what "machine learning" means and you'll get five vague answers, but underneath almost all of it there are only three classic ways to actually teach a machine — and the split is the same one you'd use to teach a child. You can drill it with flashcards that have the answer on the back. You can hand it a pile of unsorted objects and let it work out the groupings itself. Or you can turn the whole thing into a game with a score and let it learn from winning and losing. Which one applies to your problem isn't a matter of taste — it's decided entirely by what you're able to give the model to learn from.

Supervised learning: labeled examples

Supervised learning is the flashcard method. You give the model thousands of examples where the correct answer is already attached — this photo is labeled cat, this email is labeled spam, this house sold for this exact price. The model's whole job is to learn the mapping from input to answer, so that when it sees a new, unlabeled input it can produce the right answer on its own.

Supervised learning is the workhorse of practical ML because it's direct: you know exactly what you're optimizing for, and you can measure how wrong the model is against labels you already hold. The catch is that someone has to write every flashcard. ImageNet — the dataset that kicked off the deep learning era — required roughly 14 million images to be hand-annotated through crowdsourced labor over several years. That cost is the single biggest practical constraint on supervised learning, and it's why so much of modern ML is an attempt to route around it.

Unsupervised learning: find the pattern yourself

Unsupervised learning removes the answer key entirely. You hand the model raw, unlabeled data and it has to find structure on its own — nobody tells it what the "right" grouping is, because there isn't one written down anywhere. It's the equivalent of dumping a toy box on the floor and letting a kid sort it by color or shape with no one naming the piles.

That self-discovered structure shows up in three recognizable forms. Clustering groups similar points without being told what the groups mean — k-means is the canonical algorithm, and you can watch it converge step by step in the k-means clustering lab. Dimensionality reduction squeezes data down to its essential structure, discarding redundancy (PCA, autoencoders, UMAP). Anomaly detection flags points that don't fit the pattern the rest of the data follows — useful for catching fraud or a failing sensor without ever having seen a labeled example of "fraud."

The advantage is that unsupervised learning runs on the enormous volume of raw data that already exists, with none of the labeling cost. The tradeoff is that you don't get a specific answer back — you get structure, and it's on you to decide what it means. This is also why unsupervised results are hard to evaluate: there's no ground truth to score against, so you fall back on proxy metrics (silhouette score, reconstruction error) that only loosely track whether the structure is useful.

Reinforcement learning: learn from reward and penalty

Reinforcement learning throws out both the flashcards and the toy box. There's no fixed dataset at all — just an agent, an environment, and a goal. The agent takes an action, the environment responds with a reward or penalty and a new state, and through repeated trial and error the agent learns a policy: a strategy for choosing actions that maximizes total reward over time.

This is how a dog learns tricks for treats, how AlphaGo taught itself to beat the best human players by playing itself millions of times, and how a simulated robot learns to walk without falling. None of it comes from labeled examples of "the correct move." It comes from feedback earned by acting. Two properties make RL genuinely different: the agent generates its own data by interacting, and the reward is often delayed — a chess move's real consequence lands forty moves later, which is the credit-assignment problem RL exists to solve.

SupervisedUnsupervisedReinforcement
NeedsLabeled examples (input + correct answer)Raw, unlabeled dataAn environment, an agent, and a reward signal
LearnsThe mapping from input to answerStructure the data already containsA policy that maximizes reward over time
FeedbackExact, immediate, per-exampleNone — no ground truthScalar, often delayed and sparse
ExampleSpam detection, price predictionCustomer segmentation, anomaly detectionAlphaGo, a robot learning to walk, RLHF
Self-supervised learning — the modern fourth option

Instead of paying humans to write flashcards, let the raw data write its own. Hide the next word in a sentence and the "label" is simply whatever word actually comes next; mask a patch of an image and the label is the patch you removed. That turns an ordinary unlabeled pile of text into a supervised-style training signal with zero manual labeling — which is exactly why it scaled to the size it did. Mechanically it is supervised learning; economically it behaves like unsupervised, because the labels are free.

→ The failure mode

Forcing a sequential-decision problem into a supervised frame. If your labels were generated by a policy that was itself acting in the world — past pricing decisions, past ad placements, past moderation calls — a supervised model learns to imitate that policy, including its blind spots, and never discovers what would have happened under different actions. That gap between imitating logged behaviour and optimizing outcomes is why recommendation and pricing teams eventually reach for bandits or RL.

▶ Watch it explained

Supervised vs Unsupervised vs Reinforcement: The 3 Ways a Machine Learns

02

Classification vs regression

Which bucket, or how much?

Show a model a photo of a dog and you can ask it two very different kinds of questions. "What animal is this?" wants a label pulled from a short list. "How old is it?" wants a number that could land almost anywhere. That split — which bucket, or how much — is the first fork in the road for nearly every supervised problem, and it decides the model's output layer, its loss function, and how you'll score it, before you write a line of training code.

Classification: sort into a bucket

Classification sorts an input into one of a fixed set of discrete categories. Spam or not spam. Cat, dog, or bird. Fraud or legitimate. The output is always a label from a finite, predetermined list — there's no "spam-and-a-half," no answer that falls between two categories. Under the hood, a classifier is carving the input space into regions, one per bucket; the perceptron playground lets you drag points around and watch a linear boundary move in response, which is the whole idea in its simplest form.

Classifiers are trained against cross-entropy loss, which measures the negative log of the probability the model assigned to the correct answer. The shape of that penalty is the point — it grows without bound as the model becomes confidently wrong:

Model said P(spam)TruthCross-entropy lossReading
0.92spam−ln(0.92) = 0.083Confident and right — near-zero penalty
0.60spam−ln(0.60) = 0.511Right but hedging — mild penalty
0.50either−ln(0.50) = 0.693The coin-flip baseline every model must beat
0.92not spam−ln(0.08) = 2.526Confidently wrong — 30× the cost of being right
0.99not spam−ln(0.01) = 4.605Arrogantly wrong — the loss blows up

That asymmetry is deliberate. Cross-entropy doesn't just want the right answer, it wants calibrated confidence, and it punishes swagger harder than uncertainty. This is why a model trained with cross-entropy usually produces more trustworthy probabilities than one trained to maximize raw accuracy.

Regression: predict a number

Regression answers a different question: not which one, but how much. It predicts a continuous quantity — a point that can fall anywhere on a number line. Tomorrow's temperature, a house price, a person's age from a photo. There are no buckets to sort into at all, so a regression model fits a curve through the data rather than drawing boundaries between classes, and you score it by how far off it was rather than whether it was strictly right.

Predicting $402,000 for a house that sold for $400,000 is a much better outcome than predicting $250,000, even though neither is exactly correct. Squared error captures that directly — and the squaring is not cosmetic:

MSE = mean( (ŷ − y)² )  ·  error of 2k → 4M  ·  error of 150k → 22,500M  ·  5,625× the penalty for 75× the error

Squaring makes one catastrophic miss cost more than thousands of small ones — great for suppressing large errors, terrible when your data has genuine outliers. That's what MAE and Huber loss exist to fix.

Both losses are optimized the same way: compute the gradient of the loss with respect to every parameter and step downhill. The gradient descent lab shows that descent on a live surface, and the loss landscape lab shows why the shape of the surface — not just the loss value — determines whether the optimizer ever gets there.

The twist: classifiers output a number first

Here's where the line blurs. A classifier almost never jumps straight to a bare category. Internally it first produces a number — a probability like 0.92 — and only then applies a threshold to turn that probability into a bucket. Above the threshold, call it spam; below it, don't.

That's exactly why logistic regression, despite the word "regression" in its name, is a classification algorithm. It regresses onto a probability between 0 and 1, and a threshold classifies that probability into a label. The regression part is real; it's just the first half of the pipeline. It works in reverse too: any regression output can be turned into a classification by bucketing the number after the fact — turning a predicted price into "cheap," "mid-range," or "pricey." Holding onto that intermediate probability rather than throwing it away is what makes section 04 possible at all.

ClassificationRegression
OutputA discrete label from a fixed setA continuous number
Final layerSoftmax / sigmoid over classesA single linear unit, no squashing
LossCross-entropySquared error (MSE), MAE, Huber
MetricAccuracy, precision, recall, F1, AUCRMSE, MAE, R²
ExampleSpam or not spamTomorrow's temperature
→ The failure mode

Regressing on labels that only look numeric. A 1-to-5 star rating is ordinal: the order is real but the spacing isn't. Fit MSE to it and you assert that the gap from 1★ to 2★ equals the gap from 4★ to 5★, and that predicting 3.7 stars is a meaningful answer. The reverse mistake is just as common: one-hot classifying a genuinely ordered target, so the model is penalized identically for guessing 4★ instead of 5★ as for guessing 1★. Ordinal targets need ordinal regression or a cumulative-link model, not whichever of the two defaults you reached for first.

▶ Watch it explained

Classification vs Regression: Which Bucket, or How Much?

03

Bias vs variance

The two ways a model misses — and why you can't kill both.

Train a model, watch its training accuracy climb, and it's tempting to assume you're winning. Often you're not: pushing training accuracy higher can make the model worse on the unseen data you actually care about. That paradox exists because there are two completely different ways a model can miss, and they pull in opposite directions.

The dartboard

Bias is how far the center of your dart cluster sits from the bullseye — your aim. Variance is how spread out the darts are — your consistency. A high-bias model throws a tight cluster into the wrong corner. A high-variance model scatters all over the board, averaging out near the middle but never landing there twice.

Bias: consistently off-target

A high-bias model is too simple for the pattern it's trying to learn. It makes strong, often wrong, assumptions about the shape of the data — a straight line forced through a curved relationship — and no amount of extra training data will fix that, because the model simply can't represent the real function. This is underfitting, and its signature is that it shows up on both training and test data. The model isn't memorizing noise; it never learned the signal in the first place.

Variance: inconsistent across datasets

A high-variance model sits at the opposite extreme: flexible enough to memorize the training data's every quirk and every bit of random noise, not just the underlying signal. This is overfitting. It aces the data it trained on and falls apart the moment it sees anything new. Retrain it on a slightly different sample of the same population and it lands somewhere completely different — that instability is the variance.

The numbers

Here's what the two look like in a training log. Fit polynomials of increasing degree to the same noisy dataset, and watch training RMSE and test RMSE diverge:

ModelTrain RMSETest RMSEGapDiagnosis
Degree 1 (line)4.824.970.15High bias. Bad on both — the model can't even fit what it saw.
Degree 31.942.110.17Balanced. Low error, small gap — the bottom of the U.
Degree 61.023.402.38Variance creeping in — train improves, test degrades.
Degree 120.286.446.16High variance. Near-perfect on train, worse than the line on test.

Read the table the way a practitioner does. Both numbers high and close together means bias — add capacity. Train low, test high, wide gap means variance — add data or regularization. The degree-12 row is the whole lesson in one line: it fit its training set seventeen times better than the straight line and generalized worse than it.

Why you can't minimize both

Bias and variance move in opposite directions as you change model complexity. More expressive model → lower bias (it can represent more), higher variance (that same flexibility latches onto noise). Simpler model → the reverse. Expected test error decomposes cleanly:

E[(y − ŷ)²] = Bias² + Variance + σ² ← irreducible noise, nothing removes this

Plot that sum against complexity and it traces a U. Too simple is high on the left, too complex is high on the right, and the best model sits at the bottom — where the sum is smallest, not where either term is zero.

That reframes the goal entirely. You were never trying to eliminate bias or eliminate variance; you're looking for the minimum of their sum. And note the σ² floor: if your labels themselves are noisy — two annotators disagreeing 8% of the time — no model can beat 92% no matter what you do. Knowing that number stops you from burning a quarter chasing an impossible target.

High biasHigh variance
ModelToo simpleToo complex
SymptomUnderfits — bad on train and testOverfits — great on train, bad on test
More data helps?No — it plateaus immediatelyYes — directly shrinks variance
FixMore capacity: deeper model, more features, fewer assumptionsMore data, regularization, early stopping, dropout, simpler model

Regularization deserves a note because it's the most direct lever on variance: an L2 penalty shrinks weights, tree pruning caps depth, dropout randomly disables units during training. All of them buy a little bias to shed a lot of variance. Which lever fires first also depends on the optimizer — you can race several against each other on the same surface in the optimizer race lab, and see how activation choice reshapes what the network can represent in the activation functions lab.

→ The failure mode

Tuning against the test set. Every time you look at test error and change something in response, a little of that test set leaks into your model, and the number stops being an estimate of generalization. After twenty rounds of "try it and check the test score," you've effectively fit the test set by hand — the same overfitting you were trying to detect, one level up. Keep a validation set for decisions and a test set you touch exactly once. That discipline is what section 05 formalizes.

▶ Watch it explained

Bias vs Variance: The Two Ways a Model Misses

04

Precision vs recall

Which mistake can you live with?

A classifier that's "98% accurate" can still be useless, because accuracy hides the detail that matters: there are two completely different ways to be wrong. A spam filter can flag a real email you needed — a false alarm — or let actual spam through — a miss. Lumping those into one number throws away exactly the information you need.

A real confusion matrix

Take 10,000 emails, of which 400 are genuinely spam (4% — realistic class imbalance). Your filter flags 350 of them, and 300 of those flags are correct:

Actually spamActually not spamRow total
Flagged spamTP = 300FP = 50 (false alarms)350
Not flaggedFN = 100 (misses)TN = 9,5509,650
Column total4009,60010,000
Precision = TP / (TP + FP) = 300 / 350 = 85.7%
Recall    = TP / (TP + FN) = 300 / 400 = 75.0%
F1        = 2PR / (P + R) = 2(0.857)(0.750) / 1.607 = 0.800
Accuracy  = (300 + 9,550) / 10,000 = 98.5%

Precision looks down the flagged column; recall looks across the actually-spam row. Same 300, different denominator.

Now the punchline. A model that flags nothing at all — TP 0, FP 0, FN 400, TN 9,600 — scores 96% accuracy on this dataset while being worth precisely zero. Your real model beats it by only 2.5 accuracy points, which makes accuracy nearly useless as a signal here. That's the accuracy paradox, and it's why imbalanced problems are always reported with precision and recall instead.

The threshold is the dial

Precision and recall aren't independent knobs — they're two ends of the same dial: the decision threshold. Most classifiers output a suspicion score (section 02), and the threshold decides how much suspicion is enough to flag. Same model, same weights, three thresholds:

ThresholdTPFPFNPrecisionRecallF1
0.90 cautious2001020095.2%50.0%0.656
0.50 default3005010085.7%75.0%0.800
0.20 aggressive3806002038.8%95.0%0.551

Nothing about the model changed across those three rows. Moving the threshold doesn't make a classifier better — it chooses which mistake you'd rather make more of. Note how brutally precision collapses at 0.20: catching the last 80 spam messages cost 550 extra false alarms, because there are 9,600 negatives and even a 6% false-positive rate on them swamps the 400 positives. Under heavy imbalance, precision is dominated by the size of the negative class, which is exactly why ROC-AUC can look reassuring while the precision-recall curve tells you the model is unusable.

Which one should you optimize?

There's no universally best threshold, because the right setting depends on the cost of each mistake, not on chasing the highest accuracy.

  • A cancer screening test wants recall. Missing a real case is catastrophic, so you accept more false alarms — healthy patients who get a follow-up test — in exchange for almost never missing someone sick.
  • A "delete forever" spam filter wants precision. Destroying one real email is unacceptable, so you'd rather let some spam through.
  • Ranked results (search, recommendations) want precision@k: only the top handful is ever seen, so recall over the whole corpus is irrelevant.

When both errors genuinely matter and you need one number to compare models by, use F1, the harmonic mean. Because it's harmonic rather than arithmetic, it punishes lopsidedness hard: the 0.20-threshold row above averages to 66.9% arithmetically but scores just 0.551 on F1. If the two errors have genuinely different costs, use Fβ — β > 1 weights recall, β < 1 weights precision. The same framework carries directly into LLM evaluation, where these are the metrics behind pass rates and guardrail scoring; the LLM evals interview handbook works through that version, and agent evals extends it to multi-step traces where a single "positive" spans a whole trajectory.

→ The failure mode

Picking the threshold on the test set. The threshold is a hyperparameter like any other, and choosing the one that maximizes F1 on your test set inflates the reported score — sometimes by several points on small or imbalanced data. Sweep the threshold on validation, freeze it, then report test. And re-check it after every retrain: a model whose score distribution shifts slightly will silently drift to a completely different operating point at the same numeric threshold.

▶ Watch it explained

Precision vs Recall: Which Mistake Can You Live With?

05

Cross-validation

One exam, or five?

Grade a student on one random exam and the grade can lie — a lucky topic or an unlucky night's sleep says nothing reliable about what they know. Give them five exams on different material and average the results, and the grade starts to mean something. Measuring a model works the same way, and the choice between one exam and five is exactly the choice between a single hold-out set and k-fold cross-validation.

What's wrong with a single split

Carving off 20% as a test set, training on the rest, and scoring on the part the model never saw is a real improvement over scoring on training data. But a single split has two problems that don't go away just because the split was random.

Fragility. The score depends entirely on which rows landed in the test set. Shuffle differently and you get a different number, though nothing about the model changed. On a large dataset that noise washes out; on a small one it can swing enough to make a mediocre model look good.

Waste. Whatever rows you set aside never help the model learn. On a small dataset that's not a minor inefficiency — it's data held hostage to produce one score you don't fully trust.

How k-fold works

One idea fixes both: instead of one fixed split, rotate the test set through the whole dataset and average.

5-fold cross-validation — ■ test fold, □ training folds
Round 1■ □ □ □ □   train on folds 2-5, score on fold 1 → 0.81
Round 2□ ■ □ □ □   train on folds 1,3-5, score on fold 2 → 0.88
Round 3□ □ ■ □ □   train on folds 1,2,4,5, score on fold 3 → 0.79
Round 4□ □ □ ■ □   train on folds 1-3,5, score on fold 4 → 0.86
Round 5□ □ □ □ ■   train on folds 1-4, score on fold 5 → 0.84
Every row is tested exactly once and trained on exactly k − 1 = 4 times. Nothing is wasted.
scores = [0.81, 0.88, 0.79, 0.86, 0.84]  →  mean = 0.836, std = 0.033  →  report 0.836 ± 0.033

A single hold-out could have returned 0.79 or 0.88 from this exact model. Same model, a 9-point swing, decided by the shuffle.

That spread is the part people skip, and it's the most useful output. Five fold-scores clustered within a point of each other means you can trust the average. Five scores bouncing from 0.62 to 0.91 means the model's true performance is genuinely uncertain — something a single hold-out would have hidden behind one confident-looking figure. When you compare two models whose means differ by 0.01 and whose fold-standard-deviations are 0.03, the honest conclusion is that you can't tell them apart yet.

Choosing k, and the variants that matter

SetupCostUse when
Hold-out (single split)1× trainingLarge data, or training is expensive. Deep learning almost always lives here.
5-fold5× trainingThe default. Good variance reduction for a tolerable price.
10-fold10× trainingSmall data where you want more training rows per fold (90% vs 80%).
Leave-one-out (k = n)n× trainingTiny datasets only. Nearly unbiased, but high variance and brutally slow.
Stratified k-foldSame as k-foldAny imbalanced classification — mandatory, not optional (see below).
Time-series splitk× trainingAnything temporal. Folds must move forward in time, never shuffle.
Grouped k-foldk× trainingRepeated measurements per user/patient/device — keep a group inside one fold.

K-fold earns its keep in two situations. First, when data is scarce — small or expensive-to-label datasets are exactly where a single split's variance is largest and where you can least afford to spend rows on testing alone. Second, for hyperparameter tuning and model selection, where the score drives a decision rather than a report. Picking a learning rate or a champion model off one noisy hold-out score means your decision might just be chasing that split's luck. If you're tuning and reporting, you need nested CV: an inner loop that selects hyperparameters and an outer loop that scores the whole selection procedure — otherwise the reported number is optimistic for the same reason as tuning on the test set. Once the protocol is settled, freeze it and run it automatically on every change; wiring evaluation into the pipeline so a regression fails the build is what evals in CI covers.

→ The failure mode

Leakage across folds. Fit your scaler, imputer, feature selector, or target encoder on the whole dataset before splitting, and every fold's training data has already seen statistics from its own test fold. The CV score comes back beautiful and production performance collapses. Every preprocessing step that learns anything from data must be fitted inside each fold — which is exactly what a scikit-learn Pipeline passed to cross_val_score does for you. The same trap in a different costume: shuffling time-series rows, so the model trains on the future and predicts the past.

▶ Watch it explained

Cross-Validation: One Exam, or Five?

06

Bagging vs boosting

A jury, or a relay?

A single deep decision tree memorizes its training data and overfits; a single shallow tree is too weak to learn much of anything. The fix in both cases isn't a cleverer tree — it's a crowd of them. But how you build that crowd matters, and there are two opposite philosophies. Bagging trains many models independently and averages their votes. Boosting trains models one after another, each aimed squarely at the last one's mistakes. They look like variations on one idea; they fix opposite failure modes — the exact two from section 03.

Bagging: independent models, averaged votes

Bagging — bootstrap aggregating — trains a batch of models fully independently. Each sees its own random bootstrap sample of the training data (drawn with replacement, so some rows repeat and roughly 37% are left out entirely), and often a random subset of features too. None of them know what the others are doing. Their predictions are then averaged for regression, or majority-voted for classification.

The intuition is a jury: each juror reasons from a slightly different slice of the evidence, so each is wrong sometimes — but their mistakes aren't correlated, so averaging cancels the noise. The math is exact and worth carrying around. Average n estimators that each have variance σ² and pairwise correlation ρ:

Var(average) = ρσ² + (1 − ρ)σ² / n

The second term vanishes as you add trees. The first term does not — it is a hard floor set by how correlated the members are.

Put numbers in it. With σ² = 1 and ρ = 0.1, one hundred trees give 0.10 + 0.009 = 0.109 — a 89% variance reduction. Go to a thousand trees and you get 0.1009: essentially nothing more, because you've hit the ρσ² = 0.1 floor. But drop correlation to ρ = 0.05 with the same hundred trees and variance falls to 0.0595 — nearly half again. That single equation explains the entire design of random forest: since adding trees saturates quickly, the real lever is decorrelating them, which is why random forest samples a random subset of features at every split rather than just bootstrapping rows. Bagging also parallelizes perfectly — no ordering dependency means every tree can train at once, on a different core or a different machine.

Boosting: sequential models, each fixing the last

Boosting inverts the approach. Models are trained one at a time, and each new model is deliberately pointed at whatever the previous ones got wrong. AdaBoost does this by reweighting examples — cases the ensemble currently misses get more weight, forcing the next model to attend to them. Gradient boosting, the modern default, fits each new model to the residual errors left over from the ensemble so far, which is literally gradient descent in function space. The models are combined as a weighted sum, each scaled by a learning rate (typically 0.01–0.1) so no single stage can overcorrect.

The intuition is a relay of specialists: the first model takes a rough pass, the second's entire job is to clean up what the first missed, the third cleans up what's left. Because each stage explicitly hunts the ensemble's current errors, boosting is exceptional at turning weak learners into a strong one — it attacks bias. This is why gradient-boosted trees (XGBoost, LightGBM, CatBoost) still win most tabular competitions. Two costs: it's inherently sequential (stage n needs stage n−1's residuals, so you can't parallelize across models, only within a tree), and because it keeps chasing whatever it currently gets wrong, it will eventually chase noise.

BaggingBoosting
TrainingIndependent, in parallelSequential, one after another
Each model seesA bootstrap sample + feature subsetThe full data, weighted by current error
CombinationEqual-weight vote or averageWeighted sum, scaled by learning rate
Base learnerDeep, low-bias, high-variance treesShallow stumps — high-bias, weak on purpose
ReducesVariance (overfitting)Bias (underfitting)
More roundsSafe — plateaus, never overfits from count aloneDangerous — will overfit past the optimum
AlgorithmsRandom forest, extra treesAdaBoost, gradient boosting, XGBoost, LightGBM

That "more rounds" row is the most practically important difference. Adding trees to a random forest is monotonically safe: variance keeps falling toward the correlation floor and then flattens, so "how many trees" is a compute decision, not a tuning decision. Adding rounds to a boosted model is not safe — validation error falls, bottoms out, then climbs as the ensemble starts fitting noise, so the number of rounds is a genuine hyperparameter that must be chosen by early stopping on a validation fold. When ensembles get large enough that a single machine can't hold them, or when the base learner is itself a neural network, the coordination problem becomes its own subject — see distributed training.

→ The failure mode

Boosting with no early stopping, on noisy labels. Because every round targets the current residuals, a boosted model will happily allocate its last few hundred trees to memorizing mislabeled rows — the exact examples it should be ignoring. The symptom is a training curve that keeps improving while validation error has been rising for two hundred rounds. Always pass a validation set and an early-stopping patience; and if your labels are known-noisy, prefer bagging, which averages mislabeled examples away instead of hunting them down.

▶ Watch it explained

Bagging vs Boosting: A Jury, or a Relay?

07

Discriminative vs generative

A critic, or an artist?

Every classifier learns from the same labeled data, but it can learn two fundamentally different things from it. Show a model a thousand labeled photos of cats and dogs, and it can either learn just enough to tell them apart, or it can learn what a cat actually looks like and what a dog actually looks like. The first models the boundary between classes. The second models the classes themselves. That choice determines not just how accurate the model is, but what it's even capable of.

Discriminative: learn the boundary

A discriminative model answers one narrow question directly: given this input, which class is it? Formally it learns P(y | x) and models that decision boundary as directly as possible, without representing anything else about the data.

The narrowness is a strength. Logistic regression, SVMs, and most neural-network classifiers are discriminative, and because they pour all their capacity into exactly where the classes differ, they're usually the sharper pure classifiers. A discriminative cat-vs-dog model never learns what fur texture looks like in general — only which side of the line an image falls on. It's a critic: excellent at telling two things apart, with no ability to describe or produce either.

Generative: learn the distribution

A generative model takes on a harder job: learning P(x | y), the distribution the inputs themselves come from, per class — what the space of "cat" images actually looks like, and separately what "dog" looks like. To classify, it doesn't look for a boundary at all; it asks which distribution the input more plausibly came from, via Bayes' rule:

P(y | x) = P(x | y) · P(y) / P(x)   →   argmaxy P(y | x) = argmaxy P(x | y) · P(y)

The denominator doesn't depend on y, so it drops out. A generative classifier only needs the class-conditional likelihood and the prior.

A concrete pass: a Naive Bayes spam filter sees the word "invoice." Suppose P(invoice | spam) = 0.30, P(invoice | ham) = 0.02, and the prior is P(spam) = 0.04 from section 04's dataset. Then the spam score is 0.30 × 0.04 = 0.012 and the ham score is 0.02 × 0.96 = 0.019 — so despite "invoice" being 15× more likely in spam, a single occurrence still isn't enough to overcome a 24:1 prior against. Multiply in a second suspicious word at the same 15:1 ratio and spam wins decisively. That's the whole mechanic, and it's why priors matter so much on imbalanced data.

Naive Bayes and Gaussian mixture models are the classic generative classifiers. But the reason this distinction matters in 2026 is the other branch: because a generative model has modeled what each class actually looks like, it can also produce a new example of one. That's the core move behind GANs, diffusion models, and large language models — each learns the distribution of its training data, which is exactly what lets it generate an image or a sentence that never existed.

Why generative models can create and discriminative ones can't

The gap comes directly from what each bothers to learn. A discriminative model only represents the boundary, so there's nothing inside it to sample from — it has no notion of a "typical" member of a class, only which side of a line an input falls on. A generative model has already built a representation of the full distribution, so drawing a new sample is a well-defined operation: pick a class, then generate a plausible instance.

That distributional knowledge pays off twice more. It can flag an input as an outlier when it looks improbable under every distribution it knows — something a discriminative model has no basis to judge, since it never modeled what "normal" is. And because it extracts richer structure from each example, it often gets more out of a small labeled dataset. (An LLM is the extreme case: it models P(x) over text with no labels at all, and the classification ability falls out as a side effect.)

The trade-off is real. Modeling an entire distribution is a harder problem than modeling one boundary, and that extra effort doesn't always convert into classification accuracy — for a straightforward classification task, a discriminative model often wins precisely because it spends no capacity on parts of the problem the task doesn't need.

DiscriminativeGenerative
LearnsP(y | x) — the boundary between classesP(x | y) or P(x) — the distribution itself
Can generate?No — there is nothing to sample fromYes — sample new, realistic instances
Outlier detection?No — never modeled what normal looks likeYes — low likelihood under every class
Data efficiencyNeeds more labels, uses them efficientlyExtracts more per example; can use unlabeled data
Pure accuracyUsually higher for a fixed labeled datasetUsually lower — capacity spent elsewhere
ExamplesLogistic regression, SVM, most NN classifiers, cross-encodersNaive Bayes, GMM, GANs, diffusion, LLMs

The practical rule: if the job is purely to tell classes apart, a discriminative model is usually the better tool — cheaper to train and typically sharper, because every bit of capacity goes toward the one decision that matters. If the job requires you to generate, detect the unseen, or work with few labels, you need a model that has learned what the data looks like, not just where its classes end. That is also the answer to why modern generative AI is called generative: a GAN, a diffusion model, and an LLM are all, by definition, modeling the distribution of their training data — and that modeling is precisely what lets them create something new rather than merely judge something given.

→ The failure mode

Reaching for an LLM as a classifier by default. A generatively-trained model prompted to output "spam" or "not spam" is a generative model doing a discriminative job: it will be poorly calibrated (its stated 90% confidence is not a 90% frequency), sensitive to prompt phrasing, expensive per call, and beatable by a logistic regression on TF-IDF features that trains in two seconds. Use the LLM where the distribution knowledge earns its cost — zero labeled examples, open-ended categories, or a task that needs generated output — and fine-tune a small discriminative model the moment you have a few thousand labels and a stable label set.

▶ Watch it explained

Discriminative vs Generative Models: A Critic, or an Artist?

Frequently asked

Quick answers

Supervised vs unsupervised vs reinforcement?

Supervised learns input → answer from labeled examples. Unsupervised finds structure in raw unlabeled data (clusters, compressed representations, anomalies). Reinforcement has an agent act in an environment and learn a policy from reward. The deciding question is what your problem gives you — labels, raw data, or a reward signal — not which method sounds best.

Is logistic regression classification or regression?

Classification, despite the name. It regresses onto a probability between 0 and 1, then thresholds that probability into a label. The rule: a fixed set of kinds with no meaningful distance between them means classify; a quantity on a continuum where being off by a little beats being off by a lot means regress.

What is the bias-variance tradeoff?

Bias is average distance from the truth (underfitting — bad on train and test). Variance is instability across training samples (overfitting — great on train, bad on test). Expected test error is bias² + variance + irreducible noise. Complexity trades one for the other, so the target is the minimum of the sum, not zero of either.

Precision or recall — which should I optimize?

Whichever mistake costs more. Precision = TP/(TP+FP), a false-alarm score; recall = TP/(TP+FN), a miss score. Cancer screening wants recall; a delete-forever spam filter wants precision. When both matter, use F1 (the harmonic mean, which punishes lopsidedness) or Fβ to weight one side deliberately.

Why not just use one train/test split?

Because the score depends on which rows happened to be held out, and those rows never train the model. K-fold rotates the test set across k folds and averages, so every row is used for both and lucky splits cancel. You also get the spread across folds, which tells you how much to trust the mean. Cost: roughly k× the compute.

Bagging or boosting?

Bagging (random forest) trains models independently on bootstrap samples and averages — reduces variance, parallelizes, safe to add trees. Boosting (XGBoost, LightGBM) trains sequentially on the ensemble's residuals — reduces bias, usually more accurate on tabular data, but sequential and will overfit without early stopping. Diagnose bias vs variance first, then pick.

What makes a model generative?

It models the distribution of the data itself — P(x|y) or P(x) — rather than only the boundary P(y|x). That distributional knowledge is what lets it sample new instances, score how likely an input is, and flag outliers. GANs, diffusion models, and LLMs are generative in exactly this sense; logistic regression and most classifiers are discriminative.

Finished this one? 0 / 208 Handbooks done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.