Module 1 · Why Machine Learning Still Needs You ⏱ 19 min

What Machine Learning Actually Is

By the end of this lesson you will be able to:
  • Define machine learning as fitting a function to examples instead of writing explicit rules
  • Build a rule-based classifier and see where the rules become unmaintainable
  • Write a simple mean predictor that learns its parameter from data
  • Explain why learning is just searching for parameters that reduce a loss

Some problems are easy to solve with a checklist. Is the email spam? Look for the word 'free', check for all-caps, count exclamation marks. That is rule-based programming: a human writes the logic, and the computer follows it exactly. The result is predictable, explainable, and fast. You can look at the code and know exactly why a decision was made.

But what happens when the rules start to contradict each other, or when there are thousands of them, or when the right rule changes from week to week? Writing rules by hand becomes exhausting, error-prone, and eventually impossible. The system grows fragile: one new edge case demands a patch, and that patch breaks older cases.

Machine learning is what you reach for when the rules are too numerous, too subtle, or too fluid for a human to maintain. Instead of writing the function, you write a program that finds the function by looking at examples. The human still decides the shape of the solution, but the specifics are extracted from data.

flowchart LR
  A["Human writes rules"] --> B["Program applies them"]
  C["Human writes search"] --> D["Data provides examples"]
  D --> E["Program finds rules"]
  style A fill:#b45309,color:#fff
  style C fill:#166534,color:#fff
Rule-based: human writes every rule. ML: human writes the search, data supplies the specifics.

At its core, machine learning is fitting a function to examples. You collect inputs and their known outputs — emails labeled spam or not spam, houses with their sale prices, images with their contents — and you search for a function that turns the inputs into the correct outputs. The function is not written line by line. It is produced by an algorithm that tweaks internal numbers, called parameters, until its predictions line up with the real answers. The better the alignment, the lower the loss — a single number that measures how wrong the current function is. Training means repeating three steps: make a prediction, measure the loss, then adjust the parameters to reduce it. Every pass through the data is called an epoch, and after enough epochs the function fits the examples well enough to be useful. The art is choosing a model family that can represent the true pattern without being so flexible that it memorises noise.

A rule-based spam scorer: hardcoded keywords. It never learns.
def rule_spam_score(text):
    score = 0
    text_lower = text.lower()
    if "free" in text_lower:
        score += 2
    if "winner" in text_lower:
        score += 3
    if "!!!" in text:
        score += 1
    return score

print(rule_spam_score("You are a winner!!!"))
print(rule_spam_score("Free shipping today"))

The rule-based scorer works until it does not. A spammer writes 'w1nner' and the keyword misses. A legitimate store writes 'FREE returns policy' and the score jumps. Every new trick requires a new rule, and every new rule risks breaking something else. The human maintainer is playing whack-a-mole against an adversary. Machine learning offers a different contract: instead of hardcoding 'winner' as spammy, show the system thousands of labeled emails and let it discover which words and patterns correlate with the label. The model generalises to variations it has never seen, because it learned the concept rather than memorising the keyword. The engineer still designs the overall structure, but the detailed behaviour is inferred from data. That trade-off — giving up perfect explainability for adaptive coverage — is the central bargain of applied machine learning.

flowchart TD
  A["Examples<br/>(data)"] --> B["Function family<br/>(model)"]
  B --> C["Wrongness measure<br/>(loss)"]
  C --> D["Parameter search<br/>(learning)"]
  D --> B
  style A fill:#2563eb,color:#fff
  style C fill:#b45309,color:#fff
  style D fill:#166534,color:#fff
The three ingredients of every ML system: data, a model family, and a loss to minimise.
A tiny predictor that learns the mean from examples. Run it and watch the guess improve.
data = [10, 12, 14, 16, 18]

def loss(guess, targets):
    return sum((guess - t) ** 2 for t in targets) / len(targets)

guess = 0.0
lr = 0.1
for step in range(1, 21):
    grad = (2 / len(data)) * sum(guess - t for t in data)
    guess = guess - lr * grad
    if step % 5 == 0:
        print("step", step, "guess =", round(guess, 2))

The loop above is doing the same thing gradient descent does, just with one parameter: the mean. It starts at zero, measures which way the loss slopes, and nudges the guess downhill. After a few steps the guess is close to 14 — the actual mean of the data. This is learning in miniature. There is no magic, just arithmetic repeated until the error stops shrinking. The 'learning' is really a search: explore the space of possible guesses, measure the loss at each one, and keep the directions that make it smaller. When people say a model learned from data, they mean it ran a search like this one and settled on parameters that fit the examples it was shown. The search space can have millions of dimensions, but the idea is identical: follow the gradient toward lower loss. The only difference between this toy and a neural network is scale.

flowchart LR
  A["Bad guess<br/>high loss"] -->|"gradient<br/>points uphill"| B["Step opposite<br/>lower loss"]
  B --> C["Repeat"]
  C --> D["Good guess<br/>low loss"]
  style A fill:#b91c1c,color:#fff
  style D fill:#166534,color:#fff
Learning as search: start with a bad guess, follow the slope downhill, repeat.

This is why machine learning is not a replacement for thinking. You still need to choose the right model family, prepare the data carefully, and interpret the results. The algorithm finds the parameters, but it cannot tell you whether the question you asked was sensible or whether the data you collected was biased. Machine learning automates the search, not the judgment. A model trained on incomplete data will confidently produce wrong answers, and because the rules are hidden inside the parameters, the mistake can be hard to spot. The engineer's job is to set up the search so that success in the search actually means success in the real world.

Exercise

What does this print? The mean predictor from the lesson, run for one step.

data = [10, 20]
guess = 0.0
lr = 0.5
grad = (2 / len(data)) * sum(guess - t for t in data)
guess = guess - lr * grad
print(round(guess, 1))
Exercise

Write rule_score(text) that returns 1 if the lowercase text contains 'alert', otherwise 0.

def rule_score(text):
    # return 1 if 'alert' is in text.lower(), else 0
    pass
Exercise

Write mean_predictor(data) that returns the arithmetic mean of a non-empty list data.

def mean_predictor(data):
    # return the average of the numbers
    pass
Exercise

This loss function is meant to compute the average squared error between a guess and a list of targets, but it squares the wrong thing. Fix the expression inside the loop.

def loss(guess, targets):
    total = 0
    for t in targets:
        total += guess - t ** 2   # bug: squares t instead of the difference
    return total / len(targets)
Exercise

Write approach_advice(example_count, rule_count) that returns the string 'use_ml' if example_count is at least 100 AND rule_count is greater than 20, otherwise returns 'use_rules'. This models the senior decision of when to switch approaches.

def approach_advice(example_count, rule_count):
    # return 'use_ml' or 'use_rules'
    pass

Recap

  • Machine learning fits a function to examples instead of requiring a human to write every rule.
  • A rule-based system is precise and explainable, but it collapses under too many rules or an adversary that evolves.
  • An ML system needs three ingredients: data, a model family that can represent the solution, and a loss that tells it when it is wrong.
  • Learning is search: start with bad parameters, measure the loss, and step in the direction that reduces it.
  • Data quality matters more than algorithm choice. A model can only learn patterns that exist in the examples it sees.

Next you will learn the equally important skill: recognizing when machine learning is the wrong tool for the job.

Checkpoint quiz

What is the fundamental difference between rule-based programming and machine learning?

Why does the mean-predictor loop gradually move guess toward 14?

Go deeper — technical resources