Menu
CoddyTech

Recap: Spam Filter

Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 20 of 58.

challenge icon

Challenge

Medium

Build the spam filter this chapter opened with. emails.csv holds 200 emails: links, exclaims, caps_words, from_contact (1 for a known sender), words and spam. The program reads a threshold.

  1. Hold out a test quarter (random_state=0), scale on the training rows, fit a LogisticRegression; print its test accuracy (3 decimals).
  2. At the threshold, count test emails sent to spam, real ones among them, and spam let through.
  3. Print the strongest spam evidence and its weight (2 decimals).

For another service and 0.7:

Test accuracy: 0.884
At 0.7: 15 emails to spam (1 of them real), 4 spam let through
Strongest spam evidence: links (1.38)

Try it yourself

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

emails = pd.read_csv("emails.csv")
features = ["links", "exclaims", "caps_words", "from_contact", "words"]
threshold = float(input())

# Write your code below

All lessons in Supervised Learning

Practice on your own: Online Python compiler