Knowledge & Reasoning (Expert Systems)
Store facts and IF–THEN rules, then let the computer combine them to reach new conclusions.
What you will learn
- Represent facts and rules
- Build a tiny rule-based “expert system”
- See the strengths and limits of rules
Facts + rules = reasoning
A knowledge-based (or expert) system stores two things: facts about the situation, and rules that say how to draw conclusions. The computer then applies the rules to the facts to work out something new.
- Fact: “the patient has a high temperature.”
- Rule: “IF high temperature AND cough THEN possible flu.”
A tiny symptom checker
Let us encode a few medical-style rules and reason over a set of symptoms — a classic expert system.
facts = {'fever', 'cough'} # what we know about the patient
rules = [
({'fever', 'cough'}, 'possible flu'),
({'sneezing', 'runny nose'},'possible cold'),
({'fever', 'rash'}, 'see a doctor'),
]
def diagnose(facts, rules):
found = []
for needed, conclusion in rules:
if needed.issubset(facts): # are all required symptoms present?
found.append(conclusion)
return found or ['no rule matched']
print(diagnose(facts, rules))Note: Output: ['possible flu'] The patient has fever and cough, so the first rule fires. Add 'rash' to the facts and the “see a doctor” rule fires too.
Strengths and limits
| Strengths | Limits |
|---|---|
| Easy to read and explain | A human must write every rule |
| Predictable and reliable | Cannot handle messy, fuzzy data well |
| Great when rules are known | Breaks on cases nobody wrote a rule for |
Expert systems are still used (tax software, safety checklists). But writing rules for everything — like recognising a cat in a photo — is impossible. That limit is exactly why machine learning took over: instead of writing rules, we let the computer learn them from data. That is Unit 4.
Tip: A rule “fires” when all its conditions are true. Real expert systems chain rules together — one rule’s conclusion becomes a fact another rule uses.
Q. What is the main limitation of a rule-based expert system?
✍️ Practice
- Add a rule: IF
sore throatANDfeverTHEN “possible strep”. Test it. - Build a 3-rule expert system that recommends what to wear based on weather facts.
🏠 Homework
- List one real product that uses hand-written rules and one that clearly must learn from data. Explain the difference.