Step 3 of 6
A model cannot read an email the way a person does. We first turn each email into a handful of numbers called features, and then let an algorithm find how much each feature should count towards “spam”. This step uses only the 300 training emails.
1. Raw email
Subject, sender and body text, plus the human label.
2. Feature engineering
Humans decide which measurable signals might matter, e.g. body length or odd characters in the sender.
3. Learning
Logistic regression adjusts one weight per feature so its guesses match the training labels as closely as possible.
4. The model
A recipe: weighted features in, spam probability out. Above the cut-off (50%), the email is flagged.
The base model starts with four features. The learned weight says how hard a feature pushes the verdict: positive towards spam, negative towards not-spam, near zero means it was ignored. Weights are on a common scale, so they can be compared with each other. Averages are taken over the training emails only.
| Feature | What it means | Avg in spam | Avg in not spam | Learned weight | What was learned |
|---|---|---|---|---|---|
| Email body length | How long the message is.Number of characters in the body. | 314.1 chars | 201.9 chars | +1.76 → spam | Longer emails lean spam: 314.1 vs 201.9 characters on average. This is the strongest signal in the base model, but partly an accident of this inbox: a big share of legit mail is one-line replies like “Sounds good, thanks!”. Polished scams and sales pitches are the same length as normal mail, so length cannot catch them. |
| Grammar & spelling issues | Sloppy writing: common misspellings, repeated words, missing capitals or spaces.Count of rule-based flags (about 20 known misspellings, repeated words like “the the”, lowercase “i”, lowercase sentence starts, missing or double spaces). | 2.8 flags | 0.4 flags | +0.71 → spam | Sloppy writing leans spam: 2.8 flags per spam email vs 0.4 for legit mail. A real but moderate signal. Casual emails from friends (“hey sam, i can't make it”) get flagged too, and carefully written scams score zero. |
| Funky characters in sender address | Addresses that look machine-made, with random digits, hyphens or underscores.Count of digits, hyphens and underscores in the sender’s address (e.g. claims-dept@lotto-win88.xyz scores 5). | 2.3 chars | 0.5 chars | +1.17 → spam | Machine-made addresses lean spam: 2.3 vs 0.5 odd characters on average (e.g. claims-dept@lotto-win88.xyz). Strong signal, with occasional false alarms on legit no-reply addresses that contain hyphens or digits. |
| Sensational subject line | Shouting and hype in the subject.ALL-CAPS words + exclamation marks + hype words (free, winner, urgent, prize, guaranteed...) + currency symbols. | 1.4 points | 0.5 points | -0.12 ignored | Learned almost nothing (weight ≈ 0). Spam averages 1.4 points vs 0.5 for legit mail, but legit promos (“FLASH SALE!”) and urgent work notes (“URGENT: server down”) shout too, while phishing and invoice scams use calm subjects. The feature sounded sensible, but the data says it does not separate the classes. |
Pick an email. Each feature’s value is compared with its typical value, multiplied by its weight, and everything is added up into a spam probability. This is the entire “intelligence” of the base model.
CONGRATULATIONS!!! You have WON $1,000,000
claims_83@luckydayonline.co
Dear Winner, Congratulations! Your email address was randomly selected in our international online draw and you have won $1,000,000.To receive your wininngs you must reply immediately with your full name, address and phone number.This offer expires within 24 hours. Claim here: http://prizecare.com/claim?id=64146 Mrs. silva, Claims Director To stop receiving these emails click here: http://mail-pay.ru/u/1997
| Feature | Value | Pushes toward |
|---|---|---|
| Email body length | 414 | |
| Grammar & spelling issues | 3 | |
| Funky characters in sender address | 3 | |
| Sensational subject line | 8 | |
| Starting point (bias) | -0.30 |
Spam probability 98% → model says spam (cut-off 50%).
Correct on this training email.
Left of the line favours not-spam (green), right favours spam (red). The bars plus the starting point add up to the final score.
Accuracy
84.3%
on training emails
Precision
84.8%
Recall
74.2%
Do not stop here
These numbers describe emails the model has already studied, so they are the most flattering score it will ever get. A model can look excellent on training data and fail on new emails (overfitting). The next step checks it on emails it has never seen.