Part 2 · Machine Learning Essentials

Chapter 6How Machines Learn: Core Concepts

In Chapter 2 we saw the great reversal at the heart of modern AI: instead of writing rules by hand, we show a machine examples and let it find the rules itself. That single idea — learning from data — is what this chapter unpacks. We will meet the three fundamental ways machines learn, define the small vocabulary you will hear in every AI conversation, and understand the one goal that everything in this field quietly serves. No code is required to follow it; the ideas are what matter, and they will anchor everything that comes after.

What "Learning" Means for a Machine

When we say a machine learns, we mean something precise and surprisingly humble: it gets better at a task by being exposed to data, rather than by being explicitly told what to do. A child learns what a cat is not from a definition but from seeing many cats. A machine learning system works the same way — show it enough labelled examples and it gradually builds its own internal sense of the pattern.

The crucial shift from older AI is that nobody writes the rule. We do not tell the system "a cat has pointed ears and whiskers." We show it thousands of pictures labelled cat or not cat, and it discovers for itself which features matter. The system's knowledge ends up stored not as written rules but as a large collection of adjustable numbers, tuned by exposure to data.

The Three Ways Machines Learn

Almost every machine learning system falls into one of three families, distinguished by what kind of data and feedback they learn from.

Supervised learning: learning from labelled examples

The most common kind. You provide examples that are already labelled with the correct answer, and the system learns to predict that answer for new, unseen cases. Show it emails marked spam or not spam, and it learns to sort future emails. Show it house details paired with sale prices, and it learns to estimate the price of a new house. The teacher (the labels) supervises the learning — hence the name.

Unsupervised learning: finding hidden structure

Here the data has no labels, and the system's job is to find structure on its own. The classic task is clustering: grouping similar items together without being told what the groups are. Given a pile of customer data, an unsupervised system might discover natural segments — bargain hunters, loyal regulars, occasional splurgers — that nobody defined in advance. It is less like a student with an answer key and more like an explorer mapping unfamiliar territory.

Reinforcement learning: learning by trial and reward

The system learns by acting and receiving rewards or penalties, the way you might train a dog with treats. It tries an action, sees whether the outcome was good or bad, and adjusts to earn more reward over time. This is how machines have learned to master complex games and control robots. It will return in Part V, where we see that human feedback used to align language models is a close relative of this idea.

Training Data and Test Data

Here is a rule so important it is almost sacred in machine learning. You must keep some of your data hidden away and never let the system learn from it. You train on one portion of the data and test on a separate portion the system has never seen.

Why? Imagine a student who is given the exact exam questions in advance. They could score perfectly by memorizing the answers while understanding nothing. The only honest way to test understanding is with fresh questions. Test data is those fresh questions: it tells you whether the system truly learned the pattern or merely memorized the examples it saw.

Features and Labels

Two more pieces of vocabulary you will hear constantly. The features are the input information the model looks at — for a house, perhaps its size, location, and number of rooms. The label is the answer you want predicted — the house's price. Supervised learning, in one sentence, is learning the relationship between features and labels well enough to predict the label for new examples from their features alone.

The Real Goal: Generalization

Everything in machine learning serves one goal: generalization, meaning good performance on new data the model has never seen. A model that aces its training examples but fails on anything new is worthless. A model that performs well on fresh, real-world data is the whole point. Keep this north star in mind; it explains nearly every choice that follows, from how we split data to how we judge success.

Overfitting and Underfitting

Two ways the pursuit of generalization goes wrong, best understood through two kinds of struggling student.

  • Overfitting is the student who memorizes the textbook word for word but cannot answer a question phrased differently. The model has latched onto the exact training examples — including their noise and quirks — instead of the underlying pattern. It scores brilliantly on training data and poorly on anything new.
  • Underfitting is the student who barely studied and grasps only the crudest outline. The model is too simple to capture the real pattern, and performs poorly even on the training data.

Good learning lives in the balance between these two: complex enough to capture the genuine pattern, simple enough not to memorize the noise. Much of the craft of machine learning is finding that balance.

How We Know It's Working: Evaluation

Finally, we need to measure success. The most intuitive measure is accuracy — the fraction of predictions the model gets right on the test set. But a single number can quietly mislead. If only one email in a hundred is spam, a lazy model that labels everything "not spam" is 99 percent accurate while being completely useless. This is why evaluation deserves real care, a theme we return to in depth in Chapter 25 when we evaluate models and again when we evaluate agents.

Summary

Machine learning means improving at a task by learning patterns from data rather than following hand-written rules. It comes in three families: supervised (labelled examples), unsupervised (finding structure in unlabelled data), and reinforcement (learning from rewards). We train on one slice of data and test on another, untouched slice, because the real goal is generalization — performing well on new data, not memorizing old data. Overfitting and underfitting are the two ways that goal fails, and thoughtful evaluation, beyond a single accuracy number, is how we tell whether learning truly succeeded.

With these concepts in hand, Chapter 7 opens up the most important kind of model in this book — the neural network — and shows that, underneath the intimidating name, it is just numbers and arithmetic.

Practice

Exercises

  1. 1For each of these five problems, decide whether it is best framed as supervised, unsupervised, or reinforcement learning, and justify your choice: predicting tomorrow's temperature; grouping news articles by topic; teaching a program to play chess; flagging fraudulent transactions; organizing a photo library into events.
  2. 2In your own words, explain why training a model and then testing it on the very same data gives a misleading result. Use the exam-with-leaked-questions analogy or invent your own.
  3. 3Describe overfitting and underfitting using a pair of students. Make the distinction clear enough that a friend who has never studied AI would understand it.
  4. 4Give a real example, different from the spam one in the chapter, where high accuracy could hide a useless model. Explain what goes wrong.
  5. 5Pick a task you would like an AI to do. Identify what the features and the label would be, and describe how you would split your data to test whether the model generalizes.
View detailed solutions for all chapters →