Module lessons (2/4)
Types of learning problems
The hook: one question decides everything
Before choosing any algorithm, ask: do I have the answer for my past examples?
- If every example comes with the answer you want to predict, you can learn from those answers: supervised learning.
- If you only have raw data and want to discover its structure, you are in unsupervised learning.
- If an agent learns by acting and receiving rewards, it is reinforcement learning.
Supervised, unsupervised and reinforcement compared
| Learning paradigm | Past answer (label)? | Typical output | Examples |
|---|---|---|---|
| Supervised classification | Yes (category) | Discrete class or probability | Fraud detection, image tag |
| Supervised regression | Yes (number) | Continuous quantity | House price, travel time |
| Unsupervised learning | No labels | Cluster IDs or low-dim axes | Customer segments, anomaly score |
| Reinforcement learning | Delayed reward signal | Policy of actions | Game playing, robotics control |
Supervised learning
The examples are pairs: an input and the correct output (the label). The model learns the mapping from inputs to outputs, then predicts outputs for new inputs. There are two flavours:
- Classification: the output is a category (spam or not spam, which digit, which species).
- Regression: the output is a number (a price, a temperature, a delivery time).
A quick test: if it makes sense to ask "how far off was the prediction?", it is regression. If the answer is simply right or wrong (or a probability per category), it is classification.
Unsupervised and reinforcement learning
In unsupervised learning, there are no labels. The model looks for structure by itself: groups of similar customers (clustering), the main directions of variation (dimensionality reduction), unusual items (anomaly detection). Because there is no answer key, judging the result takes more care.
In reinforcement learning, an agent takes actions in an environment and receives rewards. It learns a strategy that maximises the total reward over time, like a program learning to play a game. It needs an interactive environment, so it sits outside the tabular scope of this course.
Seeing the difference in Python
In classification, target values are discrete class labels, and we evaluate exact agreement:
import numpy as np
y_true = np.array([0, 1, 1, 0])
y_pred = np.array([0, 1, 0, 0])
print("classification accuracy:", float((y_true == y_pred).mean())) # 0.75In regression, predictions are continuous floats, so we measure how far off predictions sit on average:
y_price = np.array([150.0, 260.0, 410.0])
y_hat = np.array([160.0, 250.0, 390.0])
mae = float(np.abs(y_price - y_hat).mean())
print("mean absolute error:", round(mae, 2)) # 13.33Turning the same continuous column into a threshold flag switches a regression dataset into classification:
prices = np.array([150, 260, 410, 190])
is_expensive = (prices >= 250).astype(int)
print(is_expensive.tolist()) # [0, 1, 1, 0]And in unsupervised clustering, we pass only X (y does not exist) and inspect centroids or nearest distances:
X_points = np.array([[1.0, 1.0], [1.2, 0.9], [8.0, 8.2]])
centroid_a = np.array([1.1, 0.95])
dists = np.linalg.norm(X_points - centroid_a, axis=1)
print([round(float(d), 2) for d in dists]) # [0.11, 0.11, 10.01]Try it: sort the problems
Practice: targets and errors by task type
Warmup — Binary label conversion
Turn delivery_minutes into a binary classification label vector y_late (1 where minutes > 30, else 0) as plain ints.
Show hint
Use (delivery_minutes > 30).astype(int) so 30 itself stays 0.
Solution available after 3 attempts
Practice — Numeric error vs. exact class match
Compute the Mean Absolute Error between true numeric temperatures y_true and predictions y_pred. Store it as a float in mae.
Show hint
np.abs(y_true - y_pred).mean() averages the absolute error magnitude.
Solution available after 3 attempts
Challenge — Nearest cluster without labels
For each 2-D point in X, assign cluster label 0 if it is closer to center_0, else 1. Store the integer cluster assignments in clusters.
Show hint
Compute distances d0 and d1 using np.linalg.norm(..., axis=1), then check (d1 < d0).astype(int).
Solution available after 3 attempts
Check your understanding
You have 10,000 past loan applications, each marked 'repaid' or 'defaulted'. You want to predict the outcome for new applications. What is this?
You have customer purchase histories and no labels, and you want to find groups of similar customers. What do you use?
Which of these is a regression problem?
Why is reinforcement learning not a good fit for predicting house prices from a table of past sales?