Rules vs learning: study notes

0.04 Rules vs learning: two ways to build smart software: detailed study notes

Learn AI by Story · Module 0: What is AI · Level: Beginner

The idea in one sentence

There are two ways to build smart software: write the rules yourself, or show the machine examples and let it learn the rules, and real products usually mix both.

The story from the video

Gopal's inbox is full of junk mail and he wants a spam filter. First he writes rules (if it says lottery, it is spam), but spammers write "l0ttery" and a friend's email about the office lottery is blocked by mistake. Then he collects ten thousand emails, labels each one spam or not spam, and lets an algorithm learn the patterns. The learned filter keeps improving as it sees more examples.

Key definitions

Term

Definition

In plain English

Rule-based system

Software whose behaviour is defined by explicit rules written by a person.

If this, then that.

Machine learning

Methods in which a system learns a rule or function from data.

The machine writes the rules.

Example (training example)

One piece of data used for learning, such as one email.

One row of experience.

Label

The correct answer attached to an example, such as spam or not spam.

The answer key.

Feature

A measurable property used as input, such as the number of links in an email.

A clue.

Model

The learned function that maps inputs to outputs.

The trained filter.

Training

The process of fitting the model to the examples.

Learning.

Inference (prediction)

Using the trained model on new data.

Using what it learned.

Generalisation

How well a model performs on new examples it has not seen.

Working on tomorrow's mail, not only yesterday's.

False positive

A harmless item wrongly flagged (a real email marked as spam).

A false alarm.

False negative

A harmful item missed (a spam email let through).

A miss.

Concept drift

When the world changes so that old patterns stop working.

Spammers change tricks.

Detailed explanation

1. Way one: write rules

A programmer or domain expert decides the logic and codes it. A simple spam filter might say: if the subject contains the word lottery, mark as spam; if it contains free money, mark as spam; if the sender is unknown and the message contains a link, mark as spam. The behaviour is fully predictable and easy to explain, and it needs no training data.

2. Why rules struggle

  • Exceptions: a friend writes about winning the office lottery and the rule blocks it (a false positive).
  • Evasion: spammers change the spelling to l0ttery and the rule misses it (a false negative).
  • Scale: to cover more cases you add more rules, and the rule set grows, conflicts with itself and becomes hard to maintain.
  • Hard-to-describe tasks: how would you write rules for recognising a face or understanding speech? Nobody can list them.

3. Way two: learn from examples

The steps:

  1. Collect data: for example ten thousand emails.
  2. Label it: mark each email spam or not spam. (This labelled approach is called supervised learning, covered in video 0.05.)
  3. Choose features and a model: the algorithm looks at clues such as words, links and sender history.
  4. Train: the algorithm adjusts the model to make fewer mistakes on the labelled examples.
  5. Evaluate: test on emails the model has not seen to check it generalises.
  6. Deploy and monitor: use it on new mail and keep checking, because spammers adapt.

4. Why more data helps

As the model sees more varied examples it finds more reliable patterns, so accuracy tends to rise with data, usually with diminishing returns. A fixed set of rules does not improve on its own: someone must edit it.

5. Trade-offs

Rules

Learning

Where the logic comes from

A human

Data

Data needed

None

Usually a lot

Explainability

Easy

Often harder

Handles messy, changing situations

Poorly

Well

Works when rules are known and fixed

Very well

Overkill

Typical examples

Tax calculation, form checks, safety interlocks

Spam, faces, speech, recommendations

6. Best of both

Real systems combine the two. Rules provide hard limits and guarantees, for example blocking a known bad sender immediately or never approving a payment over a legal limit without review. Learning handles the grey area, such as judging a new, tricky email.

What the video did not explain

  • Different ways of learning. Labelled examples are only one kind. Video 0.05 covers the five kinds of learning.
  • Measuring mistakes. Two kinds of error matter: false positives and false negatives. Which one costs more depends on the job: blocking a real email may be worse than letting one spam through, while in cancer screening a miss can be far worse than a false alarm.
  • Accuracy is not enough. If only 1 email in 100 is spam, a filter that says "not spam" for everything is 99 percent accurate and useless. You also look at precision and recall (covered later in the roadmap).
  • Training versus test data. You must test on data the model did not train on, or you will overestimate how good it is (video 2.07 covers train, validation and test splits).
  • Overfitting. A model can memorise the training examples instead of learning general patterns (video 2.08).
  • Bias in data. A learned model copies patterns in its data, including unfair ones, so data quality and fairness matter.
  • Concept drift. Models need monitoring and retraining when the world changes.
  • Rules can be learned too. Decision trees learn if-then rules from data, a bridge between the two worlds.

Worked example

Suppose you have 1,000 test emails, of which 100 are spam. Compare two filters.

Rule filter

Learned filter

Spam caught

60 of 100

90 of 100

Real emails wrongly blocked

30 of 900

18 of 900

The rule filter: 60 + (900 - 30) = 930 correct out of 1,000, so 93 percent accuracy. The learned filter: 90 + (900 - 18) = 972 correct, so 97.2 percent accuracy. These numbers are illustrative. Notice that you must look at both kinds of mistake, not only the single accuracy figure.

Now imagine spammers change tactics. The rule filter stays at 60 percent until someone edits the rules. The learned filter can be retrained on new examples and recover.

Common mistakes and myths

  • "Machine learning always beats rules." Not so. If the logic is clear and stable, rules are cheaper, simpler and fully explainable.
  • "Learning needs no human effort." People still collect, clean and label data, choose features and models, and check the results.
  • "More data always fixes everything." Poor-quality or biased data gives poor or biased models.
  • "A model that scores well on training data is good." Check on unseen data.
  • "Once trained, it works forever." The world changes, so monitor and retrain.

Where it is used

  • Rules: tax and payroll calculations, input validation, safety interlocks, legal compliance checks.
  • Learning: spam and fraud detection, face and speech recognition, product recommendations, language translation.
  • Both: banking fraud (rules for hard limits, learning for suspicious patterns), email security, medical decision support, search engines.

Self-check questions

  1. Who writes the logic in a rule-based system? A human.
  2. Where does the logic come from in machine learning? From the data.
  3. Why did the rule "lottery means spam" fail? It blocked a real email (false positive) and missed spelling tricks like l0ttery (false negative).
  4. List the steps for building a learned spam filter. Collect data, label, choose features and model, train, evaluate, deploy and monitor.
  5. What is a label? The correct answer attached to an example.
  6. What is generalisation? Performing well on new data the model has not seen.
  7. When are rules the better choice? When the logic is clear, fixed and must be explainable.
  8. Why is 99 percent accuracy possibly useless for spam? If almost all mail is not spam, saying "not spam" always scores high but catches nothing.
  9. What is concept drift? When the world changes and old patterns stop working.
  10. Give an example of a system that mixes both approaches. A bank fraud system with hard rules and a learned model for suspicious patterns.

Key takeaways

  • Rules: a human writes the logic. Learning: the machine finds it from examples.
  • Rules are clear and explainable but brittle; learning is flexible but needs data.
  • When the rules are too hard to write, show examples instead.
  • Evaluate on unseen data and watch both kinds of error.
  • Real products combine both.

Go deeper and what next

  • Pick a task you do every day and decide if you could write its rules, or if examples would be easier.
  • Look up precision and recall for spam filtering.
  • Next in the roadmap: 0.05 The five kinds of learning.

Comments

Popular posts from this blog

What is AI? Narrow vs General Intelligence (Study Notes)

The five kinds of learning: study notes