Knowledge & ReasoningCore· 35 min read

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.

A rule-based expert system: it fires every rule whose conditions are met
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

StrengthsLimits
Easy to read and explainA human must write every rule
Predictable and reliableCannot handle messy, fuzzy data well
Great when rules are knownBreaks 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?

Answer: Expert systems rely on rules written by people. For tasks like image recognition you cannot write all the rules — so machine learning is used instead.

✍️ Practice

  1. Add a rule: IF sore throat AND fever THEN “possible strep”. Test it.
  2. Build a 3-rule expert system that recommends what to wear based on weather facts.

🏠 Homework

  1. List one real product that uses hand-written rules and one that clearly must learn from data. Explain the difference.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →