Recap: Spam Filter
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 20 of 58.
Challenge
MediumBuild 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.
- Hold out a test quarter (
random_state=0), scale on the training rows, fit aLogisticRegression; print its test accuracy (3 decimals). - At the threshold, count test emails sent to spam, real ones among them, and spam let through.
- 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
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter4Logistic Regression
Yes-or-No PredictionsThe Decision ThresholdWeights as EvidenceSeveral ClassesRecap: Spam Filter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler