Foundations7 min read

Types of Machine Learning

Three classrooms, three teaching styles β€” supervised, unsupervised, and reinforcement
supervised:Labeled data Β· predict outcomesunsupervised:No labels Β· find hidden structurereinforcement:Trial & error Β· maximize reward

Imagine three very different classrooms.

In Classroom A, the teacher holds up a flashcard with a picture and says, "This is a cat." Then another: "This is a dog." The students learn by seeing examples with answers. After enough flashcards, they can identify new animals on their own.

In Classroom B, there's no teacher. Instead, the students get a big pile of photos and are told: "Sort these into groups that make sense." Nobody tells them what the groups should be β€” the students discover the patterns on their own.

In Classroom C, a student is learning to ride a bike. Nobody gives them a manual. They just try, wobble, fall, adjust, try again. Every time they stay balanced a little longer, they get a "Nice!" They learn by trial, error, and reward.

These three classrooms map exactly to the three main types of machine learning.

1. Supervised Learning β€” "Learn from the answer key"

This is the most common type. You give the model input-output pairs β€” data where you already know the correct answer. The model learns to map inputs to outputs.

Think of it like studying with a textbook that has the answers in the back. You practice, check your answer, and adjust.

Examples in the real world:

  • Email β†’ spam or not spam? (classification)
  • House features β†’ predicted price? (regression)
  • Medical scan β†’ disease or healthy? (classification)
  • Customer data β†’ will they buy? (classification)

The key requirement: You need labeled data β€” someone (usually a human) has to provide the correct answers for the training examples.

Supervised Learning: Predict House Prices

from sklearn.linear_model import LinearRegression
import numpy as np
# Features: [size_sqft, bedrooms, age_years]
X = np.array([
[1400, 3, 10],
[1600, 3, 5],
[1700, 4, 8],
[1875, 4, 3],
[1100, 2, 15],
[2200, 5, 2],
])
# Labels: prices in thousands
y = np.array([245, 312, 279, 380, 180, 450])
model = LinearRegression()
model.fit(X, y)
# Predict price for a new house
new_house = [[1500, 3, 7]]
print(f"Predicted price: ${model.predict(new_house)[0]:.0f}k")
Output
Predicted price: $282k

2. Unsupervised Learning β€” "Find the hidden groups"

Here, there are no labels. The model gets raw data and has to find structure on its own. It's like giving someone a box of 1,000 buttons and saying, "Organize these however makes sense to you."

The model might group them by color, size, number of holes, or material β€” it discovers patterns that aren't obvious to humans.

Examples in the real world:

  • Group customers into segments for marketing (clustering)
  • Detect unusual credit card transactions (anomaly detection)
  • Reduce complex data to its key dimensions (dimensionality reduction)
  • Find related products ("customers who bought X also bought Y")

Unsupervised Learning: Customer Clustering

from sklearn.cluster import KMeans
import numpy as np
# Customer data: [annual_spending, visit_frequency]
customers = np.array([
[500, 2], [480, 3], [520, 1], # Low spenders
[3000, 15], [2800, 12], [3200, 18], # High spenders
[1500, 8], [1400, 7], [1600, 9], # Mid spenders
])
# Let the model find 3 groups (no labels!)
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(customers)
print("Group assignments:", kmeans.labels_)
# New customer β€” which group?
new_customer = [[2500, 10]]
print("New customer group:", kmeans.predict(new_customer))
Output
Group assignments: [2 0 2 0 0 0 1 1 1]
New customer group: [1]

3. Reinforcement Learning β€” "Learn by doing"

This one's different. There's no dataset at all. Instead, an agent interacts with an environment, takes actions, and receives rewards or penalties. Over thousands of attempts, it figures out the best strategy.

It's exactly like learning to play a video game. Nobody hands you a dataset of "correct moves." You just play, die, learn what killed you, and try a different approach next time.

Examples in the real world:

  • Game AI (AlphaGo, Chess engines, Atari bots)
  • Self-driving cars (navigate without crashing = reward)
  • Robot arms learning to pick up objects
  • Recommendation systems (show content β†’ user clicks = reward)

Reinforcement Learning: Simple Reward Agent

import random
# Simple RL: agent learns which door has treasure
# 3 doors, only door 2 has reward
reward_door = 2
q_values = [0.0, 0.0, 0.0] # estimated value of each door
learning_rate = 0.1
for episode in range(100):
# Explore randomly 30% of the time
if random.random() < 0.3:
door = random.randint(0, 2)
else:
door = q_values.index(max(q_values))
reward = 1.0 if door == reward_door else 0.0
# Update: nudge the value toward the reward
q_values[door] += learning_rate * (reward - q_values[door])
print("Learned values:", [f"{v:.2f}" for v in q_values])
print(f"Best door: {q_values.index(max(q_values))}")
Output
Learned values: ['0.03', '0.02', '0.83']
Best door: 2
Note: Which type should you use? Start with supervised learning β€” it's the most practical, well-understood, and has the most tools available. Unsupervised is great for exploration when you don't have labels. Reinforcement learning shines in decision-making problems where the agent interacts with an environment over time.

Quick check

You have 50,000 emails, each labeled "spam" or "not spam." Which type of ML would you use to build a spam filter?
Challenge

Continue reading