Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

ML vs. Rule-Based Systems: When to Use Which

Recommended resources

Introduction

Welcome to your next lesson in our course. In the previous lesson, we established the core vocabulary of machine learning: features, labels, data splits, and the critical concepts of overfitting and underfitting. We learned that the goal is to build models that generalize well to new data.

Now, we'll take a step back and address a more fundamental question: should we even be using machine learning? This lesson directly addresses the learning outcome: Identify when to use ML vs. rule-based approaches for a given problem. This is a crucial first decision in any project and a frequent topic in ML engineering interviews and certification exams.

We will explore:

  • The fundamental differences between rule-based systems and ML models.
  • A decision framework for when to choose one over the other.
  • Google's own "Rules of ML" that provide guidance on this choice.

1. Traditional Programming vs. Machine Learning

At its core, the choice between a rule-based system and a machine learning model is a choice between two different programming paradigms.

  • Rule-Based (Heuristic) Systems: You, the developer, explicitly write the logic. The system is a collection of if-then-else statements that encode domain knowledge to make decisions. It's deterministic and transparent.
  • Machine Learning Systems: You provide data (features and labels), and an algorithm learns the logic. The system discovers patterns and relationships on its own. It's probabilistic and often opaque (a "black box").

This short video provides an excellent high-level overview of these two approaches.

AI Systems vs Traditional Coding

Let's watch this clip from IBM Technology to understand the fundamental differences in how these two types of systems are built and how they behave.

Watch from 03:08 to 06:51. Pay attention to the comparison across three key aspects: stability and scalability, control and transparency, and learning and data handling.

As the video highlights, traditional rule-based programming is stable and transparent but doesn't scale to complex problems. ML is scalable and can learn from vast amounts of unstructured data but can be less stable and harder to interpret.


2. The Decision Framework: When to Use Which Approach

So, how do you decide? The most important guideline, especially for the Google Professional Machine Learning Engineer exam, comes directly from Google's own best practices.

Let's read the two most important rules for our current topic.

Rules of Machine Learning: | Google for Developers

Google has published a well-regarded guide called 'Rules of Machine Learning'. The very first rule, and the third, provide a powerful framework for making our decision.

In the 'Before Machine Learning' section, please read 'Rule #1: Don’t be afraid to launch a product without machine learning' and 'Rule #3: Choose machine learning over a complex heuristic.'

These two rules give us a clear sequence:

  1. Start with a simple heuristic if you can.
  2. Move to ML only when the heuristic becomes too complex to maintain.

Let's break down the scenarios for each.

When to Use a Rule-Based (Heuristic) Approach

You should strongly favor a simple, rule-based approach when:

  • The problem logic is simple: The rules can be clearly defined and are not excessively numerous. For example, flagging a transaction as suspicious if it's from a new country and over a certain amount.
  • You have little to no data: ML models are data-hungry. If you're launching a new product, you won't have the historical data needed for training. A simple heuristic can get you started.
  • Complete transparency is required: In regulated fields like finance or healthcare, you must be able to explain exactly why a decision was made. A rule-based system's logic is self-evident.
  • You need a quick baseline: A simple heuristic is fast to implement and provides a performance benchmark. Any future ML model must prove it can significantly outperform this baseline to justify its complexity.

This article provides excellent real-world examples of simple heuristics that are surprisingly effective.

The First Rule of Machine Learning - ApplyingML

Let's look at some examples of effective heuristics from the 'ApplyingML' blog, which reinforces Google's Rule #1.

Read the sections 'What should we start with then?' and 'When should we use machine learning then?'. Notice the concrete examples for recommendations, classification, and spam identification.

When to Use a Machine Learning Approach

You should consider moving to machine learning when:

  • The problem is too complex for rules: The patterns are too nuanced or involve too many interacting variables to be captured by if-then logic. Think of image recognition, voice transcription, or identifying sophisticated financial fraud.
  • Your simple heuristic becomes a "complex heuristic": As the Rules of ML article states, a complex heuristic is unmaintainable. If you find yourself managing hundreds of fragile, interconnected rules, it's time for ML. An ML model is often easier to update (by retraining with new data) than debugging a complex rule system.
  • The system needs to adapt: If the underlying patterns change over time (e.g., spammers change their tactics), a rule-based system requires constant manual updates. An ML model can be retrained on new data to adapt automatically.
  • Personalization is key: It's impossible to write rules to personalize content for millions of individual users. This is a classic use case for ML.

The image below gives a great visual summary of the two approaches in the context of fraud detection. The rule-based side relies on explicit conditions, while the ML side learns patterns from historical data.

Rule-based vs. Machine Learning Fraud Detection Model
This diagram contrasts a rule-based approach, which uses explicit 'if/then' rules for detection, with a machine learning approach, which identifies patterns and anomalies from data and relies on ongoing model tuning.

3. Summary: A Quick-Reference Table

Here is a summary of the decision criteria, tailored for quick review. This is the kind of condensed knowledge that is highly valuable for the certification exam.

Factor Favor Rule-Based System Favor Machine Learning System
Problem Complexity Simple, well-defined logic. Can be solved with a few rules. Complex, nuanced patterns with many interacting variables.
Data Availability Low or none. Perfect for new products or features. High volume of relevant, labeled data is available.
Maintainability Easy for simple rules. Unmaintainable for complex rules. Easier to update/retrain than to manage complex rules.
Interpretability High. Decisions are transparent and easily explained. Low. Often a "black box," requiring special tools to interpret.
Adaptability Low. Rules must be manually changed to adapt to new patterns. High. Can adapt by retraining on new data.

4. It's Not Always Either/Or

Finally, remember that these two approaches can work together. You don't always have to make an absolute choice.

  • Heuristics as Features: Your domain knowledge can be encoded into features for an ML model. For instance, you could create a boolean feature is_account_new based on a rule (e.g., account age < 7 days). The ML model can then learn the weight or importance of this feature.
  • Hybrid Systems: A rule-based system can handle the obvious, clear-cut cases, while an ML model can handle the more ambiguous ones that are flagged by the rule-based system.

Conclusion

You now have a solid framework for deciding when to apply machine learning versus a simpler rule-based system. This strategic decision is the first step in responsible and effective ML engineering.

Key Takeaways:

  • Google's Rule #1: "Don't be afraid to launch a product without machine learning." Start with a simple heuristic if the problem is well-defined, you lack data, or you need a quick baseline.
  • Google's Rule #3: "Choose machine learning over a complex heuristic." Move to ML when your rules become too complex and brittle to maintain, or when the problem requires adaptation and personalization at scale.
  • The choice is a tradeoff between simplicity/transparency (rules) and scalability/adaptability (ML).

In our last lesson, we identified overfitting as a key problem in ML. In choosing to use ML, we are inherently taking on the challenge of building a model that generalizes well. The next lesson will dive into the core theoretical concept that governs this challenge: the bias-variance tradeoff. We'll explore how these two types of error relate to underfitting and overfitting, providing a deeper understanding of how to build effective models.