What is Machine Learning?
1 · The lesson
readA traditional program is a list of rules a human wrote. if user.country == "UK": show("£"). For each new currency, you add a new rule. For each new special case, you add another rule. The program can only handle the cases you remembered to write rules for.
Machine learning works the other way round. You don't write the rules. You show the program thousands of examples, and it works out the rules itself. For example: here is an email. Was it spam, yes or no? Show it enough of those, and the program learns to spot spam it has never seen before.
That is the core idea: a program that improves from data, without being explicitly programmed to handle every case.
This lesson covers what counts as ML, the three main types you'll meet, when ML is the right tool (often it isn't), and the steps you'll follow every time you build an ML project.
1. The Three Types
Almost every ML problem you will meet while learning falls into one of three groups.
Supervised learning: you have labels
You have examples and the right answer for each one. Email → spam/not-spam. House → sale price. X-ray → tumour/no-tumour. The model's job is to learn the mapping from input to label, then apply it to new inputs.
Most practical ML is supervised. Spam classifiers, price predictors, fraud detectors and image taggers are all supervised.
Unsupervised learning: no labels, find structure
You have examples but no right answer. The model finds patterns. Group customers into segments without being told what the segments are. Detect anomalies in server logs. Reduce a thousand columns down to two for visualisation.
This is useful but harder to judge: "is this a good clustering?" is a much harder question than "is this prediction correct?"
Reinforcement learning: learn from reward
An agent takes actions in an environment and gets a reward signal. Win the game → reward. Drive into a wall → penalty. Over millions of attempts the agent learns a policy: a strategy that gets the highest reward.
This powers game-playing AIs (AlphaGo, OpenAI Five), robotics, ad bidding, and the "reinforcement learning from human feedback" stage used to improve large language models. It is hard to set up and slow to train, but it can solve problems that the other types can't.
| Type | What you have | Typical example |
|---|---|---|
| Supervised | Inputs and labels | Spam classifier |
| Unsupervised | Inputs only | Customer segmentation |
| Reinforcement | Reward signal from environment | Game-playing agent |
2. ML vs Rules vs Traditional Programming
ML is not always the answer. Use this decision tree:
Can you write the rules down clearly?
├── Yes → use rules. Faster, debuggable, free.
└── No → Do you have lots of labelled examples?
├── Yes → ML is a strong fit.
└── No → collect data first, or stick with heuristics.A few worked examples:
- Compute VAT on a price → rules. It's a multiplication. Don't use ML.
- Detect spam → ML. The rules are too many, too fuzzy, and keep changing.
- Convert °F to °C → rules. There is an exact formula.
- Predict next month's revenue → ML. Too many interacting factors to hand-code.
- Check if a string is a valid email → rules (a regex). Don't use ML.
- Recognise a dog in a photo → ML. No human can write a "dog detector" by hand.
Rule of thumb: if an expert in the field can write the logic on a whiteboard in under an hour, just code it. ML is worth it when the rules are too many, too vague, or change too often to write down.
3. Why Now? Three Things Together
ML theory has existed since the 1950s. Linear regression is older than that. So why did "AI" suddenly become so widespread after about 2012?
Three things came together:
- Data. Smartphones, web logs, sensors. We now collect far more data, and far more labelled examples, than ever before.
- Compute. GPUs made it possible to train large models much faster than before.
- Algorithms. Backpropagation, transformers, gradient boosting: improvements that turned ideas from research papers into real products.
A rough timeline you should know:
- 1950s: the Perceptron, the first trainable neural network. Its limits soon became clear.
- 1980s: backpropagation lets neural networks with several layers learn. Great excitement, then a long period of low interest and funding (an "AI winter").
- 2006: deep belief nets bring new interest in neural networks.
- 2012: AlexNet wins the ImageNet competition by a very large margin. The deep learning era starts.
- 2017: the Transformer paper ("Attention Is All You Need"). Language processing changes quickly.
- 2022: ChatGPT makes ML something that everyone talks about.
The point: ML's recent growth is mostly about engineering catching up with theory. This is a good time to start, because the tools are mature.
4. The ML Loop
Every ML project follows the same six steps. Learn them well, because you will repeat them many times.
1. Collect data → CSVs, APIs, scraping, sensors, user clicks 2. Preprocess → clean, impute missing values, encode, scale 3. Train → fit a model on the prepared data 4. Evaluate → does it generalise to data it didn't see? 5. Deploy → ship it behind an API, batch job, or app 6. Monitor → does it still work next month? Re-train if not.
Beginners spend most of their effort on step 3 (training). Experienced people know that steps 1, 2, and 6 usually decide whether the project succeeds. A model trained on dirty data gives wrong answers with high confidence. An average model trained on clean data, with good monitoring, is a working product.
You'll meet each of these steps across this path. The next lesson starts with how data and a model must fit together.
5. The Python ML Stack
You don't build ML from scratch. You use a set of libraries that the community has improved over about two decades.
| Layer | Library | What it gives you |
|---|---|---|
| Arrays | NumPy | The N-dimensional array (ndarray) |
| Tabular data | pandas | DataFrame: a spreadsheet inside your code |
| Plotting | matplotlib / seaborn | Charts of your data and results |
| Classical ML | scikit-learn | Linear models, trees, forests, pipelines |
| Deep learning | PyTorch / TensorFlow | Neural nets, GPUs, autograd |
| LLMs | Hugging Face, OpenAI SDK | Pre-trained models you call as APIs |
In this path we use scikit-learn, the main library for classical ML. Its API is known for being consistent: models have .fit(), .predict(), and .score(). Once you learn one model, the others work the same way.
Forward links: pandas in depth, deep learning, LLMs and the API.
6. A 10-Second Demo
You're not expected to understand this code yet. It shows how short real ML code can be.
Run these right here: scikit-learn and pandas both work in the browser. Nothing to install, and your code never leaves your device. If you would rather keep your work between cells, the notebook shares one Python session across all of them.
from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split X, y = load_iris(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42) model = LogisticRegression(max_iter=1000).fit(X_train, y_train) print(f"Test accuracy: {model.score(X_test, y_test):.2f}") # → Test accuracy: 1.00
Seven lines of code give you a classifier for iris species, with very high accuracy on this small dataset. That is the benefit of mature libraries. The rest of this path explains what each of those lines does.
Common Mistakes
- Thinking ML can do anything. It is curve-fitting at a large scale. If your data contains no useful pattern, no model will find one.
- Thinking more data always helps. More clean, relevant, labelled data helps. More bad data doesn't.
- Jumping to deep learning when linear regression would do. A simple linear model that works in production is better than a neural net that never gets finished. Start small, and add complexity only when you can show it is needed.
- Treating accuracy as the only metric. As lesson 4 shows, on imbalanced problems accuracy can be misleading.
🎯 Your Turn: Classify These Problems
Below are 5 real problems. For each, decide:
1. Is ML the right tool, or would rules be simpler?
2. If ML, which type: supervised, unsupervised, or reinforcement?
The problems:
- A. Flag credit-card transactions that look fraudulent.
- B. Convert user-entered postal codes to upper case.
- C. Group 50,000 product reviews into "themes" nobody has labelled.
- D. Train a bot to play a custom board game with no existing strategy guide.
- E. Predict tomorrow's temperature from the last 30 days of weather.
Hint: start from the data
For each problem ask: do I have labelled examples? Do I have a reward signal? Or just raw data? That tells you the type. Then ask: could a junior dev write the rules in an afternoon? That answers ML-vs-rules.Show the most common right answers
- A. Fraud detection → ML, supervised. You have historical transactions labelled "fraud / not fraud". Rules catch the obvious cases, ML catches the patterns humans miss.
- B. Upper-casing postal codes → Rules.
code.upper(). Using ML here would make no sense. - C. Grouping reviews into themes → ML, unsupervised. No labels, you want structure. Topic modelling / clustering.
- D. Game-playing bot, no strategy guide → ML, reinforcement. No labels, only win/lose reward. Classic RL.
- E. Tomorrow's temperature → ML, supervised (regression: predicting a number). Each day has a temperature label.
If you got 3 or more right, you are on track.
What You Learned
- ML is curve-fitting at a large scale: a program that learns rules from examples instead of being given them.
- Three types: supervised (labels), unsupervised (structure), reinforcement (reward).
- ML is worth it when the rules are too many, too vague, or change too often. Otherwise, just write the rules.
- The boom is recent because data, compute, and algorithms all matured around 2012.
- Every project runs the same loop: collect → preprocess → train → evaluate → deploy → monitor.
- Python's ML stack: NumPy, pandas, scikit-learn, then PyTorch / TensorFlow / LLM SDKs for more advanced work.
Next: Your Data, Your Model: how rows, columns and the .fit() method fit together.
Found this useful? Share it with a friend who’s learning Python.