Skip to main content
Machine Learning & Deep Learning

Supervised, Unsupervised, and Reinforcement Learning

Understand the three core paradigms of machine learning and when to use each.

Beginner28 minBy ToolDix Editorial

Learning objectives

  • Define the three learning paradigms and the type of data/signal available to each
  • Match a problem statement to the appropriate paradigm based on available labels and feedback
  • Implement minimal examples of supervised classification, unsupervised clustering, and reinforcement learning with Q-learning

ToolDix original visual

ML & Deep Learning practice loop
1

Frame

Name the outcome and constraints.

2

Build

Try one bounded workflow.

3

Review

Keep evidence, revise, and share.

Three learning paradigms at a glance

ToolDix original diagram
Three learning paradigms at a glance
Supervised
Data: Labeled pairs: (input, label)
Goal: Classification, regression, segmentation
Example: Predict house price from square footage, beds, location
Unsupervised
Data: Unlabeled data only
Goal: Clustering, dimensionality reduction, anomaly detection
Example: Group customer transactions by spending pattern without predefined labels
Reinforcement
Data: Agent, environment, rewards
Goal: Decision-making under uncertainty, sequential choice
Example: Game-playing bot learns moves by trial-and-error reward signal
Most practical ML systems are supervised because labeled data is available; unsupervised and reinforcement are growing frontiers where labeled data is rare or expensive.

Machine learning breaks into three categories based on what feedback you have available:

Supervised learning

You have pairs of (input, label). The model learns to predict the label from the input. Labels mean the ground truth is known: house prices for real estates, medical diagnoses for patient records, spam/not-spam for emails.

Use supervised learning when:

  • You have hundreds or thousands of labeled examples
  • The task is to predict a categorical label (classification) or continuous value (regression)
  • The prediction time and decision are well-defined

Examples: classifying images of cats vs dogs, predicting customer churn, estimating housing prices, sentiment analysis of reviews.

Unsupervised learning

You have only inputs; there are no labels. The model discovers structure: grouping similar customers, reducing noise, or finding latent patterns.

Use unsupervised learning when:

  • Labels are unavailable or too expensive to collect
  • You want to explore data structure rather than predict a fixed target
  • The goal is clustering, dimensionality reduction, or anomaly detection

Examples: customer segmentation from purchase history, dimensionality reduction before visualization, detecting unusual transactions in fraud detection, topic discovery in documents.

Reinforcement learning

An agent takes actions in an environment and receives rewards (or penalties). The agent learns a policy — a strategy for choosing actions — that maximizes cumulative reward. Feedback is indirect: the agent does not know the right action upfront; it learns by trial and error.

Use reinforcement learning when:

  • The system makes sequential decisions with long-term consequences
  • Rewards are sparse or delayed (you only know if you won after many moves)
  • Simulation or live interaction is available for training

Examples: game-playing bots, robot control, resource allocation, recommendation systems with user feedback loops.


Comprehensive comparison table

| Dimension | Supervised | Unsupervised | Reinforcement | |-----------|-----------|---------|---------| | Data requirement | Labeled pairs (X, y); typically 100s–1000s needed | Unlabeled X only; can use millions of points | Simulator or live environment + reward signal | | Feedback signal | Immediate, ground-truth label for each input | None; algorithm discovers structure | Delayed reward; often sparse or noisy | | Typical algorithm | Logistic regression, decision trees, neural networks | K-means, PCA, DBSCAN, autoencoders | Q-learning, policy gradient, actor-critic | | Prediction task | Classification or regression; predict a fixed target | Clustering, dimensionality reduction, anomaly detection | Sequential decision-making; optimize cumulative reward | | Training time | Hours to days on standard hardware | Minutes to hours (unsupervised is often faster) | Days to weeks (requires many environment interactions) | | Interpretability | Medium–high (coefficients, feature importance) | Medium (clusters are human-inspectable) | Low (policies are often black-box) | | Production cost | Low latency per prediction (~ms); batch scoring easy | Low latency; clustering is deterministic | Moderate latency; may require fast inference | | Failure mode | Overfitting to training labels if biased | Over-clustering (too many clusters) or under-clustering | Learns a bad policy; needs human oversight | | Typical ROI | High (directly predicts business outcome) | Medium (often requires downstream action) | Very high if goal is dynamic optimization (but risky) |


Real-world example: Netflix recommendation system

Netflix uses all three paradigms in sequence:

  1. Supervised learning: Predict if a user will watch a particular title given their history. Input: user profile, watch history, metadata. Label: watch vs. didn't watch. Model: neural network on collaborative filtering embeddings.
  2. Unsupervised learning: Cluster movies by latent features (genre, pacing, target audience) learned from aggregated viewing patterns, without explicit genre labels.
  3. Reinforcement learning: Optimize the order of recommendations on the home page to maximize watch time, using A/B tests as reward signals. The "environment" is the user's session; the "action" is which 3 titles to show; the "reward" is whether they click and watch.

Each paradigm solves a different problem. Supervised learning is the foundation (what will they watch?), unsupervised learning is the structure (what are the movies really about?), and reinforcement learning is the strategy (how do we present options to maximize engagement?).


Decision guide: which paradigm?

Ask yourself three questions:

  1. Do I have labels?

    • Yes → Supervised learning
    • No → Unsupervised learning, unless...
  2. Are the decisions sequential with delayed rewards?

    • Yes → Reinforcement learning
    • No → Go to the paradigm from question 1
  3. Is the goal prediction or discovery?

    • Prediction (supervised or RL) → Focus on test accuracy
    • Discovery (unsupervised) → Evaluate cluster quality, interpretability

Supervised learning: classification

Supervised learning maps inputs to outputs using labeled training data. The two main variants are:

  • Classification: Predict a discrete category (spam/not-spam, disease/healthy, churn/retain)
  • Regression: Predict a continuous value (house price, temperature, revenue)

Example: logistic regression for binary classification

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix

# Generate synthetic labeled data: 1000 samples, 2 classes
X, y = make_classification(
    n_samples=1000,
    n_features=20,
    n_informative=15,
    n_redundant=5,
    n_classes=2,
    random_state=42
)

# Split into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Train a logistic regression classifier
clf = LogisticRegression(max_iter=1000, random_state=42)
clf.fit(X_train, y_train)

# Predict on test set
y_pred = clf.predict(X_test)
y_pred_proba = clf.predict_proba(X_test)[:, 1]

# Evaluate
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)

print(f"Classification Results:")
print(f"  Accuracy: {accuracy:.3f}")
print(f"  Precision: {precision:.3f}")
print(f"  Recall: {recall:.3f}")
print(f"  Confusion matrix:\n{cm}")
print()
print(f"Interpretation:")
print(f"  Of predicted positives, {precision:.1%} were actually positive (precision).")
print(f"  Of actual positives, we caught {recall:.1%} (recall).")

Unsupervised learning: clustering

Unsupervised learning discovers patterns without ground truth labels. Clustering is the most common task: grouping similar examples together.

Example: k-means clustering

from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt

# Generate synthetic unlabeled data: 300 samples, 2 features, 3 true clusters
X, y_true = make_blobs(
    n_samples=300,
    n_features=2,
    centers=3,
    random_state=42
)

# Apply k-means clustering (we specify k=3, though in practice you might tune this)
kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
cluster_assignments = kmeans.fit_predict(X)

# Evaluate: silhouette score (how well-separated are the clusters?)
silhouette = silhouette_score(X, cluster_assignments)
print(f"Clustering Results:")
print(f"  Silhouette score: {silhouette:.3f}")
print(f"  (Range: -1 to 1; higher is better; 0.5+ indicates well-separated clusters)")
print()

# Compare to true structure (only for demonstration; real data has no labels)
from sklearn.metrics import adjusted_rand_score
ari = adjusted_rand_score(y_true, cluster_assignments)
print(f"Adjusted Rand Index (vs true clusters): {ari:.3f}")
print(f"  (This is only available in our synthetic example; real data has no ground truth.)")
print()

# Visualize (works only for 2 features; high-dimensional data needs dimensionality reduction)
plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.scatter(X[:, 0], X[:, 1], c=y_true, cmap="viridis", alpha=0.6, label="True structure")
plt.title("True clusters (only known in simulation)")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")

plt.subplot(1, 2, 2)
plt.scatter(X[:, 0], X[:, 1], c=cluster_assignments, cmap="viridis", alpha=0.6, label="k-means clusters")
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1],
            marker='X', s=200, c='red', edgecolors='black', label='Centroids')
plt.title("k-means discovered clusters")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.legend()
plt.tight_layout()
plt.show()

Key difference from supervised: We never told the algorithm which samples belong to the same cluster. It discovered the grouping purely from proximity in feature space.


Reinforcement learning: agent and environment

In reinforcement learning, an agent learns to act in an environment by trial and error. At each step, the agent observes the current state, chooses an action, and receives a reward and the next state.

A simple example: a robot that learns to reach a goal by exploring and receiving reward signals.

import numpy as np

# Minimal RL example: a simple grid-world
# State space: 5 grid positions
# Action space: 2 actions (move left, move right)
# Goal: reach position 4 (the rightmost position)

n_states = 5
n_actions = 2
n_episodes = 100
learning_rate = 0.1
discount_factor = 0.95

# Initialize Q-table (estimated value of each action in each state)
Q = np.zeros((n_states, n_actions))

def choose_action(state, epsilon=0.1):
    """Epsilon-greedy: explore with probability epsilon, exploit otherwise."""
    if np.random.rand() < epsilon:
        return np.random.choice(n_actions)  # Random action (explore)
    else:
        return np.argmax(Q[state, :])  # Best action (exploit)

def step(state, action):
    """Simulate one step in the environment."""
    next_state = state + (1 if action == 1 else -1)
    next_state = max(0, min(n_states - 1, next_state))  # Clamp to [0, 4]
    reward = 1 if next_state == 4 else -0.1  # +1 at goal, small penalty per step
    return next_state, reward

# Training loop
episode_rewards = []

for episode in range(n_episodes):
    state = 0  # Start at leftmost position
    episode_reward = 0

    for step_count in range(20):  # Max 20 steps per episode
        action = choose_action(state, epsilon=0.2)
        next_state, reward = step(state, action)

        # Q-learning update: Q(s, a) = Q(s, a) + lr * (r + γ * max_a' Q(s', a') - Q(s, a))
        max_next_q = np.max(Q[next_state, :])
        Q[state, action] += learning_rate * (reward + discount_factor * max_next_q - Q[state, action])

        state = next_state
        episode_reward += reward

        if next_state == 4:  # Reached goal
            break

    episode_rewards.append(episode_reward)

print(f"Reinforcement Learning: Simple Grid-World")
print(f"  Q-table (learned values for each state-action pair):")
print(f"  {Q}")
print(f"  Average reward per episode (last 10): {np.mean(episode_rewards[-10:]):.2f}")
print()
print(f"  Interpretation:")
print(f"  Higher Q values indicate better actions in that state.")
print(f"  The agent learned that action 1 (move right) is better from leftward states.")

Real-world paradigm choices

Most practical systems use supervised learning when labeled data is available. It is the most straightforward to understand, evaluate, and deploy.

Unsupervised learning dominates when labels are rare or when the goal is exploratory: customer segmentation, anomaly detection, or dimensionality reduction before visualization.

Reinforcement learning is less common in production but growing in domains where sequential decision-making and long-term optimization matter: recommendation systems, game AI, and robotic control.

Many modern systems combine paradigms: use unsupervised learning to find clusters, supervised learning to classify within clusters, and reinforcement learning to optimize a recommendation policy based on user interaction.

Choose the paradigm that matches your data, your goal, and the feedback you can obtain. Start with supervised learning if you have labels — it is the most mature, best understood, and easiest to debug.


Common mistake: forcing the wrong paradigm

Mistake 1: Using unsupervised learning when you have labels. Clustering a dataset when you have ground-truth labels is wasteful. Use supervised learning; it will almost always beat unsupervised clustering on predictive accuracy because it has access to the true signal.

Mistake 2: Using supervised learning when labels are biased. If your labeled dataset has selection bias (e.g., you only have labels for high-value customers), the model will learn the bias, not the ground truth. Unsupervised methods can help detect structure despite label bias; supervised methods amplify it.

Mistake 3: Using reinforcement learning without an environment. Reinforcement learning requires repeated interaction with an environment. If you only have a static dataset and no way to simulate or A/B test, supervised learning is more appropriate. Training RL on a fixed dataset without interaction often fails.

Mistake 4: Expecting unsupervised learning to solve supervised problems. Clustering customer segments is useful, but if your goal is to predict churn (yes/no), clustering alone does not give you a model. You need supervised learning on top: cluster first (unsupervised), then train a logistic regression within each cluster (supervised).


Real-world combinations and hybrid approaches

Most modern systems combine paradigms:

  1. Recommendation systems (Netflix, Amazon):

    • Unsupervised: Cluster movies via matrix factorization (find latent genres)
    • Supervised: Predict rating given user embedding and movie embedding
    • Reinforcement: Optimize ranking on the home page via A/B tests (maximize watch time)
  2. Fraud detection (credit cards, banking):

    • Unsupervised: Anomaly detection on spending patterns (flag unusual transactions)
    • Supervised: Predict probability that a transaction is fraudulent given merchant, amount, location
    • Reinforcement: Optimize fraud-catching rules via feedback loops (minimize false positives while catching true fraud)
  3. Search and ranking (Google, Bing):

    • Unsupervised: Discover topics in documents via topic modeling
    • Supervised: Rank results by relevance given query
    • Reinforcement: Optimize ranking order via user click data (higher CTR = better ranking)

The paradigm you choose depends on your data availability and goal:

  • Have labels? → Supervised
  • Labels are expensive? → Unsupervised first to explore, then supervised on a small labeled subset
  • Sequential decisions with feedback? → Reinforcement learning (possibly with supervised learning inside)

Additional code example: multi-class classification with supervised learning

For problems with more than 2 classes, use softmax instead of sigmoid, and categorical cross-entropy instead of binary cross-entropy.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

# Load iris dataset (3 classes: setosa, versicolor, virginica)
iris = load_iris()
X, y = iris.data, iris.target

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Multi-class logistic regression (automatically uses softmax + categorical cross-entropy)
clf = LogisticRegression(max_iter=1000, random_state=42)
clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)
y_pred_proba = clf.predict_proba(X_test)

print("Multi-class Classification Results:")
print(f"  Accuracy: {accuracy_score(y_test, y_pred):.3f}")
print()
print("Confusion Matrix (rows=actual, cols=predicted):")
print(confusion_matrix(y_test, y_pred))
print()
print("Per-class metrics:")
print(classification_report(y_test, y_pred, target_names=iris.target_names))
print()
print(f"Predicted probabilities for first test sample:")
print(f"  {iris.target_names}: {y_pred_proba[0]}")

Key difference from binary classification: The model outputs K probabilities (one per class) that sum to 1, using the softmax function:

P(class=k | x) = exp(z_k) / Σ_j exp(z_j)

where z_k is the linear score for class k. The loss is categorical cross-entropy:

J = -(1/n) · Σᵢ Σ_k y_i,k · log(P_i,k)

This is zero when the model assigns 100% probability to the true class, and increases as probability decreases.


Practical considerations for paradigm selection in industry

Time and data constraints matter:

  • Supervised learning: Requires labeled data. Labeling 10,000 examples takes weeks and costs $5K–$50K. But accuracy is usually high (90%+).
  • Unsupervised learning: No labeling, but results may require human validation (is cluster 3 meaningful?). Quick to run, unclear how to act on results.
  • Reinforcement learning: Requires a simulator or live environment. Training a game-playing bot takes weeks of compute; training a robot policy takes months of real-world trials.

Data leakage in paradigm choice:

  • Supervised: Don't train on data that includes the future outcome at prediction time.
  • Unsupervised: Watch for leakage if using embeddings trained on labeled data — you've leaked label information.
  • Reinforcement: Ensure the reward signal does not depend on random chance or adversarial user behavior.

Production deployment:

  • Supervised: Fast inference (ms), easy to monitor (accuracy on labeled test set).
  • Unsupervised: Hard to monitor (how do you know if clusters are correct?). Requires downstream human review.
  • Reinforcement: Risky (agent might find shortcuts); requires careful reward design and testing.

Sources and license context

These references informed the lesson. ToolDix adds its own explanation, workflow, and practice rather than reproducing source material. Every link below leaves ToolDix and opens the publisher's own site in a new tab.

Keep going

Read these next on ToolDix.

Original lessons that build on what you just read.