Menu
CoddyTech

回顾:垃圾邮件过滤器

本课属于 Coddy Machine Learning 学习旅程的“监督学习”单元。第 20 课,共 58 课。

challenge icon

挑战

中等

构建本章开头介绍的垃圾邮件过滤器。emails.csv包含 200 封邮件:links、exclaims、caps_words、from_contact(已知发件人为 1)、words和spam。程序会读取一个阈值。

  1. 留出四分之一的数据用于测试(random_state=0),用训练数据行进行缩放,拟合一个LogisticRegression;打印测试准确率(保留 3 位小数)。
  2. 根据阈值,统计被归为垃圾邮件的测试邮件数量、其中真实邮件的数量,以及漏过的垃圾邮件数量。
  3. 打印最有力的垃圾邮件证据及其权重(保留 2 位小数)。

对于另一个服务,阈值为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)

亲手试试

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())

# 在下方编写你的代码

监督学习中的所有课

自己练习: Python 在线编译器