In ordinary programming, you write the rules: if marks >= 40: print("pass"). In machine learning (ML), the computer finds the rules itself from examples.
ordinary programming: rules + data → answers
machine learning: data + answers → rules (a "model")
That helps when the rules are too many or too subtle to write by hand: recognising a face, translating a sentence, guessing tomorrow's electricity demand, spotting a fraudulent card payment.
A first example
# Hours studied, and whether the student passed (1) or not (0)
hours = [1, 2, 3, 4, 5, 6, 7, 8]
passed = [0, 0, 0, 1, 0, 1, 1, 1]
best_cutoff, best_correct = None, -1
for cutoff in range(1, 10): # training: try every rule "pass if hours >= cutoff"
correct = sum(1 for h, p in zip(hours, passed) if (h >= cutoff) == (p == 1))
if correct > best_correct:
best_cutoff, best_correct = cutoff, correct
print(f"learned: pass if hours >= {best_cutoff} ({best_correct} of {len(hours)} right)")
for h in [2.5, 4.5, 9]: # prediction, for new students
print(h, "hours ->", "pass" if h >= best_cutoff else "fail")The program has the hours eight students studied and whether they passed. It does not know the rule. It tries every possible cut-off and keeps the one that gets most students right: that is training. Then it uses the learned cut-off on students it has never seen: that is prediction. Every ML method in this course is a smarter version of the same two steps.
The words
| Word | Means | Here |
|---|---|---|
| example (sample) | one row of data | one student |
| feature | an input value | hours studied |
| label (target) | the answer to learn | passed or not |
| model | the learned rule, with its parameters | "pass if hours ≥ 4" |
| training | finding the best parameters for the examples | trying every cut-off |
| prediction (inference) | using the model on new examples | the new students |
| loss | a number saying how wrong the model is | the mistakes it makes |
Three kinds of learning
- Supervised learning: every example has its label. If the label is a category (spam or not, which digit), it is classification; if it is a number (a price, a temperature), it is regression. Most of this course is supervised.
- Unsupervised learning: no labels. The model finds structure by itself, such as clusters of similar customers.
- Reinforcement learning: an agent acts, gets rewards or penalties, and learns which actions pay off: how game-playing programs and robots learn.
When not to use ML
ML needs data, lots of it and of good quality, and it gives answers that are usually right, not always. If you can write the rule (a tax calculation, a sort), write it. ML also learns whatever is in the data, including its mistakes and unfairness; the last lesson of this course comes back to that.
Real ML uses libraries such as NumPy, scikit-learn and PyTorch. This course builds each method from scratch in plain Python, so you can step through it and see exactly what happens. The last lesson shows the library versions.
Quick check
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.