lesson
30 min

How Machines Learn

Machine learning finds patterns in examples and uses them to make predictions. Run a tiny model yourself.

What you will be able to do

By the end of this lesson, you can explain training data, labels, and predictions, and run a simple learning example in Python.

Machine learning in plain words

Machine learning is a way to build software that learns from examples instead of following rules a person wrote by hand. It is the engine inside most AI.

Think about how you learned to spot a scam text. Nobody gave you a rulebook. You saw many messages. Over time, you noticed patterns: urgent words, strange links, promises of prizes. Machine learning works the same way, with far more examples.

Four words to know

WordPlain meaningScam-text example
Training dataThe examples the computer learns fromThousands of old text messages
LabelThe right answer attached to each example"scam" or "ok"
ModelThe pattern the computer found"Messages with many urgent words are often scams"
PredictionThe model's guess on something new"This new message is probably a scam"

The model is only as good as its training data. If the examples are old, narrow, or unfair, the predictions will be too. You will see why that matters in the next lesson.

Where you meet machine learning

  • Email spam filters learn from messages people mark as spam.
  • Bank fraud alerts learn what normal spending looks like for you.
  • Map apps predict traffic from the movement of many phones.
  • Video apps predict what you will watch next.

Run a tiny model

The code below "trains" a very simple model. Each example is a message with a count of hype words, like "WINNER" or "act now," and a label. The model finds a cutoff between the two groups. Then it predicts labels for new messages it has never seen.

The first run downloads the Python runtime, which is several MB. If you are on phone data, you can read the code below instead.

Run the code. Then add a new example, like (3, "ok"), to the list and run it again. Watch how the cutoff and the predictions change.
Python 3
Ctrl+Enter to run | Tab to indent | Python runs in your browser via Pyodide
# Each example: (number of hype words in a message, label)
examples = [(0, "ok"), (1, "ok"), (0, "ok"), (4, "scam"), (5, "scam"), (6, "scam")]

ok = [n for n, label in examples if label == "ok"]
scam = [n for n, label in examples if label == "scam"]

# "Training": find the middle point between the two groups
cutoff = (sum(ok) / len(ok) + sum(scam) / len(scam)) / 2
print("Learned cutoff:", round(cutoff, 1))

# "Predicting": label new messages the model has never seen
for n in [2, 3, 7]:
    guess = "scam" if n > cutoff else "ok"
    print(n, "hype words ->", guess)

Notice what happened when you added one example. The cutoff moved, and a prediction may have flipped. Change the data, and you change the model. Real models work the same way, with millions of examples and many more features.

Reflection

Private. Saved only in this browser. No one else can see it.

Go deeper: Machine Learning, Minus the Hype: A Practical Playbook to Ship Useful Models

Ask. Answer. Build together.