What learning is · lesson 1 of 13
Parts in this lesson
Adef rules(text): ...rules written by hand
Bweight[w] += 1rules learned from labelled examples
Cscore(program, messages)measuring both the same way

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.

rules.ipynb

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.

learned.ipynb

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:

Two programsthe twelve messages they were built from
MessageReallyRulesLearned
WIN a FREE phone! Click the link nowspamspam ✓spam ✓
Congratulations, you won 10,000 pesos. Send your PIN to claimspamspam ✓spam ✓
FREE load for you, reply YES to claimspamspam ✓spam ✓
Your GCash account is locked. Click the link to verifyspamspam ✓spam ✓
Claim your prize today, limited offerspamspam ✓spam ✓
Earn 5,000 a day from home, click herespamspam ✓spam ✓
Are we still meeting at 3 for the project?okok ✓ok ✓
Ma, I’ll be home late, traffic on EDSAokok ✓ok ✓
Can you send me the notes from class?okok ✓ok ✓
Happy birthday! See you at lunch on Sundayokok ✓ok ✓
The jeep is full, I’ll take the next oneokok ✓ok ✓
Thanks for the adobo recipe, it was perfectokok ✓ok ✓
Right12 of 1212 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.

new.ipynb

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 E with respect to some task T and performance measure P if its performance at T, as measured by P, improves with experience E.

For the spam filter:

  • Task T: label each message spam or ok.
  • Experience E: messages that people have already labelled.
  • Performance P: 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

Which of these is a program that learns?
A model that reads chest X-rays is trained on 50,000 X-rays labelled by doctors, and judged by how many new X-rays it labels the way a doctor would. What is the experience E?
Predict, then check with the figure: both spam filters were right on all 12 messages they were built from. What does that tell you about new messages?

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.

All sources for Simple Machine Learning