机器学习实验七--SVM垃圾邮件分类器

SVM垃圾邮件分类器

一、什么是SVM

支持向量机(Support Vector Machine, SVM)是一种监督学习算法,主要用于分类和回归分析。它的核心思想是找到一个最优的超平面,将不同类别的数据分隔开来,同时最大化两类数据之间的间隔(margin)。

SVM的主要特点:

  1. 可以处理高维数据
  2. 通过核技巧(kernel trick)可以解决非线性分类问题
  3. 在小样本数据集上表现优异
  4. 对噪声数据有较强的鲁棒性

二、实例:垃圾邮件分类器

1.实验要求

使用SVMs建立自己的垃圾邮件过滤器。首先需要将每个邮件x变成一个n维的特征向量,并训练一个分类器来分类给定的电子邮件x是否属于垃圾邮件(y=1)或者非垃圾邮件(y=0)。

2.原理解释

2.1 数据预处理流程

  1. 文本清洗:去除HTML标签、URL、邮箱地址等
  2. 标准化处理:将所有数字替换为"number",货币符号替换为"dollar"
  3. 分词处理:将邮件内容分割为单词列表
  4. 词干提取:使用Porter Stemmer算法提取单词词干

2.2 特征提取方法

• 使用词袋模型(Bag of Words)

• 基于预定义的词汇表(1899个单词)

• 每个单词对应特征向量的一个维度

• 如果单词在邮件中出现,对应位置设为1,否则为0

2.3 SVM分类器

• 使用线性核函数

• 正则化参数C=0.1

• 最大迭代次数1000次

3.代码实现

python 复制代码
import numpy as np
import re
from nltk.stem import PorterStemmer
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline
from scipy.io import loadmat

# ===== 1. 加载词汇表并构造映射 =====
def load_vocab_dict(vocab_path='vocab.txt'):
    with open(vocab_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    vocab_dict = {line.split('\t')[1].strip(): int(line.split('\t')[0]) for line in lines}
    return vocab_dict

# ===== 2. 邮件预处理函数 =====
def preprocess_email(email):
    email = email.lower()
    email = re.sub('<[^<>]+>', ' ', email)
    email = re.sub(r'[0-9]+', 'number', email)
    email = re.sub(r'(http|https)://[^\s]*', 'httpaddr', email)
    email = re.sub(r'[^\s]+@[^\s]+', 'emailaddr', email)
    email = re.sub(r'[$]+', 'dollar', email)
    email = re.sub(r'[^a-zA-Z0-9]', ' ', email)
    tokens = email.split()
    ps = PorterStemmer()
    words = [ps.stem(token) for token in tokens if len(token) > 1]
    return words

# ===== 3. 将邮件单词转换为特征向量 =====
def email_to_feature_vector(email, vocab_dict, vocab_size=1899):
    words = preprocess_email(email)
    features = np.zeros(vocab_size)
    indices = [vocab_dict[word] - 1 for word in words if word in vocab_dict]
    np.put(features, indices, 1)
    return features

# ===== 4. 分类器训练函数 =====
def train_svm(X_train, y_train):
    model = Pipeline([('clf', LinearSVC(C=0.1, max_iter=1000))])
    model.fit(X_train, y_train)
    return model

# ===== 5. 测试单封邮件 =====
def predict_email(model, email_text, vocab_dict):
    features = email_to_feature_vector(email_text, vocab_dict)
    pred = model.predict([features])
    return pred[0]

# ===== 6. 主函数入口 =====
if __name__ == '__main__':
    # 词汇表
    vocab_dict = load_vocab_dict('vocab.txt')

    # 加载训练数据
    train_data = loadmat('spamTrain.mat')
    X_train = train_data['X']
    y_train = train_data['y'].ravel()

    # 加载测试数据
    test_data = loadmat('spamTest.mat')
    X_test = test_data['Xtest']
    y_test = test_data['ytest'].ravel()

    # 模型训练
    model = train_svm(X_train, y_train)

    # 准确率评估
    print(f"训练准确率: {accuracy_score(y_train, model.predict(X_train)) * 100:.2f}%")
    print(f"测试准确率: {accuracy_score(y_test, model.predict(X_test)) * 100:.2f}%")

    # 测试一封邮件(如spamSample2.txt)
    with open('spamSample2.txt', 'r', encoding='utf-8') as f:
        content = f.read()
    result = predict_email(model, content, vocab_dict)
    print("预测结果:", "垃圾邮件 (Spam)" if result == 1 else "正常邮件 (Not Spam)")

4.实验结果

5.实验总结

本实验成功实现了一个基于SVM的垃圾邮件分类器,通过精心设计的预处理流程和特征提取方法,在测试集上达到了98.9%的准确率。实验结果表明,SVM在文本分类任务中表现优异,特别是对于像垃圾邮件检测这样的二分类问题。

相关推荐
蹦蹦跳跳真可爱5891 小时前
Python----目标检测(《Fast R-CNN》和Fast R-CNN)
人工智能·python·深度学习·神经网络·目标检测·cnn
mozun20201 小时前
YOLOv7 辅助检测头与重参数化解析2025.6.1
人工智能·yolo·目标检测·机器学习·计算机视觉·目标跟踪
zskj_zhyl3 小时前
从“被动养老”到“主动健康管理”:平台如何重构代际关系?
大数据·人工智能·重构
奔跑吧邓邓子3 小时前
DeepSeek 赋能车路协同:智能交通的破局与重构
人工智能·应用·车路协同·智能交通·deepseek
不剪发的Tony老师4 小时前
sqlite-vec:谁说SQLite不是向量数据库?
数据库·人工智能·sqlite
小猪猪_14 小时前
神经网络与深度学习(第二章)
深度学习·神经网络·机器学习
白熊1885 小时前
【机器学习基础】机器学习入门核心:Jaccard相似度 (Jaccard Index) 和 Pearson相似度 (Pearson Correlation)
人工智能·机器学习
pen-ai5 小时前
【深度学习】17. 深度生成模型:DCGAN与Wasserstein GAN公式深度推导
人工智能·深度学习·生成对抗网络
白熊1885 小时前
【机器学习基础】机器学习入门核心算法:层次聚类算法(AGNES算法和 DIANA算法)
算法·机器学习·聚类
简简单单做算法5 小时前
基于mediapipe深度学习的虚拟画板系统python源码
人工智能·python·深度学习·mediapipe·虚拟画板