Machine learning is not a single technique. It is a family of three very different ways a system can improve from experience, and choosing the wrong one for your problem is like bringing a calculator to a spelling bee. The three paradigms are supervised learning, unsupervised learning, and reinforcement learning. They differ in one thing only: what kind of feedback the machine receives while it learns. Supervised learning gets exact correct answers. Unsupervised learning gets no answers at all. Reinforcement learning gets a score saying how well it did, without being told the right move. In this lesson you will learn to tell them apart, see a tiny working example of each, and recognise which paradigm fits a task before you open a single library.
flowchart TD M["Machine Learning"] --> S["Supervised<br/>labeled examples"] M --> U["Unsupervised<br/>raw data only"] M --> R["Reinforcement<br/>rewards and penalties"] S --> SA["spam filter<br/>house price"] U --> UA["customer segments<br/>fraud patterns"] R --> RA["game AI<br/>robot control"]
Supervised learning: learning from answered questions
Supervised learning is the most common paradigm in industry. You feed the machine a long list of examples where both the input and the correct output are known — what statisticians call labeled data. The system searches for a rule that turns the inputs into the outputs, then applies that rule to new inputs it has never seen. Every spam filter, every credit-risk score, and every medical-image classifier starts here. The supervision comes from the labels: they tell the model exactly what it got wrong, so it can adjust. Without labels, supervised learning is impossible, which is why the most expensive part of many projects is simply paying people to label photographs, transcripts, or documents. The quality of the labels usually matters more than the choice of algorithm.
def predict_price(sqft, bedrooms):
return 50_000 + 150 * sqft + 10_000 * bedrooms
examples = [
((800, 2), 180_000),
((1200, 3), 240_000),
((1500, 4), 290_000),
]
for (sqft, beds), actual in examples:
guess = predict_price(sqft, beds)
error = abs(guess - actual)
print(f"guess={guess:,} actual={actual:,} error={error:,}")
Unsupervised learning: finding what you did not ask for
Unsupervised learning has no labels and no correct answers to compare against. You hand the machine a pile of raw data and ask it to find structure — groups, patterns, or anomalies you did not know to look for. A retailer might cluster customers by purchasing habits and discover a group of budget-conscious parents who buy diapers late at night. No one labeled those customers as parents; the algorithm found the cluster on its own. Because there is no right answer to check against, unsupervised methods are harder to evaluate than supervised ones, but they are essential when labels are too expensive to collect or when you genuinely do not know what categories exist. They are often the first step before supervised learning, because they reveal what is even worth labeling.
def distance(a, b):
return sum((x - y) ** 2 for x, y in zip(a, b)) ** 0.5
def closest_centroid(point, centroids):
distances = [distance(point, c) for c in centroids]
return distances.index(min(distances))
customers = [(2, 8), (3, 9), (8, 2), (9, 3)]
centroids = [(2, 8), (8, 2)]
for point in customers:
cluster = closest_centroid(point, centroids)
print(f"{point} -> cluster {cluster}")
flowchart LR
subgraph S["Supervised"]
A1["input + label"] --> A2["model learns rule"]
A2 --> A3["predict on new input"]
end
subgraph U["Unsupervised"]
B1["input only"] --> B2["model finds structure"]
B2 --> B3["clusters / patterns"]
end
style S fill:#166534,color:#fff
style U fill:#b45309,color:#fff
Reinforcement learning: learning from consequences
Reinforcement learning is the paradigm of trial and error. An agent takes actions in an environment, and the environment sends back a reward — a number saying how well the agent did. The agent's goal is to learn a policy: a strategy that picks actions leading to the highest total reward over time. A chess engine that learns by playing millions of games against itself, a robot that learns to walk by falling down repeatedly, and a recommendation system that learns which thumbnail gets the most clicks all use this framework. The feedback is weaker than a label — the reward says that was good but not the correct move was knight to f3 — so the agent must discover the right action through exploration. This makes reinforcement learning slower and more data-hungry, but it is the only honest approach when no one knows the correct action in advance.
import random
random.seed(0)
def pull_arm(arm):
if arm == 0:
return random.random() < 0.7
return random.random() < 0.3
wins = [0, 0]
pulls = [0, 0]
epsilon = 0.2
for _ in range(20):
if random.random() < epsilon:
arm = random.choice([0, 1])
else:
avg0 = wins[0] / max(1, pulls[0])
avg1 = wins[1] / max(1, pulls[1])
arm = 0 if avg0 >= avg1 else 1
reward = pull_arm(arm)
pulls[arm] += 1
if reward:
wins[arm] += 1
print(f"Arm 0: {wins[0]}/{pulls[0]} wins")
print(f"Arm 1: {wins[1]}/{pulls[1]} wins")
flowchart LR Agent["Agent"] -->|"chooses action"| Env["Environment"] Env -->|"returns reward + new state"| Agent Agent -->|"updates policy"| Agent style Agent fill:#7c3aed,color:#fff style Env fill:#059669,color:#fff
How the three overlap in practice
Real projects rarely use one paradigm in isolation. A self-driving car might use supervised learning to recognise traffic signs from labeled photographs, unsupervised learning to detect unusual sensor readings that do not fit any known pattern, and reinforcement learning to decide when to change lanes based on passenger comfort scores. A fraud-detection pipeline might first cluster transactions unsupervised to find new scam types, then label those clusters and switch to supervised classification. Knowing where each piece belongs is what separates a system architect from someone who merely imports libraries. The paradigms are not competitors; they are tools for different kinds of feedback, and most production systems need more than one.
What does this supervised predictor print?
def estimate_age(height):
return 2 * height + 5
print(estimate_age(100))
print(estimate_age(120))
Multiply the height by 2, then add 5.
Each print call produces one line of output.
A streaming service groups viewers by watching habits without predefined genres. Which paradigm is this?
There are no predefined labels or correct answers. The algorithm discovers structure in raw data, which is the defining feature of unsupervised learning.
Write supervised_error(predictions, labels) that returns the mean absolute error between two equal-length lists.
def supervised_error(predictions, labels):
# mean of absolute differences
pass
Sum the absolute differences with zip.
Divide by the number of predictions.
def supervised_error(predictions, labels):
total = 0.0
for p, l in zip(predictions, labels):
total += abs(p - l)
return total / len(predictions)
This function claims to do unsupervised clustering, but it cheats by using labels to assign clusters. Fix it so it only uses points and centroids, computing the nearest centroid by Euclidean distance.
def assign_clusters(points, centroids, labels):
clusters = []
for i, point in enumerate(points):
if labels[i] == 0:
clusters.append(0)
else:
clusters.append(1)
return clusters
Remove the labels parameter entirely.
Compute Euclidean distance to each centroid and pick the smallest.
def assign_clusters(points, centroids):
clusters = []
for point in points:
best = 0
best_dist = float('inf')
for i, c in enumerate(centroids):
d = sum((a - b) ** 2 for a, b in zip(point, c)) ** 0.5
if d < best_dist:
best_dist = d
best = i
clusters.append(best)
return clusters
Write recommend_paradigm(task_description) that returns the most appropriate learning paradigm string: 'supervised', 'unsupervised', or 'reinforcement'. If the description contains 'labeled' or 'answer known', return 'supervised'. If it contains 'no labels' or 'discover groups', return 'unsupervised'. If it contains 'reward' or 'trial and error', return 'reinforcement'. Otherwise return 'supervised' as the default.
def recommend_paradigm(task_description):
# Return 'supervised', 'unsupervised', or 'reinforcement'
# based on keywords in the description.
pass
Convert the description to lowercase before checking.
Check for reinforcement keywords before falling back to supervised.
def recommend_paradigm(task_description):
text = task_description.lower()
if 'labeled' in text or 'answer known' in text:
return 'supervised'
if 'no labels' in text or 'discover groups' in text:
return 'unsupervised'
if 'reward' in text or 'trial and error' in text:
return 'reinforcement'
return 'supervised'
Recap
- Supervised learning learns from labeled examples: input-output pairs that teach the model exactly what to predict. It is the workhorse of industry when labels are available and affordable.
- Unsupervised learning finds hidden structure in raw data without labels, such as clusters or anomalies. It is essential when you do not know what categories exist or cannot afford to label.
- Reinforcement learning learns from rewards and penalties, discovering strategies through trial and error in an environment. It is the right choice when no one knows the correct action in advance.
- The paradigm is a design choice, not a library setting. Choosing wrong means wasted effort and answers to the wrong question.
- When labels exist and are affordable, supervised is usually the right start. When they do not, unsupervised or reinforcement learning may be the only honest path.