A dataset is a table: each row is an example, each column is a feature, and one column may hold the label. See how the label column separates classification from regression, and what unsupervised and reinforcement learning do without one.
Read a dataset as a table: point to its examples, its features and its label, and state the shapes of X and y.
Turn a raw message into a row of features, by hand and in code.
Tell classification from regression by what the label means, not by how it is stored.
Say what the learner is given in supervised, unsupervised and reinforcement learning, with one example of each.
In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.
The question
A spam filter never reads a message the way you do. It is handed a row of numbers. Who decides which numbers go in the row? Which of them is the answer? And can anything be learned when there is no answer at all?
From the last lesson
1. Where did the number in the learned rule come from?
Show answer
From the examples. The learner tried every cut and kept the one with the fewest mistakes.
2. What three things does Mitchell’s definition of learning ask for?
Show answer
A task, experience and a performance measure. Performance at the task must improve with experience.
Intuition
Lesson 1 described each message by one number. There is more to measure. Take three counts from each message, write them side by side, and the eight messages become a table.
ML01.1.2-F1
The eight messages of lesson 1 as a dataset. Each row is one message. The three blue columns are what was measured. The grey column is the answer that a person supplied.
A dataset is a table like this one. Read it in two directions.
Across, each row is one example: one message here, one flower or one patient elsewhere.
Down, each column is one feature: one property, measured the same way for every example.
One column is special. It holds the answer we want a rule to give. That column is the label. Many books call it the target, and this course uses both words.
The learner’s job can now be said in one line: find a rule that turns a row of features into its label.
Build it up
Step 1. Features are measured, and people choose them. A message arrives as text. Someone decided to count its exclamation marks, its links and its words. Nobody had to choose those three. Another person might count capital letters. The learner works with whatever columns it is given, so this choice sets a limit on what it can learn.
Step 2. Split the table in two. Put the feature columns in a grid of numbers called \(\mat{X}\), with one row per example. Put the labels in a list called \(\vect{y}\). Here \(\mat{X}\) has 8 rows and 3 columns, and \(\vect{y}\) has 8 entries. Row \(i\) of \(\mat{X}\) and entry \(i\) of \(\vect{y}\) belong to the same example.
Step 3. With labels, the label decides the task. Learning from examples that carry their answers is supervised learning, as if a teacher had marked every example. There are two kinds.
ML01.1.2-F2
Supervised learning, twice. Left: the label is one of two names, so the rule answers with a name. Right: the label is a number, so the rule answers with a number.
If the label is a category, a name from a short fixed list, the task is classification. Spam or not spam is classification.
If the label is a number, a quantity that can be a little more or a little less, the task is regression. The right panel shows the four invented points that run through this course: inputs \(1, 2, 3, 4\) with labels \(2, 3, 5, 6\). The line drawn through them answers \(4.0\) for the new input \(2.5\).
Step 4. Without labels, look for structure. Take the label column away and only \(\mat{X}\) is left. Learning from that is unsupervised learning. A common goal is to find groups of examples that resemble each other.
ML01.1.2-F4
Two kinds of learning with no label column. Left: six petal lengths with no species given. The two clumps are there to be found. Right: no table at all. The learner acts and is paid.
The left panel shows the petal lengths of six iris flowers. No one says which flower is which species. The lengths fall into two clumps anyway.
Step 5. Without a table, act and collect rewards. In reinforcement learning nobody hands over a dataset. The learner takes an action, and the world answers with a reward, a number that says how well that went. Picture a machine with two buttons. Button A pays a coin 30% of the time and button B 60% of the time, but the learner is not told. It presses, sees what comes out, and drifts towards B. It is never told “B was the right button”. It sees only coins.
The formal statement
NoteDefinition: labelled dataset
A labelled dataset is a list of \(n\) examples \((\vect{x}_1, y_1), \dots, (\vect{x}_n, y_n)\). Each \(\vect{x}_i = (x_{i1}, \dots, x_{id})\) holds the \(d\) features of example \(i\), and \(y_i\) is its label. Stacked up, the features make \(\mat{X}\), with \(n\) rows and \(d\) columns, and the labels make \(\vect{y}\):
The entry \(x_{ij}\) is feature \(j\) of example \(i\): row first, column second. For the messages, \(n = 8\) and \(d = 3\). Message 4 is Happy birthday!!!!!, so \(\vect{x}_4 = (5, 0, 2)\), \(x_{41} = 5\) and \(y_4 = 0\). The whole of \(\mat{X}\) holds \({8 \times 3 = 24}\) numbers.
The kinds of learning differ in what the learner is given.
Kind (example here)
The learner is given, and its job
Supervised classification (spam or not spam)
\(\mat{X}\), and a \(\vect{y}\) of categories. Predict the category of a new example.
Supervised regression (the four points)
\(\mat{X}\), and a \(\vect{y}\) of numbers. Predict the number for a new example.
Unsupervised (petal lengths)
\(\mat{X}\) only. Find structure, such as groups.
Reinforcement (the two buttons)
No table: it acts and receives rewards. Choose actions that earn the most reward.
What separates classification from regression is what the label means. A category is a name. “Spam” is not greater than “not spam”, and the two cannot be averaged. A number is a quantity: a label of 5 is higher than one of 3, by 2. The pitfalls return to this.
Worked example
Turn three messages into rows, then read the table.
Message 7 is You won!!! Claim at http://win.example now!!!.
Exclamation marks: three after “won” and three after “now”. That is \({3 + 3 = 6}\).
Links: one piece starts with http. That is \(1\).
Words: split at the spaces to get You, won!!!, Claim, at, http://win.example and now!!!. That is \(6\).
A person read it and called it spam. So \(\vect{x}_7 = (6, 1, 6)\) and \(y_7 = 1\).
ML01.1.2-F3
Message 7 becomes row 7. The three features are measured from the text. The label is not in the text: a person supplies it.
In the same way, message 4, Happy birthday!!!!!, has 5 marks, 0 links and 2 words, and it is not spam: \(\vect{x}_4 = (5, 0, 2)\), \(y_4 = 0\). Message 1, Lunch at noon?, gives \(\vect{x}_1 = (0, 0, 3)\), \(y_1 = 0\).
Read the table. It has \(n = 8\) examples and \(d = 3\) features. The label is a category, so the task is classification.
Try another feature. Lesson 1’s learner used only the marks and was left with one mistake. Give it the links column instead. The four normal messages have \(0, 0, 0, 0\) links and the four spam messages have \(1, 1, 1, 2\). The cut \(c = 0.5\) flags exactly the spam: no mistakes. Same learner, same messages, a better feature. What you measure can matter more than how cleverly you learn.
Code
From scratch first: measure every message, then stack the rows.
messages = [ ("Lunch at noon?", 0), ("See you at 6!", 0), ("We got the tickets!!", 0), ("Happy birthday!!!!!", 0), ("Cheap loans!!! Apply at http://loans.example", 1), ("Last chance!! Prices cut!! http://deal.example", 1), ("You won!!! Claim at http://win.example now!!!", 1), ("FREE!!!! FREE!!!! http://free.example http://gift.example", 1),]def features(text):"""Measure one message: exclamation marks, links, words."""return [text.count("!"), text.count("http"), len(text.split())]import numpy as npX = np.array([features(text) for text, label in messages]) # one row per exampley = np.array([label for text, label in messages]) # one label per exampleprint(X)print("X has shape", X.shape, "and y has shape", y.shape)print("example 4:", X[3], "label", y[3]) # Python counts from 0, so row 3 is message 4
[[0 0 3]
[1 0 4]
[2 0 4]
[5 0 2]
[3 1 5]
[4 1 5]
[6 1 6]
[8 2 4]]
X has shape (8, 3) and y has shape (8,)
example 4: [5 0 2] label 0
Now run lesson 1’s learner on each column in turn.
marks: best cut 2.5, mistakes 1
links: best cut 0.5, mistakes 0
words: best cut 4.5, mistakes 1
The library for tables is pandas, and scikit-learn ships real datasets. Two of them return later in this course. Iris has 150 flowers, 4 features and a species label with 3 categories. The diabetes set has 442 patients, 10 features and a numeric label: a measure of how far the disease had progressed one year later.
Unsupervised learning on real data: hide the species, keep the two petal measurements, and ask a grouping method called k-means for three groups.
from sklearn.cluster import KMeanspetals = iris.data[:, 2:4] # two feature columns; the label column is left outgroups = KMeans(n_clusters=3, n_init=10, random_state=0).fit_predict(petals)counts = np.array([np.bincount(iris.target[groups == g], minlength=3) for g inrange(3)])print("group sizes:", np.bincount(groups))print("species inside each group (setosa, versicolor, virginica):")print(counts)
group sizes: [52 50 48]
species inside each group (setosa, versicolor, virginica):
[[ 0 48 4]
[50 0 0]
[ 0 2 46]]
The groups have 52, 50 and 48 flowers. Only afterwards do we look at the species. One group is exactly the 50 setosa. The other two are versicolor and virginica, apart from 6 flowers that land in the other’s group. So 144 of 150 flowers are grouped by species, and no label was used.
Last, the two-button machine. The learner keeps a running average of each button’s rewards. It mostly presses the button that looks better, and one time in ten it presses at random.
rng = np.random.default_rng(0)pays = [0.3, 0.6] # chance of a coin from A and from B: hidden from the learnervalue = [0.0, 0.0] # the learner's estimate of each buttonpresses = [0, 0]coins =0for step inrange(1000):if rng.random() <0.1: a =int(rng.integers(2)) # explore: a random buttonelse: a =0if value[0] >= value[1] else1# otherwise the button that looks better reward =1if rng.random() < pays[a] else0 presses[a] +=1 value[a] += (reward - value[a]) / presses[a] # running average of this button's rewards coins += rewardprint("presses of A and B:", presses)print("estimated pay:", [round(v, 2) for v in value], " coins won:", coins)
presses of A and B: [57, 943]
estimated pay: [0.33, 0.61] coins won: 590
It pressed B 943 times out of 1,000 and won 590 coins. Pressing at random would win about 450.
Explore
This module’s interactive, Rule versus learner, is in lesson 1. Each dot there is one row of a small table: one feature and a label. Run the learner, then press Change its label to edit the label column, and watch the learned cut answer.
Pitfalls
ML01.1.2-F5
Wrong beside right. Both columns separate the eight examples with no mistakes. Only one of them says anything about the next message.
“Every column in the table is a feature.”
The row number splits our eight messages perfectly: rows 1 to 4 are normal and rows 5 to 8 are spam, so the cut \(4.5\) makes no mistakes. That is an accident of the order the table was typed in. The ninth message gets the number 9 and is flagged whatever it says. Identifiers such as row numbers and names are not measurements of the example. Drop them before learning.
“The label is stored as a number, so this is regression.”
Iris stores its species as 0, 1 and 2. Those are names in disguise. The average of 0 and 2 is 1, but setosa and virginica do not average to versicolor. Ask what the label means. A category makes the task classification, however it is stored.
“Unsupervised learning discovers the true categories.”
It proposes groups. We asked for three and got three. They matched the species for 144 flowers out of 150, and we only know that because we held the labels back. Without labels there is no answer key, and a person has to decide what the groups mean.
“A reward is just a label.”
A label says what the right answer was. A reward says how well the chosen action turned out. When the learner pressed A and got nothing, nobody told it what B would have paid. It has to try B to find out.
Check yourself
Recall
1. In a dataset table, what is a row, what is a column, and what is the label?
Show answer
A row is one example. A column is one feature, measured for every example. The label is the column that holds the answer to be predicted.
2. A dataset has \(n\) examples and \(d\) features. What are the shapes of \(\mat{X}\) and \(\vect{y}\)?
Show answer
\(\mat{X}\) has \(n\) rows and \(d\) columns. \(\vect{y}\) has \(n\) entries, one label per example.
3. What decides whether a supervised task is classification or regression?
Show answer
What the label means. A category gives classification. A quantity gives regression.
4. What is missing in unsupervised learning, and what does the learner look for?
Show answer
The labels. The learner is given \(\mat{X}\) only and looks for structure, such as groups of similar examples.
5. What does a reinforcement learner receive in place of labelled examples?
Show answer
Rewards. It takes actions, and after each one it receives a number that says how well that went.
Exercises
1.★ A new message reads Meeting moved to 3!! See http://cal.example. Work out its row of features. What do the rules “links above 0.5” and “marks above 2.5” each answer?
Show solution
Marks: 2. Links: 1. Words: Meeting, moved, to, 3!!, See, http://cal.example, which is 6. The row is \((2, 1, 6)\).
The links rule says spam, because \(1 > 0.5\). The marks rule says not spam, because \(2 > 2.5\) is false. The message is an ordinary note about a meeting. The links rule made no mistakes on the eight examples and is wrong on the ninth. Eight examples are very few.
2.★★ Run lesson 1’s learner by hand on the words column: \(3, 4, 4, 2\) for the normal messages and \(5, 5, 6, 4\) for the spam.
Show solution
Candidates run from \(-0.5\) to \(6.5\). Any cut below 2 flags every message: 4 mistakes. Then:
Cut
Normal flagged
Spam let through
Mistakes
\(2.5\)
3
0
3
\(3.5\)
2
0
2
\(4.5\)
0
1
1
\(5.5\)
0
3
3
\(6.5\)
0
4
4
The learner returns \(c = 4.5\), with 1 mistake: the spam message with 4 words is let through.
3.★★★ A city wants to predict, at 7 am, whether each bike-share station will be empty at 8 am. Describe a dataset for this task: an example, three features and the label. Which kind of learning is it? Name one column that must not be used as a feature.
Show solution
One good answer. An example is one station on one morning. Features, all known at 7 am: the number of bikes in the station at 7 am, the day of the week, and the temperature. The label is “empty at 8 am”, yes or no. Past mornings supply rows whose labels are known.
The label is a category, so this is supervised classification. Predicting the number of bikes at 8 am instead would be regression.
A column that must not be used: the number of bikes at 8 am, or anything else recorded after 7 am. It is the answer in another form and will not exist when the rule is used. A station identifier needs care too. A learner could treat it as a quantity, and it is only a name.
Summary card
A dataset is a table: rows are examples, columns are features, one column is the label.
ML01.1.2-F1
Rows, columns and one answer column.
Shapes.\(\mat{X}\) has \(n\) rows and \(d\) columns. \(\vect{y}\) has \(n\) entries. \(x_{ij}\) is feature \(j\) of example \(i\).
Supervised. Labels are given. A category makes it classification, a number makes it regression.
Unsupervised. No labels. The learner looks for structure in \(\mat{X}\).
Reinforcement. No table. The learner acts and receives rewards.
Sources and next steps
Sources. Each was opened and checked against this lesson on 12 November 2026.
Marc Peter Deisenroth, A. Aldo Faisal and Cheng Soon Ong, Mathematics for Machine Learning, Cambridge University Press, 2020 (free PDF). Section 8.1.1 treats each row of a table as an example and each column as a feature, lists “target” among the names of the label, writes the examples as a matrix with one row per example, and drops an identifier column because it is not informative.
Tengyu Ma and Andrew Ng, CS229 Lecture Notes, Stanford University. The opening of Part I (pages 7 and 8) defines input features, the target, a training example and a training set, and separates regression (a continuous target) from classification (a small number of discrete values).
Richard S. Sutton and Andrew G. Barto, Reinforcement Learning: An Introduction, 2nd edition, MIT Press, 2018 (free PDF). Section 1.1 describes supervised learning as learning from labelled examples and unsupervised learning as finding hidden structure, and sets reinforcement learning beside them as a third kind, driven by a reward signal.
Google, Introduction to Machine Learning: What is ML? and Supervised learning. The first says a regression model predicts a numeric value and that clustering differs from classification “because the categories aren’t defined by you”. The second defines features, labels, and labelled and unlabelled examples.
scikit-learn, Toy datasets and KMeans. Iris: 150 flowers, 50 in each of three classes, 4 features. Diabetes: 442 patients, 10 features, and a target that measures disease progression one year after baseline.