What machine learning is
Last updated
Most programs follow rules a person wrote. A machine learning program writes some of its own rules, from examples. This lesson puts the two side by side on the same job, catching spam text messages, and shows where each one breaks. Then it gives the definition the rest of the course builds on, and says where machine learning helps and where it shouldn't be used at all.
By the end of this lesson you can
- Tell a program with hand-written rules from one that learns its rules from examples
- Name the task, the experience and the measure of performance for a learning problem
- Say when machine learning is a good fit, and when it isn’t
Rules by hand, and rules from examples
Say you want a program that sorts text messages into spam and ok. There are two ways to write it.
By hand. You read some spam, notice it often says "free", "win", "click", "claim" or "prize", and write a rule: if a message has one of those words, it's spam. Every rule comes from your head.
From examples. You collect messages that people have already labelled, and let the program work out its own rule. Here's about the simplest way: give every word a weight, one point for each spam message it appears in and minus one for each ok message. A new message is spam if its words add up to more than 0.
Nobody told it that "claim" means spam; it counted. It also gave weight to "to" and "your", which says more about these twelve messages than about spam. With twelve examples it learns some nonsense; with thousands, the nonsense mostly cancels out. That's the trade machine learning makes: you supply examples instead of rules, and the quality of the rule depends on the quality and number of examples.
Both programs get all twelve messages right. Switch the figure to the new messages to see what that's worth:
| Message | Really | Rules | Learned |
|---|---|---|---|
| WIN a FREE phone! Click the link now | spam | spam ✓ | spam ✓ |
| Congratulations, you won 10,000 pesos. Send your PIN to claim | spam | spam ✓ | spam ✓ |
| FREE load for you, reply YES to claim | spam | spam ✓ | spam ✓ |
| Your GCash account is locked. Click the link to verify | spam | spam ✓ | spam ✓ |
| Claim your prize today, limited offer | spam | spam ✓ | spam ✓ |
| Earn 5,000 a day from home, click here | spam | spam ✓ | spam ✓ |
| Are we still meeting at 3 for the project? | ok | ok ✓ | ok ✓ |
| Ma, I’ll be home late, traffic on EDSA | ok | ok ✓ | ok ✓ |
| Can you send me the notes from class? | ok | ok ✓ | ok ✓ |
| Happy birthday! See you at lunch on Sunday | ok | ok ✓ | ok ✓ |
| The jeep is full, I’ll take the next one | ok | ok ✓ | ok ✓ |
| Thanks for the adobo recipe, it was perfect | ok | ok ✓ | ok ✓ |
| Right | 12 of 12 | 12 of 12 | |
Both get all twelve right. That proves little: the rules were written while looking at these messages, and the learned filter learned from them.
Wrong way: a rule that breaks on the first new message
Scoring a program on the messages you built it from proves very little: you wrote the rules while looking at them, or the program learned from them. The real test is new messages.
The hand-written rules get 2 of 6. "Fr3e l0ad" slips past the word "free"; the parcel scam uses none of the five words; and two friends' messages are flagged for saying "Free tonight?" and "Click the link". Spammers change their wording every week, so the rules need rewriting every week.
The learned filter gets 4 of 6. It's better, not because it's clever, but because it weighed every word it saw, so "claim", "you" and "pesos" pushed new spam over the line. It still flags both friends. With more examples it would improve on its own; the rules only improve when someone rewrites them.
This "score it on new data" habit is the most important one in the course. Lesson 2.5 makes it a routine.
Task, experience and performance
Tom Mitchell's 1997 definition is the one most textbooks quote: a program learns from experience with respect to some task and performance measure if its performance at , as measured by , improves with experience .
For the spam filter:
- Task : label each message spam or ok.
- Experience : messages that people have already labelled.
- Performance : the share of new messages it labels right.
Writing these three down is the first step of every project in this course, and the questions are harder than they look. Which messages count as spam? Who labelled the examples, and did they agree? Is "share right" the measure you care about, or is it worse to lose a real message than to let some spam through? Lesson 2.8 comes back to that last one.
In code, "experience" means the data you fit a model to, and "task" means what it does when you ask it to predict. Every scikit-learn model from lesson 2.4 on has a fit method and a predict method, for exactly those two things.
Where machine learning is used, and where it shouldn’t be
Machine learning is a good fit when:
- the rule is too hard to write down, but examples are easy to collect: recognising a face, transcribing speech, translating a sentence;
- the rule keeps changing: spam, fraud, what people want to watch next;
- there are many examples of the right answer, and a few mistakes are acceptable.
It's a bad fit, or needs great care, when:
- a simple rule works: if a store's discount is "10% off over ₱1,000", write that, don't learn it;
- mistakes are costly and hard to undo, and the people affected can't question them: who gets bail, a loan, a job, a medical treatment. A model trained on past decisions copies their unfairness, and Stage 17 shows how;
- the examples don't match the real world: a model learns only what its data shows. Faces mostly of one group, or speech mostly in one accent, give a model that works worst for everyone else;
- you need to explain every decision exactly, and nobody can.
A good rule of thumb: try the simplest thing first, a rule, an average, a lookup, and use machine learning only if it clearly does better on new data.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 5: Machine Learning Basics: learning algorithms, and Mitchell’s definition of learning in terms of a task, experience and performance (5.1)
- Prince, Understanding Deep Learning: chapter 1, an introduction to the kinds of problem machine learning solves
- scikit-learn: Getting Started: the fit-and-predict pattern every model in Stages 2 to 4 follows