A machine-learning model is an adjustable pattern.
Training uses examples to adjust that pattern. Evaluation asks whether the pattern still works on relevant examples it did not train on.
Lesson details
- Module
- Classical machine learning
- Lesson
- 1 · What learning from data means
- Difficulty
- Beginner
- Learning time
- 55 minutes
Before you start
- Features, targets, and leakage-safe data splits
- Averages, gradients, and basic Python loops
Skills you will gain
- Distinguish programmed rules from fitted model behaviour
- Choose supervised, unsupervised, or reinforcement learning for a problem
- Name features, targets, predictions, parameters, hyperparameters, and loss
- Trace a complete parameter-fitting loop
- Build and compare a meaningful baseline
- Explain generalisation and recognise memorisation or distribution shift
Why this matters
AI engineers rarely invent a learning algorithm from nothing. They define the problem, build evidence, select a model, fit it, evaluate it, and connect it to a reliable product.
If “learning” remains vague, it is easy to leak answers, optimise the wrong score, mistake memorisation for intelligence, or use reinforcement learning where a rule would be safer.
Plain-English mental model
Part 1 — Rules and learning solve different problems
A programmed rule states behaviour directly:
if attempts >= 5:
require_identity_check()
The developer chose the threshold and action.
A learned model has adjustable values fitted from examples:
risk = model(attempts, device_age, location_change, ...)
The developer still chooses:
- the product goal;
- which data is legitimate;
- the model family;
- the objective and constraints;
- how success is evaluated;
- what happens when the model is uncertain;
- where human review and rules remain necessary.
Learning does not remove engineering decisions. It moves some behaviour from hand-written branches into fitted parameters.
When a rule is better
Prefer a clear rule when:
- the requirement is exact and stable;
- errors have severe consequences;
- there are too few representative examples;
- the decision must be directly auditable;
- the rule already performs the job cheaply;
- policy, law, or safety requires deterministic behaviour.
A good AI system often combines rules and models.
Part 2 — Examples, features, targets, and predictions
One learning example contains information about one case.
A featureFeatureAn input value presented to a model for one example, such as message length, account age, or a text embedding. A feature must be available at the real prediction time and calculated consistently. is an input value presented to the model.
A targetTargetThe answer a supervised model is trained to predict, such as a category, probability, or numerical value. Targets are also called labels in many classification tasks. is the desired answer in supervised learning.
For ticket urgency:
| Element | Example |
|---|---|
| raw case | one support ticket |
| features | message representation, customer tier, hour |
| target | urgent or not urgent |
| prediction | estimated urgency class or probability |
A label usually means a target category. Some teams use “label” for any target.
Features must exist at prediction time. The target must be defined consistently and represent the real goal closely enough to be useful.
If “urgent” means “an agent opened it within ten minutes”, the target may reflect staffing and queue position, not genuine urgency. Labels are measurements created by a process, not perfect truth.
Part 3 — Three kinds of learning feedback
The paradigms differ mainly in what feedback the learner receives.
Supervised learning
Supervised learningSupervised learningLearning a mapping from input features to known target answers using labelled examples. Classification predicts a category, while regression predicts a numerical value. uses examples paired with target answers.
Two common tasks:
- classification: predict a category, such as spam/not spam;
- regression: predict a number, such as resolution minutes.
It does not mean a human watches every training step. “Supervision” is the target signal in the training data.
Use supervised learning when historical examples contain a target that corresponds to the future prediction task.
Unsupervised learning
Unsupervised learningUnsupervised learningFinding structure in examples that do not include a desired target answer, such as groups, compressed representations, or unusual cases. The discovered structure still needs human interpretation and evaluation. receives examples without desired target answers.
It can search for:
- clusters of similar examples;
- lower-dimensional representations;
- density and unusual points;
- latent structure useful to another task.
A clustering algorithm may create three groups. It does not automatically know that the groups mean “billing”, “technical”, and “account”. A human or downstream evaluation supplies meaning.
“No labels” does not mean “no objective”. Algorithms still optimise something, such as compact clusters or reconstruction error.
Reinforcement learning
Reinforcement learningReinforcement learningLearning a policy for choosing actions through interaction with an environment and reward feedback, usually aiming to maximise expected cumulative reward rather than predict one fixed label., shortened to RL, learns action choices from interaction and reward.
Core terms:
- agent: the decision maker;
- environment: the world it interacts with;
- observation or state: information about the current situation;
- action: a choice the agent makes;
- reward: numerical feedback;
- policy: a rule or model mapping situations to actions;
- return: cumulative future reward.
Unlike ordinary supervised learning, a correct action label is not supplied for every situation. Actions change what happens next, and rewards can be delayed.
Exploration may affect real users. Train in simulation, constrain actions, use offline evidence, and require safety review where mistakes matter.
Not every AI problem belongs to only one box
A recommendation system might:
- learn item representations without labels;
- train a supervised click predictor;
- optimise a sequence of recommendations with reward feedback;
- apply deterministic safety and eligibility rules.
Name the feedback at each stage instead of labelling the whole product with one paradigm.
Part 4 — A model is a function with adjustable parameters
A model maps inputs to predictions.
For one feature:
prediction = weight × feature + bias
The weight and bias are model parametersModel parameterAn adjustable value learned from training data, such as a linear-model weight or a neural-network connection weight. Parameters change during fitting.. Training changes them.
A hyperparameterHyperparameterA setting chosen outside the parameter-fitting loop, such as learning rate, tree depth, or regularisation strength. It is normally selected with training and validation evidence, not the final test set. is chosen outside that parameter-fitting calculation.
Examples:
| Parameters learned during fit | Hyperparameters chosen around fit |
|---|---|
| linear weights | learning rate |
| tree split thresholds | maximum tree depth |
| neural network weights | number of layers |
| cluster centres | number of clusters, in basic k-means |
Hyperparameters are still informed by data, usually through validation. Do not choose them by repeatedly consulting the test set.
Model family and representation limit what can be learned
A straight line cannot represent every curve.
Features also shape the available pattern. If a model sees only message length, it cannot directly use message meaning.
Training finds parameters inside the chosen model family and representation. It does not search every imaginable program.
Part 5 — Loss turns prediction quality into a training signal
A loss functionLoss functionA numerical score describing how costly a model's prediction is relative to the training target. Fitting adjusts parameters to reduce an aggregate training loss, but lower training loss does not guarantee better real-world behaviour. assigns a numerical cost to a prediction relative to a target.
For numerical prediction, squared error is:
loss = (prediction - target)²
If the target is 10:
| Prediction | Squared error |
|---|---|
| 9 | 1 |
| 7 | 9 |
| 2 | 64 |
The square makes larger errors cost much more.
For classification, cross-entropy commonly scores predicted probabilities. Earlier you saw:
loss = -log(probability assigned to the correct class)
The training objective usually aggregates loss across a batch plus any regularisation.
Loss is a proxy, not the whole product
Lower loss does not automatically mean:
- fair decisions;
- useful user outcomes;
- safe behaviour;
- calibrated confidence;
- low latency or cost;
- correct behaviour under distribution shift.
Choose loss because it supports the product goal, then evaluate broader requirements separately.
Part 6 — The fitting loop
Fitting or training changes model parameters to reduce an objective on training examples.
For gradient-based fitting:
- Read a batch of training features and targets.
- Run the model to produce predictions. This is the forward pass.
- Calculate loss.
- Calculate how loss changes with each parameter: the gradient.
- Use an optimiser to update parameters.
- Repeat over batches.
One complete pass through the training dataset is an epoch.
A small fitting loop in Python
The next lesson derives linear regression carefully. Here, focus on the loop.
Expected output:
0 44.428
9 0.059
99 0.036
399 0.026
weight: 1.982
bias: 0.1
prediction for 6 hours: 11.99
Important lines
predictions = [weight * x + bias ...]- The forward pass uses current parameters to predict every training target.
errors = prediction - target- Each signed error records how far and in which direction the prediction missed.
weight_gradient / bias_gradient- The gradients summarise how mean squared error would change if each parameter moved slightly.
parameter -= learning_rate * gradient- Gradient descent moves parameters in the direction expected to reduce loss. The learning rate controls step size.
weight * 6 + bias- After fitting, inference applies frozen parameters to an unseen input. Extrapolating beyond the observed range still requires caution.
This demonstration uses all five rows as training examples. A real evaluation must reserve unseen evidence.
Part 7 — Start with a baseline
A baseline modelBaseline modelA simple reference method that a more complex approach must beat to justify its added cost and risk, such as predicting the training mean or most frequent class. is a simple reference.
Examples:
- classification: always predict the most frequent training class;
- regression: always predict the training mean or median;
- forecasting: repeat the latest observed value;
- recommendation: show popular eligible items;
- language task: use a clear rule or existing retrieval method.
Why:
- proves the pipeline and metric can run end to end;
- reveals whether the task has useful signal;
- catches label or metric bugs;
- defines the minimum improvement needed;
- makes complexity earn its place.
from statistics import median
training_targets = [12, 14, 16, 100]
baseline = median(training_targets)
validation_targets = [13, 18, 40]
absolute_errors = [
abs(baseline - target)
for target in validation_targets
]
print("baseline:", baseline)
print("mean absolute error:", sum(absolute_errors) / len(absolute_errors))Compare every candidate on the same validation cases and metric.
Part 8 — Training, validation, and test answer different questions
From the data-foundations lesson:
- training: what parameters fit the examples?
- validation: which model, features, hyperparameters, and threshold should we choose?
- test: after choices are frozen, how well does the selected system perform on untouched evidence?
The validation set indirectly influences the selected system. Repeated validation tuning can overfit validation too.
If experimentation is extensive:
- use cross-validation where appropriate;
- keep a final untouched test set;
- log experiments;
- limit manual metric hunting;
- obtain fresh future evidence after launch.
Do not merge the test set into training and continue reporting the old test score as independent evidence.
Part 9 — Generalisation is the real goal
GeneralisationGeneralisationA model's ability to perform usefully on relevant unseen cases drawn from the conditions it is intended to face, rather than merely remembering its training examples. means performing usefully on relevant unseen cases.
It is not merely “high test accuracy”. The test set must represent the intended use.
Memorisation versus useful pattern
Consider an image classifier:
- memorisation: recognises exact training image pixels;
- generalisation: recognises new relevant images under expected conditions.
Memorisation is not always useless. Retrieval systems deliberately remember records. The problem is claiming a model learned a reusable relationship when it only stored examples or shortcuts.
Why training performance can mislead
A highly flexible model can fit:
- real signal;
- random noise;
- duplicated examples;
- annotation quirks;
- watermarks, filenames, or source IDs;
- future information accidentally present.
Training loss usually improves as capacity and fitting time increase. Generalisation may improve, flatten, then worsen.
Distribution assumptions
Evaluation often assumes future cases are related to development data.
Independent and identically distributed, shortened to IID, is an idealisation: examples are independent draws from one fixed distribution.
Real systems violate it:
- users repeat;
- time changes behaviour;
- product policy changes labels;
- one model changes the data collected next;
- new languages, devices, or attacks appear.
Use group and time splits, stress tests, slice evaluation, and production monitoring to model those realities.
Distribution shift
Distribution shift means deployment data differs from training data.
Examples:
- new customer region;
- seasonal ticket topics;
- changed form fields;
- model-generated text entering the pipeline;
- a new fraud strategy;
- delayed or redefined targets.
A model can generalise within its test distribution and still fail after a shift. State the evaluation boundary honestly.
Part 10 — Overfitting and underfitting, first look
Underfitting means the model or training process cannot capture enough useful pattern.
Typical signal:
training error: high
validation error: high
Overfitting means the fitted system matches training-specific detail that does not transfer.
Typical signal:
training error: very low
validation error: much higher
These patterns are clues, not proof. A broken pipeline, leakage, label noise, or distribution mismatch can create similar curves.
Later lessons cover regularisation and the bias–variance trade-off in depth.
Worked example — route urgent support tickets
Goal: rank incoming tickets so genuinely urgent cases receive faster human attention.
1. Define the decision
At ticket creation, estimate whether the case meets a documented urgent policy.
The model assists queue ordering. It does not close cases automatically.
2. Define examples
One row is one ticket at creation time.
Features may include message text representation, declared issue category, and account service tier. Resolution outcome is unavailable at this moment.
The target is an urgency label produced from reviewed policy criteria, not merely whether an agent happened to answer quickly.
3. Select the paradigm
This is supervised classification: historical tickets have input features and target classes.
Unsupervised clustering might help explore topic structure, but clusters are not urgency labels.
Reinforcement learning is unnecessary for the first system. Online exploration that delays a real urgent ticket would be unsafe.
4. Split and baseline
Split by customer and time. Keep recent tickets as test evidence.
Baseline:
predict urgent only when a documented critical phrase matches
Also compare the most-frequent-class baseline, but inspect recall for urgent tickets; accuracy alone can hide failure on a rare class.
5. Fit and choose
Fit model parameters on training tickets. Use validation data to choose representation, regularisation, and operating threshold.
Do not train on validation labels after each decision.
6. Evaluate generalisation
On untouched test data, inspect:
- missed urgent cases;
- false alerts and workload;
- performance across language, source, and issue groups;
- confidence calibration;
- latency and fallback behaviour.
7. Monitor
Track input schema, category rates, delayed reviewed labels, overrides, and incidents. A shift triggers investigation and a separately evaluated model version.
In a real AI system
Common mistakes
Calling any data-processing rule machine learning.
Fix — Ask which adjustable parameters are fitted from what experience against which objective. Exact rules remain software logic.
Treating unsupervised clusters as true categories.
Fix — Describe the optimised structure, test stability and usefulness, and have domain experts interpret it.
Using reinforcement learning for a one-step labelled prediction.
Fix — Start with supervised learning when direct targets exist. RL adds sequential credit assignment, exploration, and safety complexity.
Confusing parameters with hyperparameters.
Fix — Parameters change inside fitting; hyperparameters configure or select the fitting process using training and validation evidence.
Choosing a complex model without a baseline.
Fix — Build the smallest relevant rule, mean, median, or majority baseline first and compare on identical evaluation data.
Reporting training performance as model quality.
Fix — Use protected validation and test evidence that represents intended deployment.
Assuming one held-out score proves universal generalisation.
Fix — State the test distribution, evaluate meaningful slices and shifts, then monitor future production evidence.
Optimising a convenient target that does not represent user value.
Fix — Audit how labels are produced, document proxy limitations, and evaluate product outcomes and harms separately.
Exercises
Classify each problem as supervised, unsupervised, reinforcement learning, or not necessarily machine learning: predict ticket resolution time from completed tickets; group unlabelled tickets by language; choose a sequence of robot movements from rewards; reject messages containing an exact blocked phrase.
Reveal solution
Resolution-time prediction is supervised regression because historical rows have numerical targets. Grouping unlabelled tickets is unsupervised learning. Sequential robot control from rewards is reinforcement learning. An exact blocked-phrase check can be a programmed rule and does not require machine learning.
A classifier gets 100% accuracy on training data and 61% on a representative validation set. What does this suggest, and what should you inspect next?
Reveal solution
The large gap suggests memorisation or overfitting, though leakage, inconsistent pipelines, and a changed validation distribution are also possible. Check the split and duplicates, compare a baseline, inspect training and validation curves, reduce unnecessary complexity, add representative training evidence, and verify that both sets use the same frozen transformations.
Design a baseline for predicting support-ticket resolution time and explain how it prevents a misleading claim about a complex model.
Reveal solution
A simple baseline predicts the median resolution time from the training set for every validation ticket. Compare both methods on the same untouched validation rows with an appropriate error metric. If the complex model does not reliably improve on that baseline, its added cost, latency, and maintenance are not justified. Segment checks may reveal that an average improvement hides worse performance for important groups.
Knowledge check
Knowledge check
Practice activity
Practice · 10–20 minutes
Design the learning problem before choosing a model
Goal: Turn a vague request into a testable learning problem with a baseline and generalisation boundary.
- Choose one case: ticket urgency, resume–job match, or document topic discovery.
- State the exact decision and prediction time.
- Choose supervised, unsupervised, reinforcement, or a deterministic rule; justify the feedback.
- Define one example, features, and target or objective.
- Name one learned parameter and one hyperparameter.
- Design training, validation, and test boundaries.
- Choose a baseline and one product constraint.
- Write one sentence defining where you expect generalisation.
Expected result: A one-page problem card that another engineer can implement and challenge without guessing what ‘learn’ means.
Optional challenge: Name one shortcut or distribution shift that could create a convincing but unsafe result.
Reveal solution
Example solution for ticket urgency:
Decision:
rank a new ticket for human triage at creation time
Paradigm:
supervised classification; reviewed tickets have policy-based labels
Example:
one ticket snapshot at creation
Features:
text representation, declared category, service tier
Target:
reviewed urgent / not urgent under policy v3
Parameter:
a fitted feature weight
Hyperparameter:
regularisation strength
Split:
customer groups kept together; train on older months,
validate on the next month, test on the newest untouched month
Baseline:
documented critical-phrase rule
Constraint:
no automatic closure; uncertain high-impact cases go to a human
Generalisation boundary:
expected to cover tickets from supported languages and current
products under policy v3, not new products or unseen languages
Shortcut risk:
agent queue ID may encode historical staffing rather than urgencyThe point is not to choose the final algorithm. It is to make the learning claim precise enough to test.
Key takeaways
- Machine learning fits adjustable parameters from examples; it does not remove product, data, or safety decisions.
- Supervised learning uses target answers, unsupervised learning searches for structure, and reinforcement learning learns action policies from rewards.
- Features enter the model, parameters are fitted, hyperparameters configure fitting, and loss supplies the optimisation signal.
- A simple baseline makes complexity prove its value.
- Training fits parameters, validation guides choices, and a sealed test estimates the selected system's performance.
Flashcards
Flashcards
1 of 10Revision and interview questions
- Compare supervised, unsupervised, and reinforcement learning using the feedback each receives and one suitable application.
- Trace one complete fitting iteration from features to parameter update.
- Explain parameters, hyperparameters, loss, baseline, and prediction using one model example.
- Why can low training loss coexist with poor generalisation?
- Design evidence that tests a model under time, group, and distribution-shift constraints.
One-page sketchnote summary
Print me · one-page revision sheet
What learning from data means
Main idea
Learning fits model parameters from experience against an objective. Evidence from relevant unseen cases—not memory of training rows—supports a generalisation claim.
Core terms
- Feature — model input
- Target — desired supervised answer
- Parameter — fitted value
- Hyperparameter — fitting choice
- Loss — training cost signal
- Generalisation — works on relevant unseen cases
One picture
Code pattern
for batch in training_data:
prediction = model(batch.features)
loss = objective(prediction, batch.target)
gradients = differentiate(loss)
parameters -= learning_rate * gradientsWatch out
- Do not report training score as quality
- Do not call clusters true labels
- Do not reach for RL without a sequential need
- Do not skip the baseline
- Do not claim universal generalisation
Remember
- Name the feedback
- Define prediction time
- Fit parameters on training data
- Choose with validation
- Test once · monitor future shift
Three quick questions
- What exactly changes during fitting?
- Which evidence chose this system?
- Where should it generalise?
Lesson checklist
0 of 9 completeResources
- courseGoogle Machine Learning Crash CourseA practical introduction to models, loss, gradient descent, generalisation, and production ML.
- docsscikit-learn — supervised learningOfficial map of supervised estimators; use it after the paradigm and fitting-loop mental model.
- docsscikit-learn — unsupervised learningOfficial guide to clustering, decomposition, density estimation, and other unlabelled-data methods.
- courseReinforcement Learning: An IntroductionThe freely available Sutton and Barto textbook; later course chapters develop its concepts gradually.
- docsRules of Machine LearningProduction-focused guidance on baselines, pipelines, objectives, and launching ML systems.