What Machine Learning Really Means
Learning the rule from examples instead of writing it
In this lesson
Unit 12 let you describe what happened. This unit is about predicting what happens next, and about being honest when the prediction is not good enough to use.
Machine learning has an unhelpful reputation. Strip the marketing away and it is a plain idea: instead of writing the rule yourself, you show the computer enough examples that it works the rule out.
Explain it like I’m 5
Machine learning is teaching the computer from examples instead of writing every rule by hand — like learning what a cat looks like by seeing hundreds of cats rather than reading a description.
Features and labels
Two words carry most of the vocabulary, and they map onto the table you already know from Unit 12.
A feature is an input column, a fact you know. A label (also called the target) is the output column, the thing you want to predict. Every row is one example pairing its features with its answer.
Naming which columns are which is the first decision in any project, and getting it backwards is a surprisingly common way to waste an afternoon.
houses = [
{"size_m2": 50, "bedrooms": 1, "price_k": 150},
{"size_m2": 75, "bedrooms": 2, "price_k": 205},
{"size_m2": 110, "bedrooms": 3, "price_k": 295},
]
FEATURES = ["size_m2", "bedrooms"]
LABEL = "price_k"
for house in houses:
inputs = [house[name] for name in FEATURES]
print(f"features {inputs} -> label {house[LABEL]}")
print("rows:", len(houses), "| features per row:", len(FEATURES))
features [50, 1] -> label 150 features [75, 2] -> label 205 features [110, 3] -> label 295 rows: 3 | features per row: 2
A rule you write, versus a rule the data gives you
Here is the whole distinction in one example. You could decide that houses cost about $2,700 per square meter and write that number into your code. Or you could let the examples tell you the number.
The second is machine learning, and it is not any more magical than the first: it is arithmetic over your data. What it buys you is that when the data changes, the rule updates itself.
# The hand-written rule: a human decides the number.
def rule_based(size_m2):
return size_m2 * 2.7
# Learning from examples: work the number out from the data itself.
sizes = [50, 75, 110]
prices = [150, 205, 295]
learned_rate = sum(prices) / sum(sizes)
print("human guess per m2:", 2.7)
print("learned per m2: ", round(learned_rate, 3))
print("rule predicts 90m2: ", round(rule_based(90), 1))
print("learned predicts: ", round(90 * learned_rate, 1))
human guess per m2: 2.7 learned per m2: 2.766 rule predicts 90m2: 243.0 learned predicts: 248.9
Supervised, unsupervised, and where you are
Almost everything in this unit is supervised learning: your examples come with the right answers attached, and the model learns to reproduce them. House prices you already sold at, emails already marked as spam.
Unsupervised learning has no answer column. You hand over the data and ask what structure is in it: which customers behave alike, for instance. It is genuinely useful and a much harder thing to evaluate, because there is nothing to check against.
Supervised problems split in two, and they are the next two lessons. Predicting a number is regression. Predicting a category is classification.
Name the label for three prediction problems. Each description says what you want to predict. Fill in the column name that would hold that answer, using "price", "is_spam" and "temp_tomorrow". Getting this right is the first step of every project.
DATASETS = {
"predict house price from size and bedrooms": "?",
"predict if an email is spam from word counts": "?",
"predict tomorrow's temperature from today's": "?",
}
for description, label in DATASETS.items():
print(f"{label:14s} <- {description}")
print("datasets:", len(DATASETS))
The label is always the thing after “predict”. Everything else in the sentence is a feature. Two of these labels are numbers (regression) and one is a yes/no (classification).
DATASETS = {
"predict house price from size and bedrooms": "price",
"predict if an email is spam from word counts": "is_spam",
"predict tomorrow's temperature from today's": "temp_tomorrow",
}
for description, label in DATASETS.items():
print(f"{label:14s} <- {description}")
print("datasets:", len(DATASETS))
price <- predict house price from size and bedrooms
is_spam <- predict if an email is spam from word counts
temp_tomorrow <- predict tomorrow's temperature from today's
datasets: 3
Why evaluation is the whole game
A model always produces an answer. It has no way to say “I do not know”, and it will confidently predict a house price from nonsense inputs.
So a prediction on its own is worthless. A prediction plus a measure of how wrong it usually is is useful. That is why every lesson in this unit pairs a prediction with a score, and why a model with no evaluation should never be trusted with a decision.
The other half of that honesty is the data. A model trained on the last decade's hiring decisions learns the last decade's biases, faithfully and invisibly. It is not neutral for being arithmetic — it reproduces whatever the examples contained, including the parts nobody intended.
Common mistake: Thinking machine learning is magic
The results can be impressive and the vocabulary is deliberately grand.
It is arithmetic that finds a rule in examples. Knowing that makes it much easier to reason about when it will fail.
Common mistake: Using a model where a rule would do
Machine learning sounds like the more capable answer.
If you can state the rule, write it. It will be faster, testable, and explainable to whoever asks.
Common mistake: Ignoring bad or biased training data
Training succeeds regardless of whether the data deserved to be trusted.
Check what the examples represent before training. A model reproduces the patterns in its data, including the ones you would not endorse.
Common mistake: Mixing up features and the label
They are all just columns in the same table.
Features are what you know; the label is what you want to find out. Write both down before any code.
What is a feature?
Features are the facts you already know. The label is the thing you want to predict.
What is a label?
Also called the target. In supervised learning your training examples come with it attached.
Why must you test a model?
A model cannot say “I do not know”. Only evaluation tells you whether its confident answer is any good.
Mini exercise (easy)
Sort six prediction problems into the two kinds of supervised learning. Mark each "regression" if the answer is a number, or "classification" if the answer is a category. This one distinction decides which tools you reach for in the next two lessons.
Have a go. Finish the code and press Run to see the result immediately, right on this page.
# Mark each problem "regression" if the answer is a NUMBER,
# or "classification" if the answer is one of a fixed set of CATEGORIES.
PROBLEMS = {
"predict a house price in pounds": "?",
"predict whether an email is spam": "?",
"predict tomorrow's temperature": "?",
"predict which of three flower species": "?",
"predict how many minutes a delivery takes": "?",
"predict whether a student passes or fails": "?",
}
for problem, kind in PROBLEMS.items():
print(f"{kind:14s} {problem}")
counts = {k: list(PROBLEMS.values()).count(k) for k in ("regression", "classification")}
print("regression:", counts["regression"], "| classification:", counts["classification"])
Ask what the answer looks like. A price, a temperature or a count is a number, so regression. A yes/no, a species, or a grade band is one of a fixed set of options, so classification.
# Mark each problem "regression" if the answer is a NUMBER,
# or "classification" if the answer is one of a fixed set of CATEGORIES.
PROBLEMS = {
"predict a house price in pounds": "regression",
"predict whether an email is spam": "classification",
"predict tomorrow's temperature": "regression",
"predict which of three flower species": "classification",
"predict how many minutes a delivery takes": "regression",
"predict whether a student passes or fails": "classification",
}
for problem, kind in PROBLEMS.items():
print(f"{kind:14s} {problem}")
counts = {k: list(PROBLEMS.values()).count(k) for k in ("regression", "classification")}
print("regression:", counts["regression"], "| classification:", counts["classification"])
regression predict a house price in pounds
classification predict whether an email is spam
regression predict tomorrow's temperature
classification predict which of three flower species
regression predict how many minutes a delivery takes
classification predict whether a student passes or fails
regression: 3 | classification: 3
assert PROBLEMS["predict a house price in pounds"] == "regression", "a price is a number"
assert PROBLEMS["predict whether an email is spam"] == "classification", "spam or not is a category"
assert PROBLEMS["predict which of three flower species"] == "classification", "a species is one of a fixed set"
assert PROBLEMS["predict how many minutes a delivery takes"] == "regression", "minutes are a number"
assert sorted(PROBLEMS.values()) == ["classification"] * 3 + ["regression"] * 3, "three of each"
print("✓ Looks good!")