Rules vs Learning
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.
What should you notice?
In hand-written mode, moving the slider changes the rule because you changed it.
In learning mode, the visual chooses a threshold from the labeled examples using a simple objective: make as few mistakes as possible on this tiny dataset.
Both modes can produce the same threshold. That does not make them the same process.
Learning is not automatically better
It is tempting to think that a learned model is always more advanced and therefore always better. That is not true.
Suppose your product rule is:
Lock an account after 10 failed password attempts.
If that threshold comes from a clear security policy, replacing it with a learned model might make the behavior harder to explain, harder to audit, and harder to guarantee in rare cases.
On the other hand, suppose you are trying to recognize handwritten digits. Writing a complete set of rules such as “if these pixels are dark and those curves point this way, call it a 3” becomes very difficult. Showing many labeled examples and learning from them may be much more practical.
A useful engineering question is therefore:
Is the desired behavior easy to specify directly, or is it easier to show through examples?
What happens when one threshold is not enough?
Consider this dataset:
| Suspicion score | Label |
|---|---|
| 2 | Not spam |
| 3 | Spam |
| 4 | Not spam |
| 5 | Spam |
| 6 | Not spam |
| 7 | Spam |
No single threshold can separate these labels perfectly.
That does not prove learning has failed. It may mean the input is missing useful information, the labels are noisy, or a one-threshold model is simply too limited for the pattern.
Before reaching for a bigger model, ask:
- Are the labels trustworthy?
- Does the input contain enough information?
- Can this model type represent the pattern?
- Are we measuring the mistakes we actually care about?
This is an early version of a debugging habit you will use throughout machine learning.
Optional code Lab: compare two thresholds
The visual is the main activity for this lesson. The Lab below shows the same idea in code for learners who want to see how the comparison is computed.
You do not need to understand every function yet.
First, click Run and look for two output lines:
hand-written threshold:learned threshold:
Now make one exact edit:
- Find the line
manual_threshold = 4. - Before editing, predict how many mistakes threshold 6 will make.
- Change only
4to6. - Click Run again.
- Compare
hand-written thresholdand itsmistakescount with thelearned thresholdand itsmistakescount.
The manual threshold should now make zero mistakes, while the learned threshold remains 5 because the examples and learning rule did not change. This is useful evidence: two different thresholds can match the same training examples perfectly even though they came from different processes.
Loading lab…
A little more technical: parameters and objectives
A model is a function with adjustable settings called parameters.
In this lesson, the model has only one adjustable parameter: the threshold.
An objective is a rule for deciding what counts as better. Here the objective is simple: choose the threshold with the fewest mistakes on the examples.
Later, models will have many more parameters and more complicated objectives. But the core pattern stays recognizable:
examples provide evidence → an objective defines better/worse → an algorithm adjusts parameters
You do not need to memorize that sentence yet. The important part is understanding that “learning” means the settings are being chosen using data and a goal, rather than typed in one by one by a person.
Quick Check
Key Takeaways
- A hand-written rule gets its decision setting directly from a person.
- A learned model uses examples and an objective to choose adjustable settings.
- The final rule can look similar even when the setting was chosen in a different way.
- Learning is not automatically better than a clear, reliable rule.
- When a learned rule looks wrong, inspect data, labels, available inputs, model assumptions, and the objective before simply making the model larger.
Next Lesson
Next, you will learn to separate the information a predictor receives from the target answer it is supposed to predict. That feature-versus-label distinction will make later training and evaluation experiments much easier to reason about.
References
- Google for Developers, Introduction to Machine Learning.
- scikit-learn, Getting Started.
Completion is stored locally on this device.