基于朴素贝叶斯+Python实现垃圾邮件分类和结果分析

本文介绍了使用朴素贝叶斯原理和Python实现垃圾邮件分类的方法,并详细分析了不同P(not_appear)设置下误判情况,展示了误判次数与总误判次数的关系,以及误判邮件的成分分析。
摘要由CSDN通过智能技术生成

基于朴素贝叶斯+Python实现垃圾邮件分类

朴素贝叶斯原理

请参考: 贝叶斯推断及其互联网应用(二):过滤垃圾邮件

Python实现

源代码主干来自: python实现贝叶斯推断——垃圾邮件分类

我只是加了注释,然后做了对结果的分析统计的输出添加。

源码下载: GitHub:下载NaiveBayesEmail.py

本文原载: 基于朴素贝叶斯+Python实现垃圾邮件分类

结果分析

仅出现在垃圾邮件(或非垃圾邮件)中的单词在非垃圾邮件(或垃圾邮件)中的概率设为P(not_appear)

1)P(not_appear) = 0.01时的结果:

去停用词结果:
去了停用词

不去停用词结果:
不去停用词

2)P(not_appear) = 0.05时的结果:

去停用词结果:
去停用词

不去停用词结果:
不去停用词

可见,

  • 去不去停用词差别不大;
  • P(not_appear) 越大越会把spam误判成ham。

3)[把垃圾邮件误判成非垃圾邮件的次数,总误判次数] 对应关系查看

  • Rate of mistaking spam for ham in 100 times when P(not_appear) = 0.05 without stopwords removal.

  • [wrong_spamToham, wrong]

结果1:
[[1, 1], [1, 1], [2, 2], [2, 2], [2, 2], [3, 3], [1, 1], [2, 2], [2, 2], [4, 4], [3, 3], [1, 1], [1, 1], [2, 2], [5, 5], [1, 1], [1, 1], [1, 1], [1, 1], [2, 2], [2, 2], [-1], [2, 2], [1, 1], [2, 2], [-1], [3, 3], [2, 2], [1, 1], [1, 1], [2, 2], [-1], [4, 4], [1, 1], [3, 3], [2, 2], [2, 2], [3, 3], [2, 2], [3, 3], [2, 2], [2, 2], [1, 1], [1, 1], [-1], [1, 1], [1, 1], [2, 2], [-1], [2, 2], [1, 1], [2, 2], [1, 1], [-1], [2, 2], [2, 2], [2, 2], [3, 3], [4, 4], [1, 1], [2, 2], [1, 1], [2, 2], [3, 3], [3, 3], [-1], [3, 3], [2, 2], [2, 2], [2, 2], [2, 2], [3, 3], [3, 3], [2, 2], [5, 5], [2, 2], [-1], [4, 4], [3, 3], [4, 4], [1, 1], [3, 3], [1, 1], [1, 1], [-1], [1, 1], [1, 1], [1, 1], [3, 3], [2, 2], [1, 1], [2, 2], [4, 4], [2, 2], [3, 3], [3, 3], [2, 2], [1, 1], [2, 2], [1, 1]]

结果2:
[[1, 1], [1, 1], [4, 4], [1, 1], [2, 2], [1, 1], [3, 3], [-1], [-1], [4, 4], [1, 1], [2, 2], [-1], [3, 3], [5, 5], [2, 2], [1, 1]

可以使用Python中的numpy和pandas库来实现基于朴素贝叶斯垃圾邮件分类。以下是一个简单的实现代码: ```python import numpy as np import pandas as pd # 读取数据 data = pd.read_csv('spam.csv', encoding='latin-1') data = data[['v1', 'v2']] data = data.rename(columns={'v1': 'label', 'v2': 'text'}) # 分割数据集 train_data = data.sample(frac=0.8, random_state=1) test_data = data.drop(train_data.index) # 计算先验概率 spam_count = train_data['label'].value_counts()['spam'] ham_count = train_data['label'].value_counts()['ham'] total_count = len(train_data) p_spam = spam_count / total_count p_ham = ham_count / total_count # 计算条件概率 spam_words = [] ham_words = [] for index, row in train_data.iterrows(): words = row['text'].split() if row['label'] == 'spam': spam_words += words else: ham_words += words spam_word_count = len(spam_words) ham_word_count = len(ham_words) spam_word_dict = {} ham_word_dict = {} for word in set(spam_words + ham_words): spam_word_dict[word] = (spam_words.count(word) + 1) / (spam_word_count + len(set(spam_words + ham_words))) ham_word_dict[word] = (ham_words.count(word) + 1) / (ham_word_count + len(set(spam_words + ham_words))) # 预测 def predict(text): words = text.split() p_spam_given_text = p_spam p_ham_given_text = p_ham for word in words: if word in spam_word_dict: p_spam_given_text *= spam_word_dict[word] else: p_spam_given_text *= 1 / (spam_word_count + len(set(spam_words + ham_words))) if word in ham_word_dict: p_ham_given_text *= ham_word_dict[word] else: p_ham_given_text *= 1 / (ham_word_count + len(set(spam_words + ham_words))) if p_spam_given_text > p_ham_given_text: return 'spam' else: return 'ham' ``` 其中,`data`是一个包含标签和文本的数据集,`train_data`和`test_data`是将数据集分割成训练集和测试集的结果。`p_spam`和`p_ham`是先验概率,`spam_word_dict`和`ham_word_dict`是条件概率。`predict`函数可以对新的文本进行分类。 需要注意的是,这只是一个简单的实现,还有很多可以优化的地方。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值