Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Machine learning vs rule-based systems
📖 Scenario: You are working on a simple AI assistant that can decide how to respond to user questions. You want to understand the difference between machine learning and rule-based systems by creating examples of each.
🎯 Goal: Build two simple examples: one using a rule-based system with fixed rules, and one using a machine learning style approach with data and a simple decision logic. This will help you see how each system works in practice.
📋 What You'll Learn
Create a dictionary called rules with fixed question-answer pairs
Create a list called training_data with example questions and answers
Write a function called rule_based_response that uses the rules dictionary to find answers
Write a function called ml_style_response that uses simple matching on training_data to find answers
💡 Why This Matters
🌍 Real World
Understanding the difference between rule-based and machine learning systems helps when designing AI assistants, chatbots, or decision-making software.
💼 Career
Many AI and software development jobs require knowledge of how to implement and choose between rule-based logic and machine learning models.
Progress0 / 4 steps
1
DATA SETUP: Create the rule-based system dictionary
Create a dictionary called rules with these exact entries: 'hello': 'Hi there!', 'bye': 'Goodbye!', and 'thanks': 'You are welcome!'.
AI for Everyone
Hint
Use curly braces {} to create a dictionary with keys and values.
2
CONFIGURATION: Create the machine learning style training data
Create a list called training_data with these exact tuples: ('hello', 'Hi there!'), ('bye', 'Goodbye!'), and ('thanks', 'You are welcome!').
AI for Everyone
Hint
Use square brackets [] to create a list of tuples.
3
CORE LOGIC: Write the rule-based response function
Write a function called rule_based_response that takes a parameter question and returns the answer from the rules dictionary if the question exists, or 'I do not understand.' if it does not.
AI for Everyone
Hint
Use an if statement to check if the question is a key in the rules dictionary.
4
COMPLETION: Write the machine learning style response function
Write a function called ml_style_response that takes a parameter question and searches the training_data list for a matching question. If found, return the answer; otherwise, return 'I do not understand.'. Use a for loop with variables q and a to iterate over training_data.
AI for Everyone
Hint
Use a for loop to check each tuple in training_data for a matching question.
Practice
(1/5)
1. Which of the following best describes a machine learning system compared to a rule-based system?
Machine learning systems learn from examples and improve with more data.
Step 2: Compare with rule-based systems
Rule-based systems follow fixed instructions and do not adapt.
Final Answer:
It learns from data and adapts over time. -> Option C
Quick Check:
Machine learning = adapts [OK]
Hint: Machine learning adapts; rule-based does not [OK]
Common Mistakes:
Confusing fixed rules with learning
Thinking rule-based systems adapt
Assuming machine learning cannot improve
2. Which syntax correctly describes a rule-based system?
easy
A. train_model(data) to predict temperature
B. if temperature > 30 then turn_on_fan() else turn_off_fan()
C. learn_from_data(data) to adjust fan speed
D. update_rules_based_on_feedback()
Solution
Step 1: Identify rule-based syntax
Rule-based systems use fixed if-then rules like 'if temperature > 30 then turn_on_fan()'.
Step 2: Check other options
Options A, C, and D describe learning or updating, which are machine learning concepts.
Final Answer:
if temperature > 30 then turn_on_fan() else turn_off_fan() -> Option B
Quick Check:
Rule-based = fixed if-then rules [OK]
Hint: Rule-based uses fixed if-then rules [OK]
Common Mistakes:
Confusing learning functions with rules
Choosing options that imply adaptation
Ignoring fixed condition-action format
3. Consider this simple system: rules = {'hot': 'turn_on_ac', 'cold': 'turn_on_heater'} def apply_rule(temp): if temp > 25: return rules['hot'] else: return rules['cold'] print(apply_rule(30)) What will this print?
medium
A. Error
B. turn_on_heater
C. null
D. turn_on_ac
Solution
Step 1: Analyze the input and condition
Input temperature is 30, which is greater than 25, so the 'hot' rule applies.
Step 2: Determine the returned action
The function returns rules['hot'], which is 'turn_on_ac'.
Final Answer:
turn_on_ac -> Option D
Quick Check:
Temp 30 > 25 -> 'turn_on_ac' [OK]
Hint: Check condition then pick matching rule [OK]
Common Mistakes:
Choosing 'turn_on_heater' ignoring condition
Assuming function returns null
Thinking code causes error
4. This code tries to use a rule-based system but has a bug: rules = {'hot': 'turn_on_ac', 'cold': 'turn_on_heater'} def apply_rule(temp): if temp > 25: return rules['hot'] elif temp <= 25: return rules['cold'] print(apply_rule(25)) What is the bug and how to fix it?
medium
A. Bug: 'elif' should be 'else'; fix by replacing 'elif' with 'else'.
B. Bug: Missing rule for temp=25; fix by adding 'temp == 25' rule.
C. Bug: KeyError on 'cold'; fix by adding 'cold' key to rules.
D. Bug: Function does not return anything; fix by adding return statement.
Solution
Step 1: Identify condition overlap
The code uses 'elif temp <= 25', but temp=25 matches this condition. However, since 'if temp > 25' fails implies 'temp <= 25', the elif is redundant.
Step 2: Check if 'elif' is necessary
Since the first condition is 'temp > 25', the else branch can cover all other cases, so 'else' is simpler and clearer.
Final Answer:
Bug: 'elif' should be 'else'; fix by replacing 'elif' with 'else'. -> Option A
Quick Check:
Use else for remaining cases [OK]
Hint: Use else for all other cases, not elif [OK]
Common Mistakes:
Thinking temp=25 is missing
Assuming KeyError occurs
Believing function lacks return
5. You want to build a system that detects spam emails. The rules for spam change often and new patterns appear regularly. Which approach is best and why?
hard
A. Use machine learning because it can learn new spam patterns from data.
B. Use a rule-based system because rules are easy to write and fixed.
C. Use a rule-based system because it never makes mistakes.
D. Use machine learning because it requires no data to work.
Solution
Step 1: Understand problem requirements
Spam patterns change often, so fixed rules will become outdated quickly.
Step 2: Choose approach based on adaptability
Machine learning can learn from new data and adapt to new spam patterns automatically.
Final Answer:
Use machine learning because it can learn new spam patterns from data. -> Option A