Velamuvi AIAcademy

Lesson 3 of 13 · What Is ML?

Types of Machine Learning

The main types of machine learning: supervised, unsupervised, reinforcement and self-supervised learning, for beginners, with a runnable example of each.

  • 5 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • Describe supervised, unsupervised and reinforcement learning
  • Tell regression and classification apart
  • Recognise which type of learning fits a problem
  • Explain how self-supervised learning trains language models

It depends on what the data tells you

The type of machine learning you use depends mostly on one question: what does your data contain?

One example of each

Try it yourselfPython
import random

# ---- 1. Supervised learning: learn from labelled examples ----
fruit = [((150, 7), "apple"), ((170, 8), "apple"), ((120, 3), "orange"), ((130, 2), "orange")]
# features: (weight in grams, smoothness 1-10), label: the fruit

def classify(item):
    nearest = min(fruit, key=lambda ex: (ex[0][0] - item[0]) ** 2 + (ex[0][1] - item[1]) ** 2)
    return nearest[1]

print("Supervised:   a 160 g fruit, smoothness 7 is probably an", classify((160, 7)))

# ---- 2. Unsupervised learning: find groups with no labels at all ----
monthly_spend = [12, 15, 14, 210, 190, 13, 205, 16]
low, high = min(monthly_spend), max(monthly_spend)          # start two group centres
for _ in range(5):
    group_low = [s for s in monthly_spend if abs(s - low) <= abs(s - high)]
    group_high = [s for s in monthly_spend if abs(s - low) > abs(s - high)]
    low, high = sum(group_low) / len(group_low), sum(group_high) / len(group_high)
print("Unsupervised: found two customer groups:", group_low, "and", group_high)

# ---- 3. Reinforcement learning: learn by trial, error and reward ----
random.seed(1)
win_chance = {"A": 0.3, "B": 0.7}        # hidden from the learner
wins = {"A": 0, "B": 0}
tries = {"A": 0, "B": 0}
for step in range(200):
    if random.random() < 0.1 or min(tries.values()) == 0:
        choice = random.choice(["A", "B"])                               # explore
    else:
        choice = max(wins, key=lambda c: wins[c] / tries[c])             # exploit the best so far
    tries[choice] += 1
    wins[choice] += random.random() < win_chance[choice]                 # reward: 1 or 0
print("Reinforcement: after 200 tries, chose A", tries["A"], "times and B", tries["B"], "times")
  • Supervised: the fruit examples came with labels (apple or orange), so the model could predict a label for a new fruit.
  • Unsupervised: the spending figures had no labels, but the program still found two natural groups: occasional and big spenders.
  • Reinforcement: nobody told the learner that option B was better. It tried both, counted the rewards, and ended up choosing B most of the time. The few random tries (exploration) are how it kept checking that A wasn't better after all.

Two kinds of supervised learning

ProblemType
Predict tomorrow's electricity demand in megawattsRegression
Decide whether a loan applicant will repayClassification
Estimate a used car's priceRegression
Recognise which animal is in a photoClassification
Group news articles by topic, with no topic labelsUnsupervised (clustering)
Teach a program to play a video game from its scoreReinforcement

Self-supervised learning

Labels are expensive: someone has to write them. Self-supervised learning creates labels from the data itself.

This is how large language models are trained. Take any sentence, such as “The cat sat on the mat”: the input “The cat sat on the” has the label “mat”. The internet provides trillions of such examples for free. Chat assistants are then refined with further training, including a form of reinforcement learning from human feedback.

Common mistakes

Practice exercise

Name the type of machine learning for each problem.

Your solutionPython
problems = {
    "predict a house price from past sales with known prices": "?",
    "group shoppers by behaviour, with no labels": "?",
    "teach a robot to walk by rewarding progress": "?",
    "flag emails as spam using emails people already marked": "?",
}

# Replace each "?" with "supervised", "unsupervised" or "reinforcement"
for problem, kind in problems.items():
    print(f"{kind}: {problem}")
Knowledge check

A model predicts the number of minutes a delivery will take. What kind of problem is this?

Summary

  • Supervised learning uses labelled examples; unsupervised learning finds structure without labels; reinforcement learning learns from rewards.
  • Supervised learning splits into regression (predict a number) and classification (predict a category).
  • Self-supervised learning makes labels from the data itself, which is how language models are trained.
  • Choose the type by asking what your data contains and what you need to predict.

Next: where machine learning is used, and how to tell whether a problem suits it.

Finished reading? Tick it off to track your progress.