Exercise: Spam or Not

Write three functions that split Inbox Guard's 1,000 emails into training and test emails with the same share of spam in both, train a LogisticRegression on the training emails, and turn its chances of spam into labels at a threshold, and let pytest check the split, the model and the labels.

Journeyman · AI Basics · Python · Updated

By

Inbox Guard wants a spam filter, and 1,000 emails that people already marked as spam or not are the examples to learn from. You'll write the three functions behind a first filter: one that splits the emails into emails to learn from and emails to test on, with the same share of spam in both, one that trains a logistic regression, and one that turns its chances of spam into labels at a line you choose. Tests call each function and check what it gives back, and a notebook comes with the exercise for looking at the data while you work.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-ml-spam-or-not, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:

cd ~/python-course
git clone https://github.com/ask-codey/exercise-ml-spam-or-not.git

Git makes a folder, exercise-ml-spam-or-not, with the exercise code inside. Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment. On Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt

On a Mac (zsh):

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

The prompt starts with (.venv) once the virtual environment is on. The exercise's README says what each command does, and what to do if PowerShell won't run the second one.

The folder holds:

  • emails.csv: the 1,000 emails, our own made-up data, the same file the chapter's lessons use. 299 of them are spam.
  • seed_data.py: the script that made the CSV, with the pattern it planted. You don't need to run it: the committed CSV is what keeps everyone's numbers the same.
  • spam_model.py: the three functions, for you to finish, with FEATURES (the columns the model looks at: num_links, num_exclamations, mentions_free, sender_in_contacts and subject_all_caps) and LABEL (is_spam), ready to use.
  • explore.ipynb: a notebook for looking at the emails and trying your code. Choose the .venv kernel with Select Kernel (top right) before you run it. The tests don't read it.
  • report.py: splits, trains and labels with your three functions and prints what each step gave. Ready to use.
  • tests/: the tests, which call each function and check what comes back, and conftest.py, which gives each test a fresh copy of the emails. You don't need to change them.
  • requirements.txt and pytest.ini: the packages the exercise needs, at exact versions, and where pytest finds the tests and your code.

What to do

Every function in spam_model.py is already declared with its name and parameters, and for now it stops with a NotImplementedError that says Not built yet. The docstring under each one says what it should do, with examples:

def labels_at(probabilities, threshold):
    """Turn chances of spam into labels at a threshold, and return them as a list of whole numbers.

    probabilities is a list (or a NumPy array) of chances of spam, each from 0 to 1,
    such as column 1 of a model's predict_proba. An email whose chance is at or
    above the threshold is spam (1); below it, not spam (0). A chance exactly on
    the line is spam.

    labels_at([0.1, 0.75, 0.5, 0.4], 0.5) -> [0, 1, 1, 0]
    labels_at([0.1, 0.75, 0.5, 0.4], 0.3) -> [0, 1, 1, 1]
    labels_at([], 0.5)                     -> []
    """
    raise NotImplementedError("Not built yet.")

In each function, replace the raise NotImplementedError("Not built yet.") line with your own code:

  1. split_emails(emails, seed) splits the emails into four parts with train_test_split: the FEATURES columns as X, the LABEL column as y, a fifth of the emails held back for testing, seed passed on as random_state, and the split stratified on the label. It returns X_train, X_test, y_train, y_test. With seed 42: 800 training emails holding 239 spam, 200 test emails holding 60 spam, and no email in both.
  2. train_spam_model(X, y) makes a LogisticRegression with its default settings, fits it to X and y, and returns the fitted model. Its classes_ are [0, 1].
  3. labels_at(probabilities, threshold) returns a list with a 1 for each chance at or above threshold and a 0 for each one below it: [0, 1, 1, 0] for [0.1, 0.75, 0.5, 0.4] at 0.5, and [] for no chances at all.

The split comes first, and train_spam_model is only ever given the training emails: that's what keeps the test emails ones the model never saw.

Check your work

Run the tests from the exercise folder, with the virtual environment on:

python -m pytest

At first all 17 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back, for example labels_at([0.7], 0.7) should return [1], but it returned [0]. No test checks how often the filter is right, which depends on each email's random wobble: the tests check the split, the model and the labels.

To see your functions at work on the whole file, run python report.py.

Hints

  • Look before you write. Run the notebook's first cells: emails.head(10) and emails["is_spam"].value_counts(normalize=True) show the columns and the share of spam.
  • One more setting. The split is the one from Splitting Data with train_test_split, plus stratify, which Stratified Splits shows: it takes the labels whose shares both parts should keep.
  • Make, fit, return. train_spam_model is the same three lines as a linear regression's, with LogisticRegression and no settings of your own.
  • On the line counts. Probabilities and the 0.5 Line compares chances with a line; here a chance equal to the threshold is spam, so the comparison is >=.
  • Plain whole numbers. Append 1 or 0 to a list as you go through the chances, so the answer is a list whatever probabilities was.

When you're done

Every test is green: pytest's last line says 17 passed. And python report.py prints:

1) emails: 1000
2) training emails: 800
3) test emails: 200
4) spam share, training emails: 0.299
5) spam share, test emails: 0.3
6) classes: [0, 1]
7) coefficient for num_links: 0.3
8) coefficient for num_exclamations: 0.24
9) coefficient for mentions_free: 1.49
10) coefficient for sender_in_contacts: -2.41
11) coefficient for subject_all_caps: 1.1
12) intercept: -1.5
13) first five test emails, chance of spam: [0.107, 0.062, 0.025, 0.178, 0.324]
14) first five test emails, labels at 0.5: [0, 0, 0, 0, 0]
15) first five test emails, labels at 0.3: [0, 0, 0, 0, 1]
16) test emails flagged at 0.5: 45
17) test emails flagged at 0.3: 78
18) test emails that really are spam: 60

Now compare lines 7 to 12 with what seed_data.py planted: 0.35 a link, 0.30 an exclamation mark, 1.6 for "free", −2.5 for a sender in your contacts, 1.2 for an all-caps subject, and −1.75 to start. Every sign matches; which weights came out closest? Then look at lines 16 to 18: the line at 0.5 flags 45 emails and the line at 0.3 flags 78, with 60 real spam among the 200. Which line would you choose for a spam folder people rarely check?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.

  1. On GitHub, click +, then New repository. Name it (for example exercise-ml-spam-or-not), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-ml-spam-or-not.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-ml-spam-or-not, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

A split that keeps the classes' shares, a classifier trained on one side of it, and a line you choose for its probabilities: that's the honest skeleton of every classifier in this course, and each function is small enough to test.

Key terms

Related