internalsGet Pro

Module 01 · Lesson 3 of 10Free

Supervised, unsupervised and reinforcement learning

Level
Beginner
Topic
Machine learning fundamentals
Reading time
14 min

When you finish this page you can

  1. Tell supervised, unsupervised and reinforcement learning apart by the feedback each one learns from.
  2. Explain where a supervised label comes from in an order history, and why it arrives late.
  3. Explain why clustering can’t pick out the customers who will leave, and what it is good for.
  4. Choose between a supervised model, a small random test and reinforcement learning when the outcome depends on the action you take.
  1. What machine learning is: rules learned from data13 min
0:00 / 5:48
Show transcript

0:00 Meet Farah. She’s a machine learning engineer at Kartly, our example online store. She works with the store’s order history: about a million orders, from 200,000 customers. On Monday, Joseph from the growth team brings her three questions. One: which customers are most likely to stop buying? They get this week’s hundred rupee voucher. Two: what kinds of customers do we have? He wants a different email for each kind. Three: is a hundred rupees the right voucher? Maybe fifty rupees brings back almost as many people, for half the cost. All three questions use the same order history. But Farah will answer each one with a different kind of machine learning. Why? Because of what the data can tell her about the right answer. That’s what this lesson is about.

0:43 Back in the last lesson, we saw that a model finds its rules in past examples. Here’s the new part. The three kinds of learning differ in one thing. Each past example says something different about the right answer. Picture the same customers in three tables. In the first, every customer has an extra column: left, or stayed. That’s the right answer, on every row. In the second, there’s no extra column. Only facts about each customer. In the third, there’s a result, but only for the voucher Kartly actually sent. The other results are blank. We’ll take Joseph’s questions in that order.

1:13 Supervised learning means training on examples that already have the correct answer. For the first question, one example is one past customer on one past date. We describe the customer with facts, like days since their last order. Those facts are called features. The correct answer is called the label. Here, the label asks: did this customer order nothing in the next 60 days? The model adjusts until its guesses match the labels. Then it predicts for today’s customers, whose answers nobody knows yet. There are two common shapes of supervised problem. One predicts a category. Will this customer leave, yes or no? That’s classification. The other predicts a number. How much will this customer spend next month? That’s regression. Where does the label come from? Nobody marks customers by hand. Pick a past date, say 1 March. Look at the next 60 days of orders. A customer with no order in that window left. The order history writes the answer by itself. But there are two limits. First, the label arrives late. Farah can label a date only after its 60 days have passed. So her model always learns from a world at least two months old. Second, the facts must stop at that date. If one of them counts orders from inside the window, it contains the answer. The model looks perfect on past data, then fails on Monday.

2:24 Now Joseph’s second question. Kartly’s data has no label for it. No column says bargain hunter or festival-only buyer. So Farah uses unsupervised learning. It gets only the facts about each customer, and hunts for patterns. Its most common job is clustering. That means putting similar customers in the same group. One standard method is called k-means. Farah picks how many groups she wants, say four. The method places four centres. It puts each customer with the nearest one. Then it moves each centre to the average of its customers, and repeats until the groups stop changing. The result is four groups with no names. Farah names them herself, after reading the averages. Weekly regulars, say. Or one order, then gone. Can Joseph send this week’s vouchers to the group with the longest gaps? Not reliably. The method never saw who actually left. So it can’t tell a festival-only buyer, who returns every October, from someone who is gone. Clustering groups by how close the facts are. Nothing in its data says what leaving means. And with no answer key, nobody can say whether four groups is right, or five. You judge groups by whether they’re useful.

3:23 Joseph’s third question is harder. Kartly has always sent a hundred rupees. So the history shows what happened after that voucher. It shows nothing about fifty rupees, or about no voucher. A supervised model can’t learn the effect of an action nobody took. Reinforcement learning learns by doing. It tries an action and gets a reward or a penalty. Then it leans toward actions that pay more. The rule it follows to choose is called its policy. For Kartly, the action is the voucher amount. The reward is what that voucher earned: the profit on the customer’s next order, minus the voucher’s cost. Farah counts it 30 days later. Two things make this harder. First, learning costs money. To learn what fifty rupees earns, Farah must send it to real customers. Some of them would have come back with the bigger voucher. Trying actions you aren’t sure about is called exploration. Second, rewards arrive late. Each turn takes 30 days, so the policy improves about once a month. So how should she find out? She could train a model on past voucher results. But it has nothing to learn about fifty rupees. She could build a full reinforcement learning system now. But with one reward a month, it learns slowly, and it needs far more machinery than this question does. The better choice is a small random test. Send no voucher, fifty rupees and a hundred rupees to 1,000 customers each. Customers are picked at random, so the groups differ only in the voucher. Then compare the rewards after 30 days. Here’s what the test could show, in this lesson’s made-up scenario. A hundred rupees brings back the most customers. But fifty rupees earns the most per customer sent. That small gap could still be chance, so Farah would check before trusting it.

4:55 Ask one question. What does the data say about the right answer? If it has answers for past examples, that’s supervised learning. Labels arrive late. If it has no answer, that’s unsupervised learning. You can’t score the result. If it has a result only for actions taken, that’s reinforcement learning. Exploring costs money and time.

5:13 Five points. One. The kind of learning depends on the feedback in the data. Two. Supervised learning copies labelled answers from the past, and those labels arrive late. Three. Unsupervised learning finds groups, but can’t predict an outcome it never saw. Four. Reinforcement learning learns from the rewards its own actions earn, so it has to explore. Five. When the choice is one action among a few, begin with a small random test. Knowing the kind of learning is one early step. The next lesson follows the churn model through every stage, from Joseph’s question to a list that arrives every Monday. It’s called the ML lifecycle.

Three questions about the same 200,000 customers

is our example online store. Farah, an ML engineer, works on a sample of its data in a free notebook: about 1 million orders from 200,000 customers, across 50,000 products. Joseph leads Kartly’s growth team. On Monday he brings her three questions.

  1. Which 20,000 customers are most likely to stop buying? They get this week’s ₹100 win-back voucher.
  2. What kinds of customers do we have? He wants to write a different email for each kind.
  3. Is ₹100 the right voucher? Maybe ₹50 brings back almost as many people for half the cost.

All three questions use the same order history. Yet Farah will answer each with a different kind of machine learning. The reason is what the data can tell her about the right answer.

Check yourselfAnswer in your head first, then reveal
For the first question, Farah needs past customers marked “left” or “stayed”. Does someone have to go through 200,000 customers and mark them by hand?Show answerHide answer
No. Pick a past date, say 1 March, and look at the next 60 days of orders. A customer with no order in that window left. The order history writes the answer by itself; Farah only has to wait 60 days for it.

So the question this lesson answers is: given a business question and Kartly’s data, how does Farah tell which kind of learning it needs?

The feedback in the data decides

What machine learning is showed that a model finds its rules in past examples. The three kinds of learning differ in one thing: what each example tells the model about the right answer.

Same customers, three kinds of feedback: the right answer for every row, no answer at all, or a result only for the action Kartly actually took.

The next three sections take Joseph’s questions in that order.

Supervised learning copies past answers

trains a model on examples that come with the correct answer.1 For the churn question, each example is one past customer on one date.

The inputs are : numbers that describe the customer up to that date, such as days since their last order or orders in the last year. The answer is the : did this customer place no order in the next 60 days? Training adjusts the model until its predictions match the labels on past customers. Then it predicts for today’s customers, whose answer nobody knows yet. Data, features and labels covers how to choose both.

Supervised problems come in two common shapes.1 predicts a category: will this customer leave, yes or no. predicts a number: how much will this customer spend next month.

Features look back from the snapshot date. The label looks 60 days ahead, so the newest labelled example is always 60 days old.

That picture shows the two limits of supervised learning at Kartly:

  • The label arrives late. Farah can label a snapshot only once its 60 days have passed. Her model always learns from a world at least two months old.
  • Features must stop at the snapshot date. If a feature counts orders from inside the 60-day window, it contains the answer. The model looks perfect on past data and fails on Monday, when that window hasn’t happened yet. Data leakage shows how this slips in.

Here the labels were free. Joseph’s second question has no label at all.

Unsupervised learning finds groups without answers

No column in Kartly’s data says “bargain hunter” or “festival-only buyer”. Joseph wants to know which kinds exist. gets only the features and looks for patterns in them.1

Its most common job is : putting customers who look alike into the same group. One standard method is . Farah picks the number of groups, say four. The method places four centres, assigns each customer to the nearest one, moves each centre to the average of its customers, and repeats until the groups stop changing.

The output is four groups and their averages. The groups have no names. Farah names them after reading the averages, for example “weekly regulars” or “one order, then gone”.1 These names are her reading, not something the method found.

Check yourselfAnswer in your head first, then reveal
One of Farah’s four groups has an average gap of 90 days since the last order. Can Joseph send this week’s vouchers to that group instead of training a churn model?Show answerHide answer
Not reliably. The group formed around long gaps because gap was one of the features. The method never saw who actually left, so it can’t tell a festival-only buyer who returns every October from someone who is gone. It is close to the 60-day rule again, not a churn prediction.

Many people expect clustering to find the customers about to leave. It can’t: it groups by distance between features, and nothing in its data says what “leaving” means.

Unsupervised learning has its own limits:

  • No answer key. Nothing says whether four groups are right, or five. You judge groups by whether they are useful, which Judging clusters when there are no labels covers.
  • The features decide the groups. Days since last order runs into the hundreds; orders last year rarely passes 20. Unless Farah rescales them, distance is mostly about the gap.

Neither kind helps with Joseph’s third question, because Kartly never tried the other vouchers.

Reinforcement learning learns from what actions earn

Kartly has always sent ₹100. The history shows what happened after ₹100, and nothing about ₹50 or no voucher. A supervised model can’t learn the effect of an action nobody took.

learns by acting. It chooses an action, receives a or penalty, and shifts its choices toward actions that earn more.1 The rule it uses to choose is its . Here the action is the voucher amount, and the reward is what the voucher earned:

reward=margin on the customer’s next order−voucher cost
counted 30 days after sending; both are zero if the customer doesn’t come back
The policy picks a voucher, the reward arrives 30 days later, and the policy shifts toward what paid. One turn takes a month.

Two things make this harder than supervised learning:

  • Learning costs money. To learn what ₹50 earns, Farah must send ₹50 to real customers. Some of them would have come back with ₹100. Trying actions you aren’t sure about is called . Sending the best-known voucher is called exploitation.
  • Rewards arrive late. Each turn of the loop takes 30 days, so the policy improves about once a month.

Full reinforcement learning also handles actions that change what happens next. A voucher every week might teach customers to wait for one. Learning effects like that takes many turns of the loop. Reinforcement learning: decisions learned from rewards covers that case. Joseph’s question is simpler: one choice per customer, judged on its own.

How should Farah find out which voucher amount pays best?

Train a supervised model on past voucher results.

Ships this week. But every past customer got ₹100, so the model has nothing to learn about ₹50 or no voucher. It can only say who responds to ₹100.

Build a full reinforcement learning system now.

Could adjust every week and per kind of customer. But with one reward a month, it learns slowly, and it needs far more machinery than the question does.

Why this choice

The test is the exploration step in its simplest form. Customers are assigned at random, so the three groups differ only in the voucher, and the comparison is fair. It uses 3,000 of the 20,000 weekly vouchers, so most of the list still gets ₹100.

The choice changes in two cases. If Joseph wants the split to keep shifting toward the winner while the test runs, that is a bandit, covered in Exploration and bandits. If this week’s voucher changes how customers behave next week, it becomes full reinforcement learning.

Here is what the test could show. Assume, for this scenario, that a returning customer’s next order earns Kartly ₹300 in margin.

Voucher Sent Came back in 30 days Margin Voucher cost Reward Per customer
None 1,000 50 ₹15,000 ₹0 ₹15,000 ₹15.00
₹50 1,000 95 ₹28,500 ₹4,750 ₹23,750 ₹23.75
₹100 1,000 105 ₹31,500 ₹10,500 ₹21,000 ₹21.00

All numbers in this table are this lesson’s scenario, not measurements. ₹100 brings back the most customers, but ₹50 earns the most per customer sent. A gap of ₹2.75 per customer could still be chance at 1,000 customers a group. A/B testing ML models shows how to check.

Picking the kind from the question

Ask what the data says about the right answer, and the kind follows:

What the data gives Kind Kartly example Main cost
The answer for past examples Supervised Who will leave in 60 days Labels arrive late or must be made
No answer Unsupervised Kinds of customers No way to score the result
A result only for actions taken Reinforcement Which voucher pays Exploring costs money and time
Where the three kinds blur

Some methods sit between the kinds. makes labels from the data itself. For example, hide the last product in each basket and train a model to predict it. No person labels anything, yet training is supervised. Self-supervised learning covers it. Language models are pretrained this way, by predicting the next word.

Semi-supervised learning trains on a few labelled examples and many unlabelled ones. It helps when a person has to make each label, such as a product photo’s category.

Try it yourself

This runs in a free Colab or Kaggle notebook in a few seconds. It makes up 200,000 Kartly customers, then trains a supervised model and an unsupervised one on the same features. Look at the two fit lines: that is the whole difference.

Python
import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.linear_model import LogisticRegression

rng = np.random.default_rng(42)
n = 200_000  # customers in Kartly's notebook sample (made-up data)

customers = pd.DataFrame({
    "days_since_last_order": rng.exponential(scale=30, size=n).round(),
    "orders_last_year": rng.poisson(lam=6, size=n),
})
X = customers.to_numpy()

# Supervised: every customer has a label (1 = left in the next 60 days).
# Made-up truth: a gap over 60 days means they left, except 10% random flips.
left = customers["days_since_last_order"].to_numpy() > 60
flip = rng.random(n) < 0.10
y = np.where(flip, ~left, left).astype(int)

train, test = slice(0, 150_000), slice(150_000, n)
clf = LogisticRegression(max_iter=1000)
clf.fit(X[train], y[train])  # features and answers
print("Accuracy on unseen customers:", round(clf.score(X[test], y[test]), 3))

# Unsupervised: the same features, no answers.
km = KMeans(n_clusters=4, n_init=10, random_state=42)
km.fit(X)  # features only
print("Customers per group:", np.bincount(km.labels_))
print(pd.DataFrame(km.cluster_centers_, columns=customers.columns).round(1))

What to look for:

Where this shows up

Knowing the kind of learning is one early step. The ML lifecycle follows the churn model through every stage, from Joseph’s question to a list that arrives every Monday.

Common mistakes

  • Labels that peek ahead. A feature computed from inside the label window makes the model look perfect on past data and fail on live data.
  • Treating clusters as predictions. A cluster of long gaps is not a list of customers who will leave. Only a label can teach that.
  • Asking a model about actions that were never taken. If every customer got ₹100, no amount of past data says what ₹50 does.
  • Reaching for full reinforcement learning first. A one-off choice between a few options needs a random test or a bandit, not a full system.

Check your understanding

Check yourselfAnswer in your head first, then reveal
Joseph asks for each customer’s spend next month. Which kind of learning is this, and which shape?Show answerHide answer
Supervised regression. The label is each past customer’s actual spend in the month after a snapshot date, read from the order history. Not classification: the answer is an amount in rupees, not a category.
Check yourselfAnswer in your head first, then reveal
Farah’s churn model gets 99% right on past customers. One feature is “orders in the 60 days after the snapshot”. What went wrong?Show answerHide answer
The feature contains the label. Any customer with orders in that window stayed, so the model reads the answer instead of predicting it. On Monday that window is in the future, so the feature doesn’t exist and the model fails.
Check yourselfAnswer in your head first, then reveal
Kartly adds a ₹75 voucher. Can the random test from this lesson say what it would earn?Show answerHide answer
No. No customer received ₹75, so there is no reward to learn from. Farah has to send it to some customers, which is exploration, and wait 30 days for the result.
Check yourselfAnswer in your head first, then reveal
Farah reruns k-means with the gap measured in weeks instead of days. The groups change. Which run is correct?Show answerHide answer
Neither is correct by itself. k-means groups by distance, and changing units changes which feature dominates the distance. With no labels there is no answer key; Farah judges each set of groups by whether it is useful to Joseph.

Summary

  1. The feedback in the data decides the kind of learning: right answers mean supervised, no answers mean unsupervised, results of actions taken mean reinforcement learning.
  2. Supervised learning copies labelled past answers. At Kartly the order history writes the churn label, but only after 60 days, and features must stop at the snapshot date.
  3. Unsupervised learning, such as clustering with k-means, finds groups in the features. It can’t predict an outcome it never saw, and the features and their scale shape the groups.
  4. Reinforcement learning learns from rewards for actions it takes, so it must explore, and it learns only as fast as rewards arrive.
  5. For a one-off choice between a few actions, start with a small random test; move to a bandit or full reinforcement learning only when the question needs it.

Report a mistake

Tell us what’s wrong in this lesson and we’ll fix it.