CASE STUDY 03 MODEL EVALUATION
Evaluating a
Spam Filter.
A spam filter needs to catch unwanted messages without hiding legitimate email. I compared two methods using Spambase, a public set of email measurements, and counted both kinds of mistake.
THE PROJECT AT A GLANCE
01 / THE QUESTION
Catch spam without hiding legitimate mail.
A spam filter can make two kinds of mistake: let spam through, or flag legitimate email—a message that is not spam. A high overall score can hide this difference. Losing a job offer or a customer’s message may matter more than seeing another unwanted email.
Across three assignments, I checked the data, compared two methods, and changed the rule for flagging spam. The method called logistic regression made fewer total mistakes in this study. A stricter rule protected more legitimate email but let much more spam through.
02 / MY CONTRIBUTION
Check the data and compare two methods.
I wrote Python programs and technical reports for Foundations of Machine Learning at the University of Maryland Global Campus. The work had three stages:
- Check the data — Assignment 1a. I looked for missing values and repeated records, counted spam and non-spam messages, and examined word and capital-letter patterns. I did not train a model at this stage.
- Build and test a model — Assignment 1b. A model is a method that learns patterns from examples. I used logistic regression, which combines the measured email characteristics into a spam score. I compared settings, counted mistakes, and tested a stricter rule for flagging spam.
- Try fewer inputs — Assignment 2. I tested Naïve Bayes, a method that combines evidence from individual characteristics. I checked whether using fewer measurements improved its results, then compared it with logistic regression.
The programs save file checksums to identify the source data, along with the settings, predictions, and results. This record lets someone check the work or repeat the analysis.
03 / THE METHOD
Compare the methods on the same records.
Spambase has 4,601 email records from a collection assembled at Hewlett-Packard in 1999. Each record is labeled spam or non-spam and has 57 measurements. These include how often “free” appears, punctuation use, and runs of capital letters. The full messages are not included.
I used 3,680 records to build the models and saved 921 for testing. To choose settings, I divided the first group into five parts. I trained on four parts and checked on the fifth, repeating until each part had been checked. This is called five-fold cross-validation. The programs learned how to adjust or select inputs only from the records used for training in each round.
The Naïve Bayes assignment reused the earlier test records so I could compare the results directly. This was a repeat test on the same data. Also, 105 test records had exactly the same measurements as training records. I checked the effect of those repeats separately.
Calling every message legitimate would give 60.6% accuracy here while catching no spam. I therefore counted both kinds of mistake as well as the overall accuracy.
More detail: choosing settings and preparing inputs
The 80/20 split kept roughly the same share of spam in each group. Random seeds 430 and 431 make the split and five-fold checks repeatable. I found no missing values and kept every source row in the main analysis.
For logistic regression, I tested the original inputs and a log1p transformation, which reduces the effect of very large values. I also tested six levels of regularization, which limits how much weight the model can give an input. The chosen setup used log1p, standardization to put inputs on similar scales, and L2 logistic regression with C = 1 and the lbfgs solver.
For Naïve Bayes, I tested 126 combinations of input type, settings, and input count. Gaussian versions use measured values. Bernoulli versions use whether a word or character appears. I tried 10, 20, 30, 40, 50, or all available inputs. ANOVA SelectKBest ranked inputs by how much their values differed between spam and non-spam, using training labels only.
The chosen version used 20 inputs, log1p Gaussian Naïve Bayes, and variance smoothing of 0.001. Smoothing helps keep the calculations stable. The best Gaussian version using all inputs had smoothing of 0.1.
I ranked candidates by their average ROC-AUC, a measure of how well they give spam higher scores than non-spam. For this comparison, I kept the earlier logistic-regression settings and used a cutoff of 0.50 for every model. I did not adjust the cutoff or add a separate step to improve the probability estimates.
Both methods use the same test records. The data are old, and some test records have the same measurements as training records.
04 / THE FINDINGS
Fewer inputs helped, but the first model still led.
The test group had 558 legitimate messages and 363 spam messages. Each model gives a message a spam score from 0 to 1. In this comparison, a score of 0.50 or higher means “flag as spam”:
| Model | Legitimate mail flagged | Spam missed | Correct overall |
|---|---|---|---|
| Naïve Bayes · all 57 inputs | 166 | 14 | 80.5% |
| Naïve Bayes · 20 selected inputs | 49 | 31 | 91.3% |
| Logistic regression · 57 inputs | 23 | 32 | 94.0% |
After choosing settings for each version, the 20-input Naïve Bayes model made 100 fewer total mistakes than the all-input version. It flagged 117 fewer legitimate messages but missed 17 more spam messages.
Compared with logistic regression, the 20-input model caught one more spam message but flagged 26 more legitimate messages. Logistic regression made fewer mistakes overall on these test records.
More detail: what the scores mean and how certain they are
Precision is the share of spam flags that are correct. Recall is the share of spam caught. F1 combines precision and recall. ROC-AUC measures how well a model ranks spam above legitimate mail across possible cutoffs: 0.5 is chance-level ranking and 1.0 is perfect ranking. ROC-AUC is different from the percentage of messages labeled correctly.
| Measure | All-input Bayes | Selected Bayes | Logistic |
|---|---|---|---|
| Precision | 0.678 | 0.871 | 0.935 |
| Recall | 0.961 | 0.915 | 0.912 |
| F1 | 0.795 | 0.892 | 0.923 |
| ROC-AUC | 0.952 | 0.960 | 0.982 |
| Log loss | 2.422 | 1.085 | 0.167 |
Log loss gives a larger penalty when a model is very confident and wrong; lower is better. The 20-input Naïve Bayes model scored worse than simply giving every message the same spam probability (log loss 0.671). It made fewer labeling mistakes but still made some with very high confidence. Its probability estimates therefore need care.
Naïve Bayes treats each input as independent of the others once the message’s class is known. Related word or capital-letter measurements can break that assumption and make it count similar evidence more than once.
To check uncertainty, I repeated the comparison on 2,000 resampled versions of the test data. Records with identical measurements stayed together, giving 887 distinct groups to sample. This is called a paired group bootstrap. For the 20-input model versus all-input Naïve Bayes, the 95% interval for the F1 improvement was 0.0666–0.1303. The ROC-AUC difference ranged from −0.0032 to 0.0199. Because that range includes zero, this check does not clearly establish a ranking improvement.
These ranges apply to the models already fitted on this dataset. They do not cover uncertainty from choosing the models, or fix the overlap between training and test records.
05 / THE DECISION
A stricter cutoff let more spam through.
Raising the cutoff makes a filter stricter about what it calls spam. In the logistic-regression assignment, I chose a higher cutoff using the training data. The goal was to leave at least 99% of legitimate messages unflagged in those training checks.
| Decision cutoff | Legitimate mail flagged | Spam caught | Spam missed |
|---|---|---|---|
| Standard · 0.50 | 23 of 558 | 331 of 363 | 32 of 363 |
| Stricter · about 0.934 | 3 of 558 | 230 of 363 | 133 of 363 |
The stricter rule protected 20 more legitimate messages but let 101 more spam messages through. The right cutoff depends on how much each kind of mistake matters to the people using the filter.
More detail: how I chose the stricter cutoff
I chose 0.933843 using only training data. I used nested, grouped cross-validation: inner rounds chose model settings, and outer rounds checked predictions on records left out of that training. Records with identical measurements stayed together in every round.
This cutoff left 99.01% of legitimate mail unflagged in those training checks. That measure is called specificity. On the test records, it left 99.46% unflagged and caught 63.36% of spam. At the 0.50 cutoff, those figures were 95.88% and 91.18%. The test results were not used to choose the cutoff.
06 / THE LIMITS
These results describe a classroom test.
- Repeated records can make a test easier. There are 391 rows that repeat an earlier row exactly. Also, 105 test records have the same measurements as training records. This limits how much the main scores tell us about new email.
- I checked the effect of repeats. Extra checks kept records with identical measurements together. Logistic regression still led in the later comparison. These checks still used the same historical dataset.
- The data reflect their source. Words such as “hp” and “george” reflect the original collection. They may help recognize that particular correspondence more than legitimate mail in general. Results on this 1999 collection do not show how the models would perform in today’s inboxes.
- The records cannot explain every mistake. Without the full messages, I could check the measurements but could not reliably explain what a wrongly labeled email said. Some records with identical measurements also have different spam labels.
More detail: two checks for repeated measurements
In Assignment 1b, I kept the chosen logistic-regression settings and ran two checks: removing exact duplicate rows, and keeping records with identical measurements in the same group. The grouped check used five rounds across all 4,601 records. Its combined ROC-AUC was 0.9799, with no identical patterns shared between training and checking groups.
In Assignment 2, I used only the original 3,680 training records. Five outer rounds checked results, with three inner rounds choosing settings each time. Records with identical measurements stayed together throughout. Average outer-round ROC-AUC was 0.9527 for all-input Naïve Bayes, 0.9580 for selected Naïve Bayes, and 0.9781 for logistic regression.
These checks used different records and procedures, so their scores cannot be compared directly. They address exact repeats. They cannot undo earlier work with this dataset, reveal unknown links between messages, or settle conflicting labels.
07 / WHAT I TOOK FROM THE WORK
A useful score must lead to a useful decision.
The work showed why data quality and the cost of mistakes matter when comparing models. Fewer inputs helped Naïve Bayes. A stricter cutoff changed which mistakes logistic regression made. A real email service would need to decide which mistakes it could accept.
This was a classroom analysis; I did not put a live filter into use. Before real use, I would test current email, keeping messages from the same sender or time period together. I would agree on acceptable rates for both kinds of mistake, test the cutoff, and check how well the probability estimates match actual results.
Coursework basis: DATA 430, Assignments 1a and 1b (data exploration and logistic regression) and Assignment 2 (Bayesian classification).
Dataset: UCI Spambase , by Hopkins, Reeber, Forman, and Suermondt (1999), licensed CC BY 4.0. DOI: 10.24432/C53G6X .