Machine learning from zero · Lesson 1.1 · 2 of 33 lessons ready · 10 min
What is machine learning?
In short
Machine learning lets a program find its rule in examples instead of having a person write it. The lesson shows the difference on real breast tumour data.
How would you write a program that tells a dangerous tumour from a harmless one? You could sit down with a doctor and turn their experience into rules: "if the cells are larger than this, raise an alarm". Or you could show the computer hundreds of past cases with known outcomes and let it work out the rule by itself. The second approach is machine learning, and this course is about why it works, when it fails, and how to tell the difference.
Choose an answer and how sure you are.
Answer to unlock the lab.
Interactive lab
This interactive lab runs in a browser with JavaScript enabled. The text around it describes what the lab shows.
Show the answer
Correct answer: Worse than on the training biopsies
The learned threshold scores 89.7% on training but 86.0% on test (171 biopsies): a rule fitted to one set of data usually does worse on new data. What is more, a hand-set threshold of 15.0 scores 88.3% on the test biopsies, beating the learned one. Best on training is not guaranteed to be best on new data.
Try this
- Move the threshold slider slowly from left to right and watch the accuracy on the training data. Stop where you think the rule works best.
- Press Learn and compare the threshold the algorithm picks with yours. Look at both numbers: accuracy on the training data and on the test data.
- Push the threshold slider all the way to one end. The rule now gives almost every tumour the same answer — yet the accuracy does not fall to zero. Work out why before reading on.
- After pressing Learn, check which score is higher, train or test, and decide which one you would trust with a real patient.
Now that you've tried it, let's see what was actually going on.
The idea in plain words
In ordinary programming a person writes the rule and the computer applies it. In machine learning a person supplies examples — inputs together with the correct answers — and an algorithm searches for a rule that reproduces those answers. The rule it finds is called a model. Once found, the model is used exactly like a hand-written rule: new input in, prediction out.
Think of learning to tell whether an avocado is ripe. Nobody hands you a formula. You press a few, cut them open, notice which ones were good, and after a dozen tries your thumb "knows". You never wrote the rule down, but you have one, and you built it from examples with known outcomes. A learning algorithm does the same thing, only with numbers and far more patience.
Why it works
The examples in this lesson come from a real medical dataset: 569 breast tumours, each described by measurements of cell nuclei seen under a microscope, and each labelled by a pathologist as malignant (212 cases) or benign (357 cases). To start, we use just one of those measurements and the simplest rule imaginable: "above a threshold → malignant, below it → benign".
Choosing the threshold by hand means guessing. Learning the threshold means trying every candidate value on the examples and keeping the one that makes the fewest mistakes. That is all "learning" is here: a search, guided by data, for the setting that works best on cases we already know. Every model you will meet later, from a straight line to a language model with billions of parameters, follows the same recipe — a family of possible rules, a way to score them, and a search for the best score.
There is one catch, and it is the most important idea in the whole field. A rule that fits the known cases well is not automatically good. What we care about is the cases we have not seen yet: the next patient. So before learning, we hide part of the data. The model learns from the training set and is then graded on the test set, which it never saw. The grade is usually accuracy, the share of cases the rule gets right:
accuracy = correct predictions / all predictions
A rule learned on the training set often scores a little lower on the test set. That gap is the honest price of meeting new data, and the test score is the one you would report to a doctor.
Common mistake
The classic beginner error is judging a model by how well it does on the data it learned from. On those cases the algorithm has had every chance to adjust itself, so the score flatters it. The second trap is forgetting the baseline. Most tumours in this dataset are benign, so a "rule" that always answers "benign" is already right in 357 of 569 cases, nearly two thirds. That is why even a rule that gives almost every tumour the same answer is nowhere near zero accuracy. Any learned model has to beat this trivial answer before it deserves attention, and in medicine it also has to make the right kind of mistakes — a theme we return to in lesson 6.
Machine learning, then, is not magic and not understanding. It is a disciplined search for a rule, followed by an honest exam on data the search never touched. In the next lesson we look more closely at the raw material — data, features and labels — and at why the way you split the data changes the score you get.
Quiz
Three questions to check yourself. Pick an answer, or open the answer under a question. Keyboard: arrow keys choose, Enter or Space confirms.
Key takeaways
- In machine learning the rule comes from labelled examples, not from a programmer's head.
- Learning is a search: a family of rules, a score, and the setting with the best score.
- A model is graded on a test set it never saw; the training score flatters it.
- Always compare with a trivial baseline, such as always predicting the most common answer.
Going further: formula, code, practice
The math
Accuracy is accuracy = correct / n. A threshold rule says "malignant" when x > t. Learning picks the t with the fewest mistakes on the training rows: t* = argmin over t of (mistakes on the training set). Only the midpoints between neighbouring sorted values can differ, so the search is finite and exact.
- The score on the training set is biased upwards:
t*was chosen to make that number as high as possible. - The score on the test set is an unbiased estimate of future accuracy, because no choice was made with it.
- The baseline is the share of the biggest class,
max(n₀, n₁) / n. A learned rule is worth keeping only if it clearly beats it.
In code (Python)
Counts and the "always benign" baseline for the whole dataset (the lab uses only mean radius and a split, so its 89.7% and 86.0% are not reproduced here):
from sklearn.datasets import load_breast_cancer
from sklearn.dummy import DummyClassifier
X, y = load_breast_cancer(return_X_y=True, as_frame=True)
print("tumours:", len(y))
print("malignant:", (y == 0).sum(), "benign:", (y == 1).sum()) # in sklearn, 0 = malignant
always_benign = DummyClassifier(strategy="most_frequent").fit(X, y)
print(f"always 'benign': {always_benign.score(X, y):.1%}")
Output
tumours: 569
malignant: 212 benign: 357
always 'benign': 62.7%
A rule that never looks at the tumour is right in 62.7% of cases. That is the bar any learned rule has to clear.
Download the notebook (.ipynb). Open it in Jupyter or upload it to Google Colab; it fetches any missing data itself.
For practitioners
- Compute the baseline first.
DummyClassifier(strategy="most_frequent")takes one line and tells you what "good" has to mean for this data. - Split before you learn anything, including a threshold. Any choice made on the test rows, even a small one, makes the test score optimistic.
- Use
stratify=yintrain_test_splitfor classification, so train and test keep the same share of malignant cases. - Accuracy counts all mistakes equally. A missed malignant tumour and a false alarm are not equally costly, so also look at the two kinds of error separately (lesson 6).
- A test set of 171 rows gives a score that wobbles by two or three points (lesson 2: the standard error is about 2.7 points at 86%). Do not read a gap that small as proof that one rule is better.
Was this helpful?
Thanks! This helps us improve the Atlas.
Go deeper in the Atlas
Encyclopedia entries on the ideas in this lesson: