Machine learningChapter 101 of 114
What Machine Learning Is
Finding the rules from examples instead of writing them yourself.
Rules you write, versus rules you find
Ordinary programming means writing the rule and letting the computer apply it:
def is_spam(subject):
return "free money" in subject.lower()
print(is_spam("FREE MONEY now"))
print(is_spam("lunch tomorrow?"))
print(is_spam("f r e e m o n e y"))Output
True False False
That third line is the problem. Every rule you add gets worked around, and the list of rules grows for ever.
Machine learning turns it around. You supply examples that are already labelled, and the program works out the rule:
examples in rule out
"FREE MONEY now" -> spam
"lunch tomorrow?" -> not spam a model that scores
"f r e e m o n e y" -> spam any new subject line
...ten thousand moreNobody writes the rule down. Nobody can fully read it afterwards either, which is the trade.
The words
| Term | Means |
|---|---|
| features | the inputs, one row per example. Usually called X |
| label or target | the answer you want. Usually called y |
| model | the thing that maps features to a prediction |
| training | adjusting the model so it fits the examples |
| inference | using the trained model on new data |
| training set | the examples the model learns from |
| test set | examples it has never seen, used to judge it |
The convention of capital X and lowercase y is everywhere in Python ML code: X is a table, y is a single column.
The three kinds
Supervised learning. You have the answers for your examples, and want them for new ones. Nearly all practical ML is this.
- Classification — the answer is a category: spam or not, which of three
species, which digit.
- Regression — the answer is a number: a price, a temperature, a duration.
Unsupervised learning. No answers. You are looking for structure — grouping customers who behave alike, or spotting the odd one out.
Reinforcement learning. The program acts, gets a score, and adjusts. Games and robotics, mostly, and a different toolkit.
answers = [("spam or not", "classification"),
("house price", "regression"),
("group similar users", "clustering")]
for question, kind in answers:
print(f"{question:22} -> {kind}")Output
spam or not -> classification house price -> regression group similar users -> clustering
It is arithmetic on numbers
A model has no idea what a flower or an email is. It sees a grid of numbers and finds arithmetic that maps that grid to the answers. Everything else — turning words into numbers, scaling, encoding categories — is preparation you do first.
That is also why data quality dominates. A model trained on wrong labels learns the wrong rule perfectly.
When not to use it
Reach for a rule you write yourself when:
- The rule is known. Tax is a formula. Do not train a model to add up.
- You have very little data. Under a few hundred examples, a person writing
rules usually wins.
- A wrong answer is expensive and you must explain it. "The model said so"
is not a reason anyone accepts for a refused loan.
- The pattern will not repeat. Models assume tomorrow resembles yesterday.
Use it when the rule is real but nobody can write it down: which photos contain a cat, which transactions are fraud, which sentence comes next.
What the rest of this section does
The next chapters build a working model end to end, then take it apart: how the data has to be shaped, why you hold some back, how to tell a good model from one that has merely memorised, and the mistakes that make a model look far better than it is.
Test yourself
2 questionsWhat does machine learning give you that writing the rule yourself does not?
Show the answer
A rule found from examples, for cases where nobody can write one down — The trade is that nobody can fully read the rule afterwards either.
When should you not use machine learning?
Show the answer
When the rule is already known, such as a tax calculation — Also when data is scarce, when you must explain every decision, or when tomorrow will not resemble yesterday.
Your First Model
Load data, train, predict and score, in fifteen lines.