What machine learning is
- Lesson 1
- 50 min
- Module 1: Understand machine learning
Lesson 1 of 27 · Machine Learning: From Problem to Reliable ModelWhat machine learning is
1. Understand machine learning
2. Frame the prediction problem
3. Evaluate supervised models
4. Build the first models
5. Try other model families
6. Tune and finish the model
7. Understand and choose the model
8. Prepare the model for handoff
What you will be able to do
Understand what machine learning is before you build a model. It is a program that learns a rule from examples, instead of a rule that a person writes. Then you can see when it helps, and when a simple written rule is the better choice.
NotebookDo this lesson in a notebook
The notebook compares a spam rule that a person writes with a model that learns from 12 messages. Then it takes a first look at Larkfield’s ticket history. Its setup cells install pandas and scikit-learn and download the practice data, so it needs no local setup. About 20 minutes.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- Python functions,
ifand lists, from Decisions and repetition in Python for Practical AI Engineering. - How to read a CSV file into a pandas DataFrame and use
groupby, from Transform with Python in SQL and Data Preparation for AI. - Nothing about machine learning. This lesson starts from zero.
What you need on your computer (only for the local route; the notebook needs none of it)
- Nothing yet. You make the project folder
~/projects/ticket-modelin Module 2, in Define a baseline: Set up your project. Until then, open the lesson notebooks in Colab or Kaggle.
The notebook was tested in Jupyter on macOS. It used Python 3.14.6, pandas 3.0.6 and scikit-learn 1.9.1.
How you learned to recognise a ripe mango
Section titled “How you learned to recognise a ripe mango”Think about how you learned to choose a ripe mango. Nobody gave you a rule such as “a mango is ripe when 60% of its skin is yellow and soft under your thumb”. Instead, you saw many mangoes. Someone said “this one is ripe” or “this one is not ready yet”, and sometimes you tasted one. After many examples, you can choose a ripe mango in a shop that you never visited before.
You learned to recognise a cat in the same way. Try to write the rule: “four legs, fur, pointed ears, a tail”. A fox matches it too, and a cat with folded ears does not. You cannot write the rule well, but you use it every day. You learned it from examples, not from a rule.
Machine learning gives a computer the same way to learn. This lesson shows what that means, and when it is a good idea.
Two ways to make a program decide
Section titled “Two ways to make a program decide”A program must often decide something: is this order suspicious, is this message spam, how long will this delivery take? There are two ways to give the program the rule.
Way 1: a person writes the rule. This is traditional programming. For example, a shop checks every order above $500 by hand. The rule is one line of code: if order_value > 500. It is exact, anyone can read it, and it changes only when the shop changes its policy. For a question like this, writing the rule is the best way.
Now try the same for spam email. You write: “spam if the message has the word free”. It catches “WIN a free phone”. But it also marks “Is the free car park open on Sunday?” as spam, and it misses “Cheap watches, buy now”. So you add words, and each new word catches more normal messages. Then spammers write “fr3e” instead of “free”. A rule list for real email grows to thousands of rules, and nobody can keep it right. The pattern is too complex, and it changes too fast, for a person to write.
Way 2: the program learns the rule from examples. You collect many messages, and for each one a person says “spam” or “not spam”. You give the program the messages and these right answers. The program finds the rule itself. When spammers change their words, you add new examples, and the program learns again. This is machine learning.
The diagram shows the two ways side by side.
Text description of the diagram
The left box is traditional programming. A person writes the rules, for example “if the order value is above $500”. The rules and one new case go into the program, and the program gives an answer.
The right box is machine learning. Many past cases, each with its right answer, go into training. Training produces a model: the learned rule. Then a new case, which has no answer yet, goes into the model, and the model gives a prediction.
In short, traditional programming turns rules and data into answers. Machine learning turns data and answers into a rule, and then uses that rule on new data.
Model, training and prediction
Section titled “Model, training and prediction”Three words describe every machine learning system in this course. Each one has a place in the mango story.
- A model is the rule that the program learned. In code, it is an object that you can ask for an answer. In the mango story, it is the feeling in your hands that says “ripe”.
- Training is the step in which the program learns the model from the examples. In scikit-learn, the Python library of this course, training is the method
fit. In the mango story, it is all the mangoes that you looked at and tasted. - A prediction is the model’s answer for a new case. In scikit-learn, it is the method
predict. In the mango story, it is your choice in a new shop. Some people call this step inference; this course says prediction.
A prediction is a good guess from the examples, not a fact. Even an experienced buyer sometimes chooses a mango that is not ripe. A model also makes mistakes, and much of this course is about measuring them honestly.
A tiny example: twelve messages
Section titled “A tiny example: twelve messages”Priya is a data scientist. She shows the two ways on 12 short, made-up messages. A person read each message and wrote the right answer: 1 for spam, 0 for a normal message.
| Spam (1) | Normal (0) |
|---|---|
| WIN a free phone now, click here | Can we meet for lunch tomorrow? |
| You are a winner! Claim your free prize | Your order 4417 has shipped |
| Cheap watches, best price, buy now | Here are the notes from today’s meeting |
| Free gift card for you, click the link | Is the free car park open on Sunday? |
| Earn money fast from home, click now | Happy birthday! See you on Saturday |
| Claim your prize today, offer ends soon | The invoice for March is attached |
The notebook’s first cell makes this table as a DataFrame messages, with the columns text and spam.
Way 1: the written rule. Priya writes the rule “spam if the message has free or winner”, and counts how often it is right:
def rule(text): """Spam (1) if the message has the word "free" or "winner".""" text = text.lower() return int("free" in text or "winner" in text)
messages["rule"] = messages["text"].map(rule)print(messages)print("Right:", (messages["rule"] == messages["spam"]).sum(), "of", len(messages))The output:
text spam rule0 WIN a free phone now, click here 1 11 You are a winner! Claim your free prize 1 12 Cheap watches, best price, buy now 1 03 Free gift card for you, click the link 1 14 Earn money fast from home, click now 1 05 Claim your prize today, offer ends soon 1 06 Can we meet for lunch tomorrow? 0 07 Your order 4417 has shipped 0 08 Here are the notes from today's meeting 0 09 Is the free car park open on Sunday? 0 110 Happy birthday! See you on Saturday 0 011 The invoice for March is attached 0 0Right: 8 of 12The rule is right on 8 of the 12 messages. It misses three spam messages (rows 2, 4 and 5), and it marks the message about the car park (row 9) as spam.
Way 2: a model that learns from the same 12 messages. The notebook has a helper, new_spam_model(), that makes an empty model. The model counts the words of each message. During training, it finds how much more often each word appears in spam than in normal messages. You do not need its details now. What matters are the two steps:
model = new_spam_model()model.fit(messages["text"], messages["spam"]) # training: learn from the examples
messages["model"] = model.predict(messages["text"]) # predictionprint("Right:", (messages["model"] == messages["spam"]).sum(), "of", len(messages))The output:
Right: 12 of 12Tomás, an AI engineer who works with Priya, is pleased: “12 of 12, perfect.” Priya is not. The model saw these 12 answers while it learned. A student who saw the answers also gets every question right, and that says nothing about what the student understood. The fair test is messages that the model has never seen.
So Priya writes six new messages, with their right answers, in a DataFrame new. Then she asks both ways:
new["rule"] = new["text"].map(rule)new["model"] = model.predict(new["text"])print(new)print("Rule right:", (new["rule"] == new["spam"]).sum(), "of 6;", "model right:", (new["model"] == new["spam"]).sum(), "of 6")The output:
text spam rule model0 Click now to claim your cash prize 1 0 11 Buy cheap phones, best offer today 1 0 12 Lunch on Friday? 0 0 03 Your parcel has shipped 0 0 04 Fr3e g1ft for you, click here 1 0 15 The notes from the meeting are attached 0 0 0Rule right: 3 of 6; model right: 6 of 6The rule misses all three new spam messages, because none of them has “free” or “winner”. The spammer even wrote “Fr3e” to get past such rules. The model uses many clues together, such as “click”, “claim”, “prize” and “cheap”, so one changed word does not stop it.
Omar, the product manager, asks: “So a model is always better than a rule?” No. Six messages are very few, and part of this result is luck. A real spam filter learns from millions of messages, and it still makes mistakes. The important difference is how each way improves. To improve the model, you give it more examples. To improve the rule, a person must write more rules.
Machine learning you have already met
Section titled “Machine learning you have already met”You use machine learning many times every day. In each case, a program learned from many examples, and each example had the right answer.
Your email spam filter learned from messages that people marked as spam. A film or music service suggests what to watch next: it learned from what millions of people chose after the same films. A map app tells you when you will arrive: it learned from past trips on the same roads, with their real travel times. Your bank may send a fraud alert for a strange card payment: it learned from past payments that turned out to be fraud. Autocomplete on your phone suggests the next word: it learned from text that people typed, where the next word was the right answer.
One example is different. A photo app can group the photos of the same face, before you tell it any names. Nobody gave it the right answers. It puts similar faces together, and you add the names later. This is a second kind of machine learning, and Unsupervised learning explains it.
| System | The examples it learns from | The right answer in each example |
|---|---|---|
| Spam filter | Past emails | Spam or not, marked by people |
| Film suggestions | What people watched | What they chose next |
| Arrival time | Past trips | The real travel time |
| Fraud alert | Past card payments | Fraud or not, found out later |
| Autocomplete | Text that people typed | The next word |
| Face groups in photos | Photos | None: the app groups similar faces, and you add the names |
When machine learning helps, and when it does not
Section titled “When machine learning helps, and when it does not”Tomás asks: “So why not use machine learning for everything?” Because a model costs time, and it is sometimes wrong. Someone must collect the examples and their answers, train the model, test it, explain it and check that it still works next month. That cost is worth it only in the right situations.
Machine learning is a good choice when all four of these are true:
- The pattern is too complex to write as rules, as for spam, or for a ripe mango.
- Examples exist, usually with the right answer. Without examples, there is nothing to learn from.
- The decision repeats many times. A spam filter decides for every email. A model that is used once is rarely worth building.
- Some errors are acceptable. Every model is sometimes wrong. A spam filter that misses one spam message in a hundred is still very useful.
Machine learning is a poor choice when one of these is true:
- The answer is already known. “How many tickets arrived last month?” needs a count, not a prediction.
- A simple rule gives the exact answer. “Is the order above $500?” needs
order_value > 500. A model can only be less exact. - There are no examples, for example for a product that has not launched yet.
- Every error is unacceptable, or every answer must follow a written rule. Tax on an invoice follows the law, so a person writes it as rules.
Larkfield’s problem
Section titled “Larkfield’s problem”Larkfield is a fictional online shop for home and garden products, as in the SQL course. Customers contact support, and each contact is a ticket. Some tickets turn out to be serious: a senior agent must take them over. Larkfield calls this an escalation. Grace, the support lead, describes the problem: “When we find out late, the customer has already waited a day in the normal queue. I want those tickets to go to the senior team when they arrive.”
Can a person write the rule? Priority helps: 29.1% of the most urgent tickets (priority 1) escalated, but only 6.7% of priority 3 tickets. But most escalations come from priority 2 and 3 tickets, so a rule on priority alone misses most of them. The pattern hides in many facts together: the team, the order, the customer’s history.
The other three conditions are also true. Larkfield has two years of past tickets, and for each one it knows if it escalated. About 26 tickets arrive each day. And a few wrong flags are acceptable, because a senior agent checks each flagged ticket. So machine learning is a good candidate. In this course, you build that model with Priya, step by step. The data is made up for this course; it has no real people.
The machine learning workflow
Section titled “The machine learning workflow”A model is not one step. It is the middle of a longer piece of work, and this course follows that work in order:
- Frame the problem: what decision the prediction changes, and what exactly to predict (Module 2).
- Collect the data: past cases with their right answers. You prepared Larkfield’s data in the SQL course, and Module 2 downloads it.
- Measure a baseline: the simplest answer that a model must beat, for example “flag priority 1” (Module 2).
- Decide how to evaluate: how you will judge any model fairly, before you build one (Module 3).
- Train a first model (Module 4).
- Improve it: try other models and settings, measure each one, and keep what helps (Modules 5 and 6).
- Understand it: where it fails, why it predicts what it does, and whether a simpler model is better (Module 7).
- Hand it off: save it, write down its limits, and plan the checks that keep it working (Module 8).
The list looks like a line, but in real work it is a loop. A weak result in step 6 often sends you back to step 2 for better data. A new question from the business starts the loop again. Machine learning is iterative comes back to this loop.
Text description of the diagram
The diagram shows the eight steps of the workflow, with the module of this course that teaches each step. 1, Frame: the decision and the target, Module 2. 2, Data: past cases with answers, the SQL course and Module 2. 3, Baseline: the simplest answer to beat, Module 2. 4, Evaluate: decide how to judge a model, Module 3. 5, Model: train a first model, Module 4. 6, Improve: try, measure and keep what helps, Modules 5 and 6. 7, Understand: errors, reasons, and simple or complex, Module 7. 8, Hand off: files, limits and checks, Module 8.
Three dotted arrows go back. From Improve to Model: try again. From Understand to Data: go back for better data. From Hand off to Frame: new data and new questions start the loop again.
The case studies at the end of the course
Section titled “The case studies at the end of the course”Larkfield’s help desk is the example that runs through every lesson. It keeps one problem in view while you learn each idea. Real work has more variety: other data, other decisions, other traps. So the course ends with seven case studies. Each one takes a real, public dataset from a raw file to a tested result and a recommendation, with every output explained.
Case studies in this course
At the end of the course, you use what you learn on a realistic project, from the data to the recommendation. Complete at least one for the certificate; the others are optional.
- Forecast bike-share demandRegression on hourly rentals: leaks, a time split, Poisson models and error slices.
- Estimate house pricesRegression on real house sales: a log target, location, outliers and what a 13% error means.
- Predict credit-card defaultLogistic regression with a cost-based threshold, calibration, and fairness slices.
- Rank a call listA decision tree that ranks calls under capacity: the duration leak, pruning, lift and drift.
- Choose a model for hospital readmissionsCompare logistic regression, a tree, a forest and boosting under one protocol, and choose.
- Segment online-shop customersClustering customers on recency, frequency and spend, checked against their future purchases.
- Find wholesale buyer typesClustering skewed spending data: scaling, the silhouette trap, PCA and an existing label.
The short descriptions use words that the course teaches later, such as regression, clustering and a threshold. For now, read the list as a map of where the course goes.
Each case study is also a portfolio piece: a project that you can put in a public repository and talk about in an interview. You can open them at any time, but they use the whole course, so most learners start them after Module 8. While you learn, some lessons show a short See it end to end box. It points to the case study where you see that lesson’s idea at work on real data.
Guided practice: a rule or examples?
Section titled “Guided practice: a rule or examples?”- Open the notebook from the box Do this lesson in a notebook at the top of this page.
- Run the setup cells. The data cell ends with
Ready: train 16939 rows (1983 escalated), valid 1179 (183), test 1002 (157). - Run sections 1 to 5. Compare each output with the tiny example above.
- In section 5, look at the escalation rate for each priority. You should see
0.291,0.120and0.067. - In section 6, read the six tasks below.
- For each task, write
"rule"or"examples"in the dictionaryways. - Run the check cell. It prints
Check passedwhen all six answers are right. - If it prints
Not yet, ask for each task: can a person say the exact rule in a few lines?
The six tasks:
- Give free delivery to every order above $50.
- Read handwritten postcodes on parcels.
- Suggest films to a viewer from what similar viewers watched.
- Add the sales tax to an invoice.
- Estimate when a taxi arrives, in city traffic.
- Check that a new password has at least 12 characters.
Show the answer
| Task | Way | Why |
|---|---|---|
| 1. Free delivery above $50 | Rule | The rule is exact and one line long. |
| 2. Handwritten postcodes | Examples | Nobody can write the rule for every handwriting, but many scanned postcodes with the right digits exist. |
| 3. Film suggestions | Examples | Taste is too complex to write, and the service has millions of viewing histories. |
| 4. Sales tax | Rule | The law gives the exact rule, and every error is unacceptable. |
| 5. Taxi arrival time | Examples | Traffic is too complex to write, and past trips have their real times. |
| 6. Password length | Rule | len(password) >= 12 is exact. |
Your turn: teach the model a new kind of spam
Section titled “Your turn: teach the model a new kind of spam”Change one thing: the spam. In section 7 of the notebook, the model meets a new kind of spam that it never saw: “Your holiday voucher is waiting, reply today”. It says 0, normal. With a written rule, you would now write a new rule. With machine learning, you add examples.
Write two new spam messages about vouchers, as a spammer would write them, each with the answer 1. Add them to the 12 examples, train a new model, and ask it again. Before you run it, predict: will two examples be enough?
Show an example answer
more_examples = pd.DataFrame({ "text": [ "Your holiday voucher is ready, reply YES today", "Reply YES to get your voucher", ], "spam": [1, 1],})bigger = pd.concat([messages[["text", "spam"]], more_examples], ignore_index=True)model2 = new_spam_model()model2.fit(bigger["text"], bigger["spam"])print("The new model says:", model2.predict([tricky])[0])The output:
The new model says: 1Two examples were enough here, because they share words with the new message (“holiday”, “voucher”, “reply”, “today”). The check cell also asks the new model about a normal message, “Congratulations on your new job! See you at the party”. It still says 0. No person wrote a rule: the examples taught the rule.
Common mistake
A model for an answer that a rule already knows
Symptom: Tomás wants to use machine learning everywhere. He trains a model to say if an order is above $500. It learns from nine example orders, each with the right answer, and it copies the answer of the most similar example order. He tests it on four new orders:
from sklearn.neighbors import KNeighborsClassifier
orders = pd.DataFrame({"order_value": [120, 260, 380, 455, 470, 560, 610, 750, 900]})orders["above_500"] = (orders["order_value"] > 500).astype(int)copycat = KNeighborsClassifier(n_neighbors=1) # copies the answer of the most similar ordercopycat.fit(orders[["order_value"]], orders["above_500"])
check = pd.DataFrame({"order_value": [490, 505, 520, 640]})check["rule"] = (check["order_value"] > 500).astype(int)check["model"] = copycat.predict(check[["order_value"]])print(check)The output:
order_value rule model0 490 0 01 505 1 02 520 1 13 640 1 1The model says that an order of $505 is not above $500.
Cause: the most similar example of 505 is 470, so the model copies “not above”. A model only approximates a rule from examples. Here the exact rule was known, and the model was only a worse copy of it.
Fix: ask first whether a person can write the exact rule. If yes, write the rule: order_value > 500 is never wrong, costs nothing to train, and anyone can read it. Use machine learning only when the rule is too complex to write and examples with answers exist.
Consequence in AI work: a model where a rule would do adds errors, cost and work. Someone must train it, test it and explain its mistakes, for an answer that was already exact. Teams that start with “which model?” instead of “do we need a model?” often build this kind of system.
Knowledge check
Section titled “Knowledge check”Check your understanding
In your own words, what is the difference between writing the rules and machine learning? Use the words model, training and prediction.
Show the answer
In traditional programming, a person writes the rule, and the program applies it to new data. In machine learning, the program learns the rule from examples with their right answers. That step is training, and the learned rule is the model. Then the model gives a prediction for each new case. To improve a written rule, a person writes more rules; to improve a model, you give it more or better examples.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
A shop wants to know if a customer is 18 or older, from the date of birth in the customer's account. Should it train a model? Why or why not?
Show the answer
No. The answer follows exactly from a known value: compare the date of birth with today’s date. A rule gives the right answer every time, and anyone can check it. A model would only approximate the rule. It could be wrong near the 18th birthday, as Tomás’s model was wrong for an order of $505.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
A model was right on all 12 messages that it learned from. Why is that not enough to trust it?
Show the answer
The model saw the right answers of those 12 messages during training, so it can repeat them. That shows nothing about new messages, which is where the model is used. You must test it on examples that it never saw, as Priya did with six new messages. Module 3 shows how to do this properly.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- In traditional programming, a person writes the rule. In machine learning, the program learns the rule from examples with their right answers.
- Training (
fit) learns a model from examples. A prediction (predict) is the model’s answer for a new case. A prediction can be wrong. - On 12 messages, a written spam rule was right 8 times, and it missed all three new spam messages. A model trained on the same examples caught them, and it learned a new kind of spam from two more examples.
- Use machine learning when the pattern is too complex to write, examples exist, the decision repeats and some errors are acceptable. When a rule gives the exact answer, write the rule.
- Larkfield wants to send tickets that will escalate to the senior team at once. The pattern is in many facts together, and two years of tickets have the answer. This course builds that model, following the workflow from framing to handoff, as a loop.
Next, you see how a program learns from examples with right answers, in the same way that a child learns the names of colours.
Next: Supervised learning