Data, features, and labels
Learning starts with examples. For a spam filter, each example is one email. To learn from it, a model needs:
- Features: measurable things about each example. For an email: does it contain a link? How many exclamation marks? Is the sender new? Does it mention money?
- A label: the right answer for that example, such as âspamâ or ânot spamâ, usually decided by people.
A dataset is many examples with their features and labels. The quality of the dataset decides almost everything: a model can only learn patterns that are in its data.
Training and testing
Training is the learning step: an algorithm looks at the training examples and adjusts the model until its answers match the labels as well as possible.
Then comes the important part: testing. You keep some labeled examples aside, never shown during training, and check how the model does on them. That tells you how well it generalizes: how it will do on new emails it has never seen, which is the whole point.
Testing on the training examples would be like grading a student with the exact questions they practiced. It proves memory, not understanding.
Overfitting
A model can learn its training examples too well, including their accidents. If every spam email in the training data happened to arrive on a Tuesday, an overfitted model may decide âTuesday means spamâ. It scores perfectly on its training data and badly on new data. This is called overfitting, and checking on held-out test data is how you catch it.
Drift
The world changes after training. Attackers change their wording, new services appear, and peopleâs behavior shifts. A model trained on last yearâs data slowly gets worse. This is called drift, and it is why security models must be monitored and retrained, not set once and forgotten.
Hands-on: build a spam filter on paper
You are the learning algorithm. Here are eight training emails, already labeled:
| # | Email subject and first line | Label |
|---|---|---|
| 1 | âURGENT: verify your account now!!! Click hereâ | spam |
| 2 | âTeam lunch moved to Thursdayâ | not spam |
| 3 | âYou won a prize! Claim your reward todayâ | spam |
| 4 | âInvoice for October attached, please reviewâ | not spam |
| 5 | âYour password expires, click to keep access!!â | spam |
| 6 | âNotes from yesterdayâs meetingâ | not spam |
| 7 | âCongratulations, you are selected! Act nowâ | spam |
| 8 | âCan you review my pull request?â | not spam |
1. Find features. Write down three simple rules that separate spam from not spam in these eight, for example âcontains !!â.
2. Test on new emails. Now apply only your rules to these four test emails, which your âmodelâ has never seen. Decide spam or not spam for each:
| # | Real label | |
|---|---|---|
| A | âReminder: your parcel is waiting, confirm delivery detailsâ | spam |
| B | âURGENT: server room door left open!!â | not spam |
| C | âYour account was selected for a security review, sign in hereâ | spam |
| D | âQuarterly report draft, feedback welcomeâ | not spam |
3. Score yourself. How many of the four did your rules get right?
What usually happens
Rules like âcontains !!â, âcontains urgentâ, or âcontains clickâ work perfectly on the training emails. On the test emails, they usually miss A and C (calm, believable phishing with none of those words) and wrongly flag B (a real, urgent message from a colleague).
That is overfitting in miniature: the rules learned the surface habits of eight examples, not the real idea of âsomeone trying to trick youâ. Real models need far more, and far more varied, examples, and still make exactly these kinds of mistakes. Lesson 4 is about measuring them.
What to notice
- A model learns from examples: features describe each one, and labels give the right answer.
- Test on examples kept aside from training; scoring on training data only proves memory.
- Overfitting is learning accidents in the training data; drift is the world moving on after training.
- A model can only learn what its data contains. Data quality decides model quality.