Part I · Ch. 1 — Learning from Data

Part I · Chapter 1 of 13

Learning from Data

Rules you write vs. rules the machine finds


Two ways to teach a computer

There are two ways to get a computer to sort your mail into "spam" and "not spam." The first is to write the rules yourself: if it says "free money," flag it; if it comes from grandma, keep it. You find out fast that there are ten thousand rules, they contradict each other, and the spammers change tactics every week. The second way is the subject of this site.

ApproachWho supplies the rule?
Write itYou specify which messages count as spam.
Learn itYou label examples; the machine finds a rule that fits them.

You show the machine thousands of emails a human has already labeled "spam" or "not spam," and you let the machine find the rule. That flip, from writing rules to finding them, is what machine learning means.

I built this for a friend who wanted to know how these things work and had no math background. We build one idea at a time until you can look at a fresh problem and pick a tool with reasons.

The flip

Two stacked left-to-right pipelines. Top, labeled Classical programming: two input boxes, RULES (violet) and DATA (blue), feed a COMPUTER box, which outputs an ANSWERS box (mint). Bottom, labeled Machine learning: two input boxes, DATA (blue) and ANSWERS (mint), feed a COMPUTER box, which outputs a RULES box (violet). A small 'the flip' marker between the rows notes that RULES and ANSWERS have traded sides.
The flip. Classical programming: you write the rules, feed in data, out come answers. Machine learning turns it around. Feed in data and answers, and the rules come out. Then you run those rules on new data.

Classical programming is a pipeline. Rules and data go in, answers come out, and you wrote the rules by hand. Machine learning runs the pipeline the other way: data and answers go in, and the rules come out.

On the spam example, the classical way is to write "if it contains X, then spam," feed it emails, and get labels back. The machine-learning way is to feed it emails and their known labels, and get back a fitted rule describing what spam tends to look like.

When the rule is…Our choice
Easy to stateWrite it yourself: a fixed sender allowlist needs no training.
Easy to demonstrate, hard to describeLearn it from examples: what makes a photo contain a cat?

The flip wins where the rule is too big, too vague, or too changeable to write down. Try describing every arrangement of pixels that could be a cat. Examples show that task more readily than a list of conditions specifies it.

  1. Supply examples. Give the learner emails paired with known spam / not-spam labels.
  2. Find a rule. Adjust the model until its outputs fit those examples.
  3. Use the rule. Run the trained model on a new email whose answer you do not yet know.

If finding a rule is the job, it helps to say what a rule is. Every rule in this book is the same kind of object: a function.

Hand-written rules miss the message nobody thought of. The examples take over, and the dial turns until the wrong answers run out. (2:06)

Every model is a function

Every model in this book is a function. A function takes an input and returns an output. We write it $f$: you feed in $x$, it hands back $f(x)$. Same input, same output, every time.

House features predict a price, email text a spam label, and photo pixels a cat label: three tasks share the input-to-model-to-output structure.
The task changes; the input → model → output structure stays the same.

Linear regression, a decision tree, a neural network, the model behind your favorite chatbot: each one is a function $f$. Training is the search for the settings of $f$ that make its outputs match reality. The rest of this site fills in what $f$ can look like and how the fitting is done.

OutputTask
A quantity: a price, temperature, or number on a dialregression; Chapter 3 fits our first line.
A category: spam or not, cat or dog, which of ten digitsclassification; Chapter 4 draws our first boundary.

Worked example: read the function’s two ends

In the picture, a house of 1,800 square feet goes in and 430,000 dollars comes out. The size is the input; the price is the prediction. For an email, the output is instead the category “spam.” Those are different jobs, so we would train different functions. The shared box describes the input-to-output shape they have in common. It is not one rule that prices houses and sorts email.

Inputs, outputs, and adjustable rules vary. Training still searches for settings whose outputs match the examples.

TaskInputOutput
Price a house1,800 square feet430,000 dollars
Sort email“Free money today”spam
Predict temperatureWeather measurementsA number on a dial

A function need not be a tidy algebra equation. Billions of adjustable numbers can still define an input-in, output-out rule. You run it rather than solve it for $x$. Memory and random draws count as inputs too; fix all the inputs and the rule gives the same output.

Three ways a machine learns

There are three families of machine learning, and which one you are in depends on what answers you have to learn from.

Three equal panels. Supervised: three input squares each paired with a mint check-mark 'answer' tag; caption 'learn with an answer key'; example 'email to spam / not spam'. Unsupervised: a scatter of blue dots lassoed into three faint dashed clusters with no labels; caption 'find the pattern nobody labeled'; example 'group similar customers'. Reinforcement: an agent circle facing a fork, a mint +1 reward token on one branch and an amber minus-1 penalty token on the other; caption 'learn from trial and reward'; example 'a program learns a game'.
The three families. Supervised learning has an answer key and matches it. Unsupervised learning has no labels and hunts for structure on its own. Reinforcement learning has no answer key either, but the world hands out rewards and penalties after the fact. Most of this site is about the first panel.
What you give itWhat it learnsExample
Supervised: inputs paired with correct labelsA function matching an answer key, then applied to new inputsEmails → spam / not spam; houses → sale prices; photos → cat / dog
Unsupervised: inputs without output labelsThe pattern nobody labeled: natural groups or directions of variationGroup similar customers; find genes that switch on together
Reinforcement: actions followed by rewards or penaltiesA strategy earning more reward over time, without a correct-action answer keyPlay a game, steer a robot, improve a chatbot’s behavior
Without labelsWhat we look for
clusteringGroups of similar examples; k-means appears in Chapter 7.
dimensionality reductionThe few directions that preserve important variation.

Most of Parts I and II use supervised learning: an answer key makes fitting easy to see. The capstone returns to choosing which family suits a fresh problem.

AI, ML, deep learning: which is inside which

AI, machine learning, and deep learning are nested, like Russian dolls: deep learning sits inside machine learning, which sits inside AI. The three words get used interchangeably, and they shouldn't be.

RingWhat belongs inside
Artificial intelligenceThe broad goal of machines doing tasks that seem to require intelligence: learned models, hand-written expert rules, search, and game trees. A 1990s chess engine could be AI without machine learning.
Machine learningThe subset of AI that learns its rules from data: the flip above. Everything from Chapter 3 onward lives here.
Deep learningThe subset of machine learning using neural networks with many layers stacked up: many processing stages, not profound insight.

The arrows only point outward: every deep-learning model is machine learning, and every machine-learning model is AI. The reverse statements fail. A search program can be AI without learning anything from examples; a fitted straight line can learn from data without having any layers.

“Deep” counts layers: many processing stages stacked one after another. A deep network can do a great deal without understanding anything in a human sense. The word describes how the network is built.

Each row narrows the family above.
FamilyScope
AIRules, search, and ML
Machine learningAI learned from data
Deep learningML with neural layers

When a headline says “AI,” ask which ring it means: the broad goal, learning from data, or a many-layer neural network. It is usually the innermost one.

What "training data" really is

For supervised learning, the training set is a table. Each row is one example, with the input in some columns and the correct answer in the last column. Ten thousand emails fill ten thousand rows, each holding the words of one message and its human-assigned "spam / not-spam" label. Throughout this site we call it "our $N$ examples," where $N$ is how many rows we have.

One row pairs input with an answer.
InputLabel
“Free money today”spam

Examples determine what a model can learn. Biased examples teach bias, wrong labels teach mistakes, and a model trained on sunny-day photos alone is not ready for rain. Getting relevant data is much of the work. That is the practical warning behind “garbage in, garbage out.”

We grade the model on data it has never seen. Hold some examples back, hide their answers, and check whether the found rule generalizes. A rule that aces the examples it studied and flops on new ones has overfit. It memorized the answer key instead of learning the subject. The tension between memorizing and generalizing turns up in every chapter from here on, and Part II treats it properly.

  1. Train. Fit the rule using the training rows and their answers.
  2. Hold back. Keep separate examples out of fitting; hide their answers from the model.
  3. Check. Compare its predictions with those held-back answers to test generalization.

This explains something you have probably noticed. A model that has read much of the internet can still be confidently wrong, because it learned from what people wrote, warts and all. There is no separate reservoir of truth it checks against.

Where you'll meet this

WhereWhat it looks likeChapter link
Mathematical toolsRepresent data, compare it, transform it, find downhill, handle uncertainty.The math toolkit
Choosing a functionFit a straight line for a quantity; bend a boundary for a category.Regression · Classification
Network shapesStack neurons, then match the architecture to images, sequences, or graphs.The neuron
A fresh problemChoose what shape the function takes and how to fit it.Choosing your tool

What you now know

  • Machine learning finds rules from examples when writing those rules is hard.
  • Training fits a function: regression predicts quantities, and classification predicts categories.
  • Supervised learning matches labels, unsupervised learning finds structure, and reinforcement learning learns from consequences.
  • Deep learning is inside machine learning, which is inside AI; “deep” counts layers, not insight.
  • Relevant, correctly labeled examples matter, and testing on unseen examples distinguishes generalization from overfitting.

Where we’re headed. “Fitting a function” is still just a phrase. The next chapter builds five tools: vectors to hold data, dot products to compare it, matrices to transform it, slopes to find downhill, and probability to handle uncertainty. Each gets a picture and a worked example in the math toolkit.