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.
| Approach | Who supplies the rule? |
|---|---|
| Write it | You specify which messages count as spam. |
| Learn it | You 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
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 state | Write it yourself: a fixed sender allowlist needs no training. |
| Easy to demonstrate, hard to describe | Learn 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.
- Supply examples. Give the learner emails paired with known spam / not-spam labels.
- Find a rule. Adjust the model until its outputs fit those examples.
- 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.
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.
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.
| Output | Task |
|---|---|
| A quantity: a price, temperature, or number on a dial | regression; Chapter 3 fits our first line. |
| A category: spam or not, cat or dog, which of ten digits | classification; 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.
| Task | Input | Output |
|---|---|---|
| Price a house | 1,800 square feet | 430,000 dollars |
| Sort email | “Free money today” | spam |
| Predict temperature | Weather measurements | A 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.
| What you give it | What it learns | Example |
|---|---|---|
| Supervised: inputs paired with correct labels | A function matching an answer key, then applied to new inputs | Emails → spam / not spam; houses → sale prices; photos → cat / dog |
| Unsupervised: inputs without output labels | The pattern nobody labeled: natural groups or directions of variation | Group similar customers; find genes that switch on together |
| Reinforcement: actions followed by rewards or penalties | A strategy earning more reward over time, without a correct-action answer key | Play a game, steer a robot, improve a chatbot’s behavior |
| Without labels | What we look for |
|---|---|
| clustering | Groups of similar examples; k-means appears in Chapter 7. |
| dimensionality reduction | The 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.
| Ring | What belongs inside |
|---|---|
| Artificial intelligence | The 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 learning | The subset of AI that learns its rules from data: the flip above. Everything from Chapter 3 onward lives here. |
| Deep learning | The 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.
| Family | Scope |
|---|---|
| AI | Rules, search, and ML |
| Machine learning | AI learned from data |
| Deep learning | ML 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.
| Input | Label |
|---|---|
| “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.
- Train. Fit the rule using the training rows and their answers.
- Hold back. Keep separate examples out of fitting; hide their answers from the model.
- 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
| Where | What it looks like | Chapter link |
|---|---|---|
| Mathematical tools | Represent data, compare it, transform it, find downhill, handle uncertainty. | The math toolkit |
| Choosing a function | Fit a straight line for a quantity; bend a boundary for a category. | Regression · Classification |
| Network shapes | Stack neurons, then match the architecture to images, sequences, or graphs. | The neuron |
| A fresh problem | Choose 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.