PythonMastery
beginner 12 min read · lesson 1 of 6 in Machine Learning Fundamentals

What is Machine Learning?

1 · The lesson

read

A traditional program is a list of rules a human wrote. if user.country == "UK": show("£"). Add a new currency, add a new rule. Add a new edge case, add a new rule. The program is exactly as smart as the rules you remembered to write.

Machine learning flips this. You don't write the rules — you show the program thousands of examples and let it work out the rules itself. Email arrived, was it spam? Yes or no. Show enough of those, and the program learns to spot spam it has never seen before.

That's it. That's the whole pitch. A program that improves from data without being explicitly programmed to handle every case.

This lesson covers what counts as ML, the three flavours you'll meet, when ML is the right tool (often it isn't), and the loop you'll run every time you build something.


1. The Three Flavours

Every ML problem you'll meet for the next two years falls into one of three buckets.

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.

This is 90% of practical ML. Spam classifiers, price predictors, fraud detectors, image taggers — 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.

Useful but slipperier — "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 maximises reward.

This powers game-playing AIs (AlphaGo, OpenAI Five), robotics, ad bidding, and the RL-with-human-feedback stage that polishes large language models. Hard to set up, slow to train, magical when it works.

FlavourWhat you haveCanonical example
SupervisedInputs and labelsSpam classifier
UnsupervisedInputs onlyCustomer segmentation
ReinforcementReward signal from environmentGame-playing agent

2. ML vs Rules vs Traditional Programming

ML is not always the answer. The honest decision tree:

python
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 reach for ML.
  • Detect spam → ML. The rules are too many, too fuzzy, and keep changing.
  • Convert °F to °C → rules. There's literally a 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 reach for ML.
  • Recognise a dog in a photo → ML. No human can write a "dog detector" by hand.

Rule of thumb: if a domain expert can write the logic on a whiteboard in under an hour, just code it. ML earns its keep when the rules are too numerous, too vague, or too dynamic to specify.


3. Why Now? The Trifecta

ML theory has existed since the 1950s. Linear regression is older than that. So why did "AI" suddenly become unavoidable around 2012?

Three things came together:

  • Data. Smartphones, web logs, sensors. We now generate more labelled examples in a day than the world had in 1990 in total.
  • Compute. GPUs. A model that took a month to train in 2005 trains in an afternoon today.
  • Algorithms. Backpropagation, transformers, gradient boosting — refinements that turned theoretical ideas into shipping products.

A rough timeline you should know:

  • 1950s — Perceptron. First trainable neural net. Promptly hits a wall.
  • 1980s — Backpropagation lets multi-layer nets actually learn. Hype, then winter.
  • 2006 — Deep belief nets re-ignite neural nets.
  • 2012 — AlexNet wins ImageNet by a landslide. Deep learning era starts.
  • 2017 — Transformers paper ("Attention Is All You Need"). NLP changes overnight.
  • 2022 — ChatGPT brings ML into every conversation.

The point: ML's recent boom is mostly about engineering catching up to theory. You're arriving at a great time — the tooling is mature.


4. The ML Loop

Every ML project follows the same six steps. Memorise them. You'll do this loop dozens of times in a career.

python
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 90% of their effort on step 3 (training). Practitioners know that steps 1, 2, and 6 are usually where the project lives or dies. A perfect model on dirty data is a confident liar; a mediocre model on clean data with good monitoring is a working product.

You'll meet each of these steps across this path. The next lesson starts on the contract between data and model.


5. The Python ML Stack

You don't build ML from scratch. You stand on a stack the community has spent two decades polishing.

LayerLibraryWhat it gives you
ArraysNumPyThe N-dimensional array (ndarray)
Tabular datapandasDataFrame — the spreadsheet for code
Plottingmatplotlib / seabornCharts of your data and results
Classical MLscikit-learnLinear models, trees, forests, pipelines
Deep learningPyTorch / TensorFlowNeural nets, GPUs, autograd
LLMsHugging Face, OpenAI SDKPre-trained models you call as APIs

For this path we live in scikit-learn — the workhorse library for classical ML. It has a famously consistent API: every model has .fit(), .predict(), and .score(). Learn one, you've learned them all.

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's a teaser of how compact real ML looks.

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.

python
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

Eight lines, an iris-species classifier, near-perfect accuracy. That's what "stack maturity" buys you. The rest of this path is unpacking what each of those lines actually does.


Common Mistakes

  • Thinking ML is magic. It isn't. It's curve-fitting at scale. If your data has no signal, no model will invent one.
  • Thinking more data always helps. More clean, relevant, labelled data helps. More garbage just makes the garbage faster.
  • Jumping to deep learning when linear regression would do. A linear model that ships beats a neural net that doesn't. Start small, justify the complexity.
  • Treating accuracy as the only metric. Spoiler from lesson 4: on imbalanced problems, accuracy lies.

🎯 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 flavour — 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 — anchor on the data For each problem ask: do I have labelled examples? Do I have a reward signal? Or just raw data? That answers flavour. 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(). ML would be absurd.
  • 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 classified all five correctly, you already know more about ML problem-framing than most product managers. If you got 3+, you're solidly on track.


What You Learned

  • ML is curve-fitting at scale — a program that learns rules from examples instead of being given them.
  • Three flavours: supervised (labels), unsupervised (structure), reinforcement (reward).
  • ML earns its keep when rules are too many, too fuzzy, or too dynamic. 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 the deep end.

Next: Your Data, Your Model — the contract between rows, columns, and the .fit() method.