Rules vs Learning
About 15 min
Goal
By the end of this lesson, you can explain the difference between a hand-written rule and a learned model setting, and describe why learning is not automatically better than a clear rule.
Two systems can make the same kind of decision
Imagine a spam filter that uses one number called a suspicion score.
A low score means the message looks less suspicious. A high score means it looks more suspicious.
We want the system to answer one question:
Should this message be marked as spam?
One simple way to make the decision is to choose a threshold.
For example:
If suspicion score ≥ 5, predict spam.
The number 5 is an adjustable setting. If we change it to 3 or 7, some messages will receive different predictions.
That is why this lesson asks a question that may seem unusual at first:
Where did the threshold come from?
The final rule may look exactly the same, but there are two very different ways the number 5 could have been chosen.
Option 1: a person chooses the rule
A person might decide that 5 is a sensible threshold after reading a policy, looking at past cases, or choosing a cautious safety boundary.
The rule could be written in Python like this:
def is_spam(suspicion_score):
return suspicion_score >= 5
The computer follows the rule exactly. The number 5 came directly from a person.
This can be a good engineering choice when the desired behavior is clear, stable, easy to test, and easy to explain.
Option 2: examples are used to choose the setting
Now imagine that instead of choosing the threshold ourselves, we have labeled examples:
| Suspicion score | Correct label |
|---|---|
| 1 | Not spam |
| 2 | Not spam |
| 4 | Not spam |
| 6 | Spam |
| 7 | Spam |
| 9 | Spam |
A simple learning process can try several possible thresholds and count how many mistakes each one makes.
With the rule “predict spam when score ≥ threshold,” the counts are:
| Threshold | Mistakes | Why |
|---|---|---|
| 4 | 1 | Score 4 is incorrectly marked as spam. |
| 5 | 0 | Scores 1, 2, and 4 stay not-spam; 6, 7, and 9 are spam. |
| 6 | 0 | It makes the same predictions as threshold 5 on these six examples. |
Notice something important: more than one setting can fit the observed examples equally well. The data above does not prove whether threshold 5 or threshold 6 will be better on future messages. A learning procedure therefore needs both an objective and, when there is a tie, a clear tie-breaking rule.
In this tiny example, learning means using examples and a measure of mistakes to choose a threshold. Our code Lab breaks ties by choosing the smaller threshold, so it selects 5.
The output rule may still look simple:
If suspicion score ≥ 5, predict spam.
But now the setting 5 came from data and an objective instead of being typed in directly by a person.
Same final rule, different path to get there
Put the two cases side by side:
Hand-written path
person chooses 5
↓
threshold = 5
↓
if score ≥ 5 → spam
Learning path
labeled examples + "make fewer mistakes"
↓
try possible thresholds
↓
threshold = 5
↓
if score ≥ 5 → spam
Both systems can end with the same visible rule. The difference is how the adjustable setting was chosen:
- in the first system, a person directly supplied
5; - in the second system, a learning process used examples and an objective to select
5.
That difference also changes what evidence you inspect when something goes wrong. A hand-written threshold points you toward the policy or reasoning used to choose it. A learned threshold also requires you to inspect the examples, labels, and objective that caused the learning process to prefer it.
That is the central idea: learning changes where model settings come from, not necessarily what the final rule looks like.
Explore the difference visually
The visual below shows the same labeled examples in two modes.
Start with Hand-written rule.
- Move the Your threshold slider to
4.0. - Read the status line at the top and notice how many examples are correct.
- Move the threshold to
6.0and notice which examples change from match to mismatch or back again. - Now click Learn from examples.
- Read the threshold shown in the status line for the learned rule.
Ask yourself:
When I moved the slider, who chose the threshold? When I switched to learning mode, what chose the threshold?
Choose a rule or learn a threshold
The hand-written rule uses a threshold you choose. Learning searches the labeled examples for a threshold that makes fewer mistakes.