What machine learning is
Programs that learn rules from examples instead of being told them.
Why it matters
You've used machine-learned models all through Phase 1: every LLM is one. This phase opens the box on the classic kinds. They still run a lot of quiet business machinery. They predict which customers will leave, score which leads are likely to buy, and estimate what a customer will spend over time. They flag fraud and recommend products. They're also the clearest place to learn the ideas that carry over to deep learning and LLMs. Those ideas are training data and generalization (working well on cases the model hasn't seen). They also include overfitting (memorizing instead of learning) and honest testing.
What you'll be able to do
- Contrast writing rules by hand with learning them from examples
- Name the main kinds of learning: supervised, unsupervised, self-supervised and reinforcement
- Judge when machine learning is worth it, and when a rule is better
- See 'learning' as searching for the settings that make the fewest mistakes
What it covers
- Rules in, or rules out
- Learning is searching
- Worked example: learning a cutoff by hand
- Kinds of learning
- When machine learning is worth it
Rules in, or rules out
Think about how a child learns what a dog is. Nobody hands them a rulebook: "four legs, fur, a tail." That rulebook would fail anyway, because cats have all of those too. Instead, people point and say "dog" or "not a dog," over and over. The child works out the pattern alone.
Computers can work either way.
In ordinary programming, you write the rules. Say you run an online shop. You might write: "if a customer hasn't ordered in 90 days, flag them as at risk of leaving." Customer data goes in, and answers come out. A customer who stops buying has churned, and the loss of customers is called churn.
In machine learning, you don't write the rule. You give the computer past examples together with the answers. Here that means past customers, and whether each one churned. The computer finds the rule itself:
Traditional programming: rules + data → answers Machine learning: data + answers → rules (a model)
The rule it finds is called a model. A model is a function with some adjustable numbers inside it. Those numbers are its parameters. Think of them as dials, like the dial on an oven. Training means turning the dials until the model's answers match the known answers as closely as possible. Then you use the model on new customers, whose answers you don't know yet.
In plain words: ordinary programming turns rules into answers. Machine learning turns examples with answers into rules.
Show the answer
The number 90 (the cutoff)
The cutoff is the adjustable number, the parameter. The past customers are the data and their churn records are the answers. Training picks the number.
Learning is searching
Here is learning in its simplest form. Why 90 days? Someone guessed. Instead of guessing, you can try lots of cutoffs. Check each one against past customers and count how often it's right. Then keep the cutoff that scores best.
To score a cutoff you need its accuracy: the share of predictions that turned out right.
accuracy = correct predictions ÷ all predictions
Say a cutoff gets 5 of 6 customers right. Its accuracy is 5 ÷ 6, about 0.83, or 83%.
In plain words: accuracy is your hit rate. Count the right answers and divide by the number of guesses.
The code below loads 600 past customers from the course's practice API. It scores the 90-day rule. Then it tries 78 cutoffs, from 10 days up to 395 days in steps of 5, and keeps the best one:
import numpy as np import requests customers = requests.get("https://api.gradient.test/v1/customers").json()["data"] days = np.array([c["days_since_last_order"] for c in customers]) churned = np.array([c["churned"] for c in customers]) def accuracy(cutoff): predicted = days > cutoff # predict churn if the gap is longer than the cutoff return np.mean(predicted == churned) print("hand-picked rule (90 days):", round(accuracy(90), 3)) cutoffs = np.arange(10, 400, 5) scores = [accuracy(c) for c in cutoffs] best = cutoffs[int(np.argmax(scores))] print("learned cutoff:", best, "accuracy:", round(max(scores), 3)) print("always predict 'stays':", round(np.mean(churned == 0), 3))
Here is what the main lines do:
days > cutoffgives True or False for each customer. True means "we predict this one churns."predicted == churnedchecks each prediction against what really happened. Python counts True as 1 and False as 0, so they compare cleanly.np.meanof a list of True and False values gives the share that are True. That's the accuracy.np.argmax(scores)gives the position of the highest score. Sobestis the winning cutoff.
The hand-picked 90-day rule is right 73.2% of the time. The search finds a cutoff of 295 days, which is right 82.5% of the time. That's a model with one parameter, trained by trying candidates. Real models have many parameters. They use gradient descent (Phase 2) instead of trying everything. But the idea is the same: adjust the parameters to make fewer mistakes on known examples.
Now look at the last line. Predict "stays" for every customer, with no model at all, and you're right 80.7% of the time. That's because most customers stay. This do-nothing score is a baseline. The 90-day rule actually does worse than doing nothing. The learned cutoff beats the baseline by less than 2 points. That's far less impressive than "82.5% accurate" sounds. You'll learn better ways to measure this in the evaluation lessons.
Watch out: before you praise a model's accuracy, ask what the baseline scores.
Go deeper Why big models can't try every setting
Trying every candidate works for one parameter. Here it took 78 tries. Two parameters with 78 candidates each need 78 × 78 = 6,084 tries, one for every pair. Ten parameters need 78 multiplied by itself ten times. That's about 8 billion billion tries (an 8 followed by 18 zeros). Modern LLMs have billions of parameters.
Gradient descent avoids this. It doesn't test every combination. It works out which way to nudge each parameter to cut the errors. Then it takes a small step and repeats. To do that, it needs a smooth score for how wrong the model is: the loss you met in Phase 2. The loss functions lesson, later in this phase, looks at them closely.
Show the answer
It's worse than always predicting 'doesn't cancel', which scores 90%
Always guessing the most common answer scores 90% here. A model at 85% makes more mistakes than doing nothing.
Worked example: learning a cutoff by hand
A small online coffee shop has six past customers. For each one, you know how many days had passed since their last order. You also know whether they churned (1) or stayed (0).
| Days since last order | 10 | 25 | 40 | 70 | 105 | 130 |
|---|---|---|---|---|---|---|
| Churned | 0 | 0 | 0 | 1 | 0 | 1 |
Try three cutoffs: 30, 60 and 100 days. Predict "churn" when the gap is longer than the cutoff. Count the right answers for each. Then compare with always predicting "stays."
days = [10, 25, 40, 70, 105, 130] churned = [0, 0, 0, 1, 0, 1] for cutoff in [30, 60, 100]: right = 0 for d, c in zip(days, churned): guess = 1 if d > cutoff else 0 if guess == c: right += 1 print(cutoff, right, "of", len(days)) print("always 'stays':", churned.count(0), "of", len(days))
It prints 30 4 of 6, 60 5 of 6, 100 4 of 6 and always 'stays': 4 of 6.
Check the 30-day cutoff by hand. It predicts churn for the customers at 40, 70, 105 and 130 days. It's right about 70 and 130, and wrong about 40 and 105. It's also right about 10 and 25, which it predicts will stay. That's 4 of 6.
The 60-day cutoff predicts churn for 70, 105 and 130. Only 105 is wrong, so it scores 5 of 6. The 100-day cutoff predicts churn for 105 and 130. It's wrong about 105 and misses 70, so it scores 4 of 6.
Of the three, 60 days wins, with accuracy 5 ÷ 6, about 0.83. The baseline is 4 ÷ 6, about 0.67. Even the best cutoff makes one mistake: the customer at 105 days came back. Real data is messy like that.
import numpy as np days = np.array([10, 60, 90, 30]) churned = np.array([0, 1, 0, 0]) print(np.mean((days > 50) == churned))
Show the answer
0.75
The cutoff 50 predicts churn for 60 and 90. It's right about 60 and wrong about 90. It's also right about 10 and 30, which it predicts will stay. That's 3 of 4, so 0.75.
Kinds of learning
Machine learning comes in four main kinds. They differ in what the computer gets to learn from.
- Supervised learning is like studying with flashcards that have the answer on the back. Each example comes with the right answer, called a label. "This customer churned" is a label.
- Unsupervised learning is like sorting a big box of mixed buttons with no instructions. There are no labels. The computer looks for groups and patterns on its own.
- Self-supervised learning is like covering the next word of a sentence and guessing it. The answer is hidden in the data itself, so nobody has to write labels.
- Reinforcement learning is like training a dog with treats. The learner tries actions and gets rewards for good ones. It learns by trial and error.
| Kind | What it learns from | Marketing example |
|---|---|---|
| Supervised | inputs paired with the right answer (a label) | predict churn from customer history |
| Unsupervised | inputs only, no labels | find customer segments |
| Self-supervised | raw data where the "label" is part of the data | an LLM predicting the next word of web text |
| Reinforcement | rewards for actions, by trial and error | choosing which offer to show, learning from clicks |
Large language models use several kinds. During pretraining they're self-supervised. In any web text, the next token is a free label. After that, they're fine-tuned with supervised examples and with reinforcement learning from feedback. This phase covers supervised and unsupervised learning, the base that all the rest builds on.
Sorting these by what data you have is how you start most real projects.
Show the answer
Unsupervised
Finding structure without labels is unsupervised learning (clustering, later in this phase).
When machine learning is worth it
Machine learning isn't always the right tool. Use it when:
- the pattern is too complex or fuzzy to write down (what makes a lead likely to buy?),
- you have plenty of examples with known outcomes,
- some mistakes are acceptable, and
- the pattern changes over time, so you'll want to relearn it from new data.
Use a plain rule when a simple rule works well, or when you have little data. Also use a rule when you need a decision you can fully explain and guarantee. "Never email unsubscribed customers" is a rule, not a prediction. A model that's right 99% of the time would still be wrong 1% of the time. Here, even one wrong email breaks a promise.
The workflow
Real machine learning projects follow roughly the same six steps:
1. Frame the question and the metric which customers will churn next quarter? judged how? 2. Gather data with known outcomes past customers and what happened to them 3. Prepare features clean, encode, scale (the next lessons) 4. Train on part of the data fit the model's parameters 5. Evaluate on data it hasn't seen the honest number 6. Deploy and monitor predictions drift as customers change
A few words here may be new. Features are the inputs a model learns from, such as days since the last order. A metric is the score you'll judge the model by, such as accuracy. To deploy a model is to put it to work on real customers. To monitor it is to keep checking it, because customers change and old patterns fade.
Steps 4 and 5 use different customers on purpose. Testing a model on examples it trained on is like grading students on the exact questions they practiced. They'll look better than they are.
Most of the effort in practice goes into steps 1 to 3 and step 5, not the model itself.
Practice what you just read
- 2 code examples you can edit and run
- a practice exercise that checks your code
- a 5-question quiz
- 4 review cards for spaced repetition
Try a quiz question
What does a supervised learning algorithm need that traditional programming doesn't?