EVERYTHING AIAI engineering, made visual
0/18 complete
LESSON 01 · MACHINE LEARNING · LEARN

Show it examples.
It finds the rules.

Machine learning flips programming: instead of writing the logic by hand, you hand an algorithm labelled examples, and it returns a model — rules encoded as numbers that generalise to data it has never seen.

45 MIN · 7 CHAPTERSPREREQ · PHASE 1
FIG. 01 / LABELS IN, BOUNDARY OUT
TRAIN — TEST — class 0 class 1 correct missed
LESSON 01TYPE · LEARN~45 MINPREREQ · PHASE 1ORIGINAL LESSON ↗
THE 60-SECOND VERSIONThen find the rules ↓
01 / RULES OR EXAMPLES

Programming writes rules. ML finds them.

Traditional code takes rules plus data and produces output. Machine learning takes data plus expected outputs and produces the rules — a model whose parameters were tuned by the examples themselves.

rules + data → output ⇄ data + answers → model
02 / FEATURES AND LABELS

Rows are examples. Columns split into inputs and answers.

Each row is one example. The feature columns are what the model may look at. The label column is the answer it learns to predict — and it must never appear among the inputs.

X (features) → y (label)
03 / SPLIT BEFORE YOU FIT

Measure on data the model never saw.

Hold out part of the data before training starts: train for learning, validation for tuning, test for the final honest score. A model scored on its training data is being graded on the answers it memorised.

train 70% · validation 15% · test 15%
MENTAL MODEL IN ONE SENTENCE

Machine learning is programming by example: you provide rows of features and their answers, the algorithm finds the rules, and generalisation is whether those rules still work on rows it has never seen.

By the end you will classify a problem as supervised, unsupervised or reinforcement; point at the features and the label in a table; split honestly; walk the eight-step workflow; recognise underfitting and overfitting; read accuracy without being fooled by it; and know when the right answer is to skip ML entirely.

RULES OR EXAMPLES

Programming writes the rules.
Learning finds them.

A spam filter written by hand is a pile of if-statements that breaks the moment spammers change wording. A model trained on labelled emails learns the pattern instead — and can be retrained when the world changes.

Traditional programming and machine learning solve problems in opposite directions. In the traditional route you write the logic yourself: if the subject contains “FREE MONEY”, mark it spam. In the learning route you supply examples — emails with their known labels — and the algorithm discovers the rules. What comes out of training is the model: the rules, stored as numbers (called parameters or weights) that map inputs to outputs.

The hard part is not fitting the examples you show it. The hard part is generalising: making good predictions on rows the model has never seen. That single idea — measure on unseen data — is why the rest of this lesson spends so much time on splitting data and on scores that can be trusted.

TRADITIONAL PROGRAMMINGRulesDataProgramOutputapplies the rulesMACHINE LEARNINGDataAnswersLearning algorithmModel = rules
Traditional programming consumes rules and data and emits output. Machine learning consumes data and the expected answers and emits the rules — encoded as numbers that later make predictions on new data.

The smallest complete example is the nearest-centroid classifier. Training computes the average of each class; prediction asks which average is closest. No gradient descent, no hyperparameters, no iteration — yet it has the same three steps every learning algorithm follows: learn a representation, predict from it, evaluate against the truth.

The whole learner — Pythonpython
import numpy as np

class NearestCentroid:
    """The smallest possible "learning" algorithm: one mean per class."""

    def fit(self, X, y):
        self.classes = np.unique(y)
        self.centroids = np.array([
            X[y == c].mean(axis=0) for c in self.classes   # learn: two means
        ])

    def predict(self, X):
        distances = np.array([
            np.sqrt(((X - c) ** 2).sum(axis=1))
            for c in self.centroids                        # compare distances
        ])
        return self.classes[distances.argmin(axis=0)]      # predict: closest mean

    def score(self, X, y):
        return np.mean(self.predict(X) == y)               # evaluate: % correct

# fit = compute means, predict = nearest mean, score = fraction correct.
# Every algorithm you meet later keeps this three-step shape.
fit, predict, score: three lines of real work. Later lessons replace the means with more complex representations; the workflow does not change.
Worked check: nearest centroid, by hand

Here is a one-dimensional spam filter. The single feature is the overlap score with a spam-word list; the label is spam (1) or legitimate (0).

training set (feature score → label) 1.0 → legit 7.0 → spam 2.0 → legit 8.0 → spam 3.0 → legit 9.0 → spam 4.2 → spam (the sneaky one) learn: one mean per class legit mean = (1 + 2 + 3) / 3 = 6 / 3 = 2.00 spam mean = (7 + 8 + 9 + 4.2) / 4 = 28.2 / 4 = 7.05 predict: closest mean wins boundary = (2.00 + 7.05) / 2 = 4.525 1, 2, 3 → legit ✓ 7, 8, 9 → spam ✓ 4.2 → left of 4.525 → predicted legit ✗ evaluate: 6 correct out of 7 examples → 6/7 ≈ 85.7% training accuracy A new email with score 4.5 lands at 4.5 < 4.525 → predicted legitimate. The model has no idea that 4.2 and 4.5 sit in the overlap zone; it only knows which mean is nearer. That limit is the point.

Notice what the algorithm never did: nobody wrote a rule such as “score above 4.5 means spam”. The 4.525 boundary fell out of the two means. Change the examples and the boundary moves.

FEATURES & LABELS

Rows are examples.
Columns are inputs — except one.

A training table has a rigid anatomy: each row is one example, each feature column is something measurable, and exactly one column holds the answer the model must learn to predict.

The features (often written X) are the columns the model is allowed to look at. The label (written y) is the answer column. When the label is a category — spam/ham, cat/dog, benign/malignant — the task is classification. When it is a number — a price, a temperature, a demand — the task is regression. Both are supervised: both train on examples whose answers are known.

Models do not read text or pictures; they read numbers. A categorical feature such as sender_in_contacts = yes/no becomes 1/0 before training, and a raw email becomes counts or overlaps. This translation is feature engineering, and the same translation must run again at prediction time — production does not know anything the training table did not encode.

A table with 100 emails and 3 features X is 100 × 3 : 100 rows (examples) × 3 columns (features) y is 100 : one label per row an 80/20 split gives train: 80 rows → 80 × 3 = 240 feature values + 80 labels test: 20 rows → 20 × 3 = 60 feature values + 20 labels Nothing else exists. If a signal is not a column, the model cannot see it; if the answer is a column, remember to remove it before fitting.

Anatomy of a training table

Every row is one example. Click each column header to cycle its role — input feature or the answer column — then check yourself. Exactly one column is the label.

meeting notes0.900yesham
WIN FREE MONEY NOW!!!0.105nospam
invoice attached0.401yesham
URGENT: claim your prize0.204nospam
lunch tomorrow?0.700yesham
exclusive offer 4 u0.153nospam
three inputs, one answer: which column is the label?

If is_spam slipped in as a feature, the model would score perfectly in training and be useless in production, because at prediction time nobody has told it the answer yet. The label is what training is trying to predict.

Supervised taskClassificationRegression
OutputDiscrete categoriesContinuous number
Question it answersWhich category?How much?
ExampleIs this email spam?What will this house sell for?
Output space{spam, not spam}Any real number
Typical lossCross-entropyMean squared error
What training drawsBoundaries between classesA curve through the data
Quick check

Which of these is a regression task?

THREE WAYS TO LEARN

Answers, structure,
or rewards.

The clearest question to ask about any ML problem is what the data gives you: known answers, inputs alone, or a score that arrives after you act.

Supervised learning has labelled examples: photos with cat/dog tags, houses with sale prices. The model learns the mapping, and the label’s type decides the task — categories make it classification, numbers make it regression. Unsupervised learning has inputs only. With no answer column, the model can only look for structure: groups of similar customers (clustering), or a compressed version of 1,000 columns that keeps the important variation (dimensionality reduction). Reinforcement learning has neither a dataset nor labels — an agent acts, receives rewards and penalties, and adjusts its strategy to earn more reward over time. Game-playing, robot control and RLHF for language models all live here.

Most day-to-day work is supervised, unsupervised is the standard tool for exploration and preprocessing, and reinforcement learning is powerful but data-hungry and hardest to make safe. The proportions in practice are lopsided: the vast majority of production models start with a labelled table and a supervised objective.

MLSupervisedclassification — categoriesregression — numbersUnsupervisedclustering — groupsdimensionality reductionReinforcementpolicy — what to dovalue — how good states are
The three paradigms differ in what the data provides: labelled answers (supervised), inputs alone (unsupervised), or a reward signal earned by acting (reinforcement).

Sort the scenarios

Ask one question first: is there an answer column? If yes it is supervised. If not, is the model chasing structure or a reward?

10,000 photos are labelled “cat” or “dog”. Learn to tell them apart.

SCENARIO 1 OF 6
score: 0 / 0 sorted pick a paradigm →

The three buckets are not the whole story: semi-supervised learning mixes a few labels with many unlabelled rows, and self-supervised learning mints its own labels from the data.

The three buckets are clean; real projects blur them. Semi-supervised learning mixes a few labelled rows with many unlabelled ones — label propagation, pseudo-labelling and consistency training all use the unlabelled data to stretch a small label budget. Self-supervised learning goes further and creates the labels from the data itself: hide 15% of words and predict them (BERT), or predict the next word (GPT). No human labels; the supervision is manufactured from the text.

Why unlabelled data matters — the label budget a medical dataset with 100 labelled + 100,000 unlabelled images a clinician labels one image in about 2 minutes labelling all 100,000 would take 100,000 × 2 min = 200,000 min ≈ 3,333 hours ≈ 139 days of non-stop labelling (≈ 417 eight-hour working days) the 100 labelled images took 100 × 2 min = 200 min ≈ 3.3 hours semi-supervised and self-supervised methods exist to close this gap: learn structure from the cheap unlabelled rows, calibrate with the expensive labelled ones.
Quick check

A model hides 15% of the words in a sentence and learns to predict them. Which paradigm is this?

THE HONEST SPLIT

Lock the test set
before you fit anything.

A score means one of two things: how well the model memorised its examples, or how well it will handle new ones. The split is what decides which question you are answering.

Hold out part of the data before training starts. The model learns on the training set. You compare feature sets and settings on the validation set, which absorbs all your trial and error. The test set stays locked until the end, and is scored exactly once. That final number is the only honest estimate of generalisation you will get.

The order matters more than the ratio. A quick way to remember the sizes: 70/15/15 or 80/10/10. For small datasets, use k-fold cross-validation: rotate which chunk is held out, train k times, and average the k scores. Every row gets to be held out once, and the average is more stable than a single split.

SplitPurposeWhen usedTypical size
TrainingThe model learns from this dataDuring training60–80%
ValidationTune settings, compare modelsAfter each training run10–20%
TestOne final unbiased scoreOnce, at the very end10–20%

Shuffle, split, then fit

The class means are computed from the filled training points only. Watch what happens to the test score when the boundary is allowed to see the held-out points — that is what “peeking” buys you, and why it is a lie.

all data: 20 points · 10·10 (class 0 · class 1) train 14 points · 8·6 (class 0 · class 1) test 6 points · 2·4 (class 0 · class 1) model = nearest centroid (one mean per class) train accuracy = 92.9% (13/14) test accuracy = 83.3% (5/6)

In a real project the true test set is locked away. This simplified lab lets you break the rule so you can see the size of the lie. Test accuracy computed on data that influenced the fit always looks better than it is.

Numeric check: the split arithmetic, and k-fold
10,000 rows at 70 / 15 / 15 train = 10,000 × 0.70 = 7,000 val = 10,000 × 0.15 = 1,500 test = 10,000 × 0.15 = 1,500 7,000 + 1,500 + 1,500 = 10,000 ✓ (every row in exactly one pile) 5-fold cross-validation on 1,000 rows fold size = 1,000 / 5 = 200 round 1: train 800, validate 200 → score s₁ round 2: train 800, validate 200 → score s₂ … 5 rounds, each row validated exactly once reported score = (s₁ + s₂ + s₃ + s₄ + s₅) / 5 each model sees 80% of the data for training, vs 70% in a single split. The test set is still separate: cross-validation replaces the validation split, not the test set.

One more piece of arithmetic worth doing before the split: look at the class balance. If 90 of 1,000 rows are positive and you split at random, a 15% test set holds about 13.5 positives — round to 13 or 14. Stratify the split (keep the class ratio in every pile) so a small test set does not accidentally contain no positives at all.

Quick check

You try 40 different models, pick the one with the best test-set accuracy, and report that score. What went wrong?

THE WORKFLOW LOOP

Eight steps.
Mostly the boring ones.

The algorithm is one step of the job. Collecting, cleaning, splitting, evaluating and monitoring are the other seven — and they decide whether the model is useful.

Every project follows the same pipeline: collect raw data, clean and explore it, engineer features, split train/validation/test, train the model, evaluate against a trivial baseline, deploy it, and monitor it while the world drifts underneath. Evaluation sends you backward: a score that is not good enough means better features, not automatically a fancier model. Monitoring sends you all the way back to collecting fresh data.

Cleaning and exploration are not a warm-up; they are the job. The source material puts it plainly: this step often takes 60–80% of total project time. A model trained on duplicated rows, impossible values and mislabelled examples will faithfully learn the mess. No algorithm choice undoes that.

Walk the workflow

Every ML project runs this loop — the algorithms change, the steps do not. Click any step, or walk it one at a time.

STEP 1 OF 8 · THE LOOP

Collect data

Gather raw examples — labelled if you plan to learn with supervision. More data is usually better, but relevant data beats more data.

Ask: Does the data actually contain the pattern I want to predict?

Common mistake: Collecting whatever is cheap, then discovering the label is missing.

→ The arrows only point forward until evaluation or monitoring sends you back.

step 1 / 8: Collect data Gather raw examples — labelled if you plan to learn with supervision. More data is usual… the loop, not the line, is the real shape of an ML project

This is the same pipeline for a two-dollar regression and a two-million-dollar language-model run: data, cleaning, features, split, fit, evaluate, ship, watch.

Numeric check — what cleaning does to a dataset collected: 1,000 rows missing values in 12% of rows → 120 rows need repair or removal after repair, 880 usable rows an audit finds 5% of labels wrong → 0.05 × 880 = 44 mislabelled rows those 44 become 44 wrong answers no model can out-learn split 80/20: 704 train / 176 test if a duplicate bug had tripled the rows, train would contain each customer three times — the same 704 examples, counted three times. Data quality is measured in the same arithmetic as everything else: counts, fractions and percentages.
TOO SIMPLE, TOO COMPLEX

Miss the pattern,
or memorise the noise.

A model can fail in two opposite directions. Underfitting is not learning enough; overfitting is learning too much — including the accidents of the training rows.

Underfitting is a model too rigid to capture the real pattern: a straight line through a curved relationship. Training and held-out scores are both poor. Overfitting is a model flexible enough to fit the training rows exactly, noise included: the training score keeps climbing while the held-out score stalls or falls. Good fit is the middle — it captures the real pattern and ignores the accidents.

The classical explanation is the bias–variance trade-off. Bias is error from wrong assumptions (too simple); variance is error from sensitivity to which particular rows happened to land in the training set (too complex). Total error decomposes as bias² + variance + irreducible noise. You cannot remove the noise; you choose a complexity where the other two terms sum to their smallest.

The flexibility dial

k is how many neighbours vote. Small k bends the boundary around every point; large k flattens it. Watch the two accuracy curves and find the k where held-out accuracy peaks.

k = 1 · 18 train points · 10 test points train accuracy = 100.0% (18/18) test accuracy = 50.0% (5/10) gap = 50.0% overfitting — the training score is far above the held-out score best held-out score: 90.0% at k = 7 label noise in this dataset: 4 of 28 points sit in the wrong blob

k = 1 fits the training data perfectly — including the four mislabelled points — and pays for it on held-out data. That gap is overfitting in its purest form. Note that this simple U-shape is a classical picture; very large modern models can behave differently.

Numeric check: two ways to read the gap
Scores from three candidate models (accuracy on train / held-out) A, a single rule 72% / 70% → gap 2 pts, both low = underfitting B, a balanced model 92% / 89% → gap 3 pts, both high = good fit C, a huge model 99.8% / 73% → gap 26.8 pts = overfitting The diagnosis is the pattern of the two numbers, not either one alone: low train, low test → underfit (add capacity or features) high train, high test → good fit high train, much lower test → overfit (more data, less capacity, regularisation, or early stopping) Bias–variance budget, same units (squared error) Model A: bias² 0.25 + variance 0.01 + noise 0.01 = 0.27 Model B: bias² 0.04 + variance 0.04 + noise 0.01 = 0.09 ← best total Model C: bias² 0.01 + variance 0.30 + noise 0.01 = 0.32 Making the model more flexible trades bias down and variance up; the winner is where the sum is smallest, not where either term is smallest.

Signs of overfitting in a real run: training accuracy far above validation accuracy, the validation curve turning upward while training loss keeps falling, and performance improving noticeably when you add data. Signs of underfitting: both curves are flat and high, and more data barely helps — the model simply cannot represent the pattern.

Quick check

A model scores 58% on training data and 56% on held-out data. What is the diagnosis?

ACCURACY & WHEN NOT

One number can lie.
Sometimes the answer is no model.

Accuracy is the first metric everyone reaches for, and the easiest to be fooled by. And before any metric matters, one question comes first: should this be machine learning at all?

Accuracy is the fraction of predictions that are correct: correct predictions divided by total predictions. If a model labels 90 of 100 emails correctly, its accuracy is 90/100 = 0.9 = 90%. That sentence is the whole formula. The problem is what it hides: a dataset where 99% of rows are one class lets a model score 99% by saying that class every time.

The fix is to count the four possible outcomes separately. For a rare positive class — fraud, disease, a failing machine — write a confusion matrix: true positives (caught), false positives (false alarms), false negatives (missed), true negatives (correctly cleared). Then read the metrics that name your real concern. Recall is the fraction of real positives the model caught; precision is the fraction of alarms that were real. When missing a positive is expensive, recall matters most; when false alarms are expensive, precision does. Always compare against a trivial baseline — the majority class, or random guessing — before celebrating any score.

The 99% model that does nothing

Fraud is rare: 10 of these 1,000 transactions. Compare a model that always says “legitimate” with one that actually catches most fraud — and watch which one wins on accuracy.

actual fraud actual legit predicted fraud TP 0 FP 0 predicted legit FN 10 TN 990 accuracy = (TP+TN)/1000 = 990/1000 = 99.0% recall = TP/(TP+FN) = 0/10 = 0.0% precision = TP/(TP+FP) = 0/0 = undefined (no alarms raised) 99% accuracy and it catches zero fraud. This is why a rare class needs more than accuracy.

The cost of the two mistakes is not equal: a missed fraud can cost thousands, a false alarm costs a phone call. When classes are rare or mistakes are asymmetric, report recall and precision, not just accuracy.

Numeric check: the same confusion matrix, three ways
1,000 transactions, 10 fraudulent. The detector's matrix: caught 8 frauds → TP = 8 missed 2 frauds → FN = 2 flagged 15 innocents → FP = 15 left 975 innocents alone → TN = 975 accuracy = (TP + TN) / total = (8 + 975) / 1,000 = 983/1000 = 98.3% recall = TP / (TP + FN) = 8 / (8 + 2) = 8/10 = 80.0% precision = TP / (TP + FP) = 8 / (8 + 15) = 8/23 ≈ 34.8% compare with “always legitimate”: TP = 0, FN = 10, FP = 0, TN = 990 accuracy = 990/1,000 = 99.0% ← higher! recall = 0/10 = 0% ← catches nothing precision = undefined (no alarms were ever raised) The useless model wins on accuracy and loses on the only metric that matters here. A 0.7-point accuracy drop bought 80% recall.

Different domains weight these differently: a cancer screen would rather over-refer (lower precision) than miss a case (lower recall), while a spam filter that silences real mail is worse than one that lets a few ads through. The metric is a statement about which mistake you refuse to make — choose it deliberately.

When not to use machine learning

ML is a tool with a cost: data collection, cleaning, training, monitoring, and a stream of probabilistic answers that are sometimes wrong. Before reaching for a model, check whether the problem actually wants one.

  • The rules are simple and well-defined. Taxes, unit conversions, sorting: write the logic in code. A model adds error and maintenance for no benefit.
  • You have no data, or very little. Ten examples cannot train anything meaningful. Collect data first, then decide.
  • Being wrong is catastrophic and correctness is guaranteed. Dosage calculations, reactor control, cryptography: use deterministic methods when “usually right” is not good enough.
  • A lookup table or heuristic covers the cases. If a simple threshold handles 99% of situations, a model mostly adds failure modes.
  • Every decision must be explainable. Some regulated decisions require a full reason. Prefer interpretable models (a linear formula, a small tree) or stay deterministic.
  • The problem changes faster than you can retrain. If the rules shift daily and retraining takes a week, the model is always stale — rules or a fast retraining loop are the better investment.

The decision order that follows from this: Do I have data? Can I write the rules? Is being sometimes wrong acceptable? Do I need full explainability? Do I have enough labels? — and only then, which algorithm. Saying “this should be a rule, not a model” is a senior answer, not a failure.

CHECK YOURSELF

Five questions.
Then the terms worth keeping.

Answer before you look. The split, overfitting and labelling questions are exactly the ones that decide whether a real project succeeds.

0 / 5 answered · 0 correct

01In supervised learning, what does the model receive during training?

02What is the purpose of splitting data into training and test sets?

03A model gets 98% accuracy on training data but 55% on test data. What is this an example of?

04An e-commerce site wants to group customers into segments based on purchase behaviour, with no predefined labels. Which type of ML is this?

05Which scenario is NOT a good use case for machine learning?

Key terms, demystified

Click a card to swap the lazy description for what it actually means.

Exercises from the lesson

Four problems with fully worked answers — the nearest-centroid exercise has every number you need to reproduce it by hand.

  1. Take any dataset (Iris, Titanic, or one of your own). Split it 70/15/15 into train, validation and test. Explain in two sentences why you should not tune hyperparameters on the test set.
    Show one worked answer

    Concretely: of 1,000 rows, 700 become train, 150 validation and 150 test. Train the model on the 700; compare feature sets and settings on the 150 validation rows; only when you have stopped changing anything, score once on the 150 test rows. You must not tune on the test set because every comparison you make against it leaks information back into your choices — after a hundred tweaks, the test set has effectively become training data, and the reported score measures how well you fitted the test rows, not how well the model will generalise. The validation set exists to absorb that trial and error; the test set is the one honest exam you take at the end. If 150 rows feels too small to trust, use k-fold cross-validation on the train+validation portion and keep the test set untouched.

  2. List three real-world problems. For each one, identify whether it is classification, regression or clustering, and whether it is supervised or unsupervised.
    Show one worked answer

    (1) Predicting next quarter's sales from past months is regression: the output is a number, and the past is a labelled dataset because the true values are recorded. (2) Grouping grocery baskets into shopping segments without any predefined segment names is clustering — unsupervised, because there is no answer column; you only have the baskets. (3) A warehouse robot learning to pick items, rewarded +1 per success and −0.01 per second, is reinforcement learning: there are no labelled examples, only actions and rewards. Two follow-up habits worth building: name the metric before naming the algorithm (mean absolute error for sales, a within-cluster distance for segments, successful picks per hour for the robot), and write down a trivial baseline (last quarter's sales, one giant cluster, a random policy) so the learned model has something to beat.

  3. A model gets 99% accuracy on training data but 60% on test data. Diagnose the problem and list three things you would try to fix it.
    Show one worked answer

    The 39-point gap — training far above held-out — is the signature of overfitting: the model has enough capacity to memorise the training rows, noise included. Three fixes: (1) Get more training data, so memorising one quirk costs accuracy on many others. (2) Reduce capacity — a simpler model (fewer features, shallower tree, smaller network) or add regularisation, which penalises large weights; early stopping is a cheap version when you can watch the validation curve. (3) Re-check the split and the features before blaming the model: is the test set from a different time period or a different distribution, and did any answer-like column leak into the features? A model that scores 60% may also be underfitting on a harder test distribution rather than overfitting; the training score alone cannot tell you which. Diagnose with a validation set, fix the data problem first, then adjust capacity.

  4. Work the nearest-centroid classifier by hand. One-dimensional spam-word overlap scores: legitimate emails 1, 2, 3; spam emails 7, 8, 9 and one sneaky spam at 4.2. Compute the two class means, the decision boundary, the training accuracy, and predict a new email with score 4.5. Would refitting on all points before scoring give an honest test number?
    Show one worked answer

    Legitimate mean = (1 + 2 + 3)/3 = 6/3 = 2.00. Spam mean = (7 + 8 + 9 + 4.2)/4 = 28.2/4 = 7.05. The nearest-centroid boundary sits halfway between the means: (2.00 + 7.05)/2 = 4.525 — left of it is legitimate, right is spam. Training predictions: 1, 2, 3 → legitimate ✓; 7, 8, 9 → spam ✓; the sneaky 4.2 lands left of 4.525, so it is predicted legitimate ✗. Training accuracy = 6/7 ≈ 85.7%. The new email at 4.5 is also left of 4.525 → predicted legitimate; if it were truly spam, that is the same mistake the boundary already makes for points in the overlap zone. Refitting on all 7 points before scoring would not change this training measure, but on a real dataset refitting on the points you are about to score makes them influence the boundary — that is leakage, and the number it produces is optimistic. Fit on train, score on test; report one honest number.

Terms this lesson borrows from later lessons (or outside)

You do not need to master these here. Each one gets a proper treatment in its own lesson; the one-line meaning is enough to keep reading. Orange dotted underlines in the prose point back to this list.

  • meanThe average of a column. The nearest-centroid classifier is nothing but one mean per class per feature. (Phase 1, Lesson 15 · Statistics for ML)
  • Euclidean distanceStraight-line distance between two points: the square root of the sum of squared coordinate differences. “Nearest” centroid means smallest Euclidean distance. (Phase 1, Lesson 14 · Norms and Distances)
  • probability distributionA description of how likely each outcome is. Data collection is sampling from one; data drift is that distribution changing. (Phase 1, Lesson 06 · Probability and Distributions)
  • loss functionOne number that says how wrong the predictions are. Training is the search for parameters that make it small. (Phase 1, Lesson 04 · Calculus for ML)
  • gradient descentThe optimisation loop that nudges parameters downhill on the loss surface. It is the “adjust the parameters” step of training. (Phase 1, Lesson 08 · Optimization)
  • cross-entropyThe standard loss for classification: it punishes confident wrong answers hardest. Accuracy is what you report; cross-entropy is what you train. (Phase 1, Lesson 09 · Information Theory)
  • k-nearest neighboursClassify a point by a vote of its closest training points — the flexibility lab's model, explored in full later. (Phase 2, Lesson 06 · KNN and Distances)
  • model evaluationThe discipline of choosing the right metric and the right split. Accuracy, precision, recall and cross-validation all live there. (Phase 2, Lesson 09 · Model Evaluation)
KEEP GOING

A picture is a start.
Practice is the rest.

This lesson is a port of an open course. Everything here traces back to it — and the next step is running the code yourself.

Lesson text adapted from AI Engineering from Scratch (Phase 02, Lesson 01) and the Math Foundations Notebook reference build. The split lab, paradigm sorter, workflow stepper, flexibility playground, accuracy-trap lab, label-anatomy table, every numeric check, and the worked exercise answers are original to this page. Every lab runs in your browser.