利用Scikit Learn库进行垃圾邮件过滤

模型:采用朴素贝叶斯、逻辑回归和多层感知机3个模型。

输出:画出混淆矩阵,计算准确率、精准率、召回率。

精度:至少一个模型的准确率>0.96

使用朴素贝叶斯模型

代码如下:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report
# 二元分类指标
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

# 通过read_table给分割成两份的信息设置标签
df = pd.read_table('./SMSSpamCollection', sep='\t', names=['label', 'sms_message'])

# 需要首先将label标签都转换成数字,0表示有用邮件,1表示垃圾邮件
df['label'] = df.label.map({'ham':0, 'spam':1})
# 将数据划分成训练集和测试集,其中因为test_size=0.2时,精确率达到了0.99,所以我便设置了test_size=0.2
X_train, X_test, y_train, y_test = train_test_split(df['sms_message'],
                                                    df['label'],
                                                    test_size=0.2,
                                                    random_state=1)
# 统计一下邮件总数、训练集大小、测试集大小
print('Number of rows in the total set: {}'.format(df.shape[0]))
print('Number of rows in the training set: {}'.format(X_train.shape[0]))
print('Number of rows in the test set: {}'.format(X_test.shape[0]))

# 对数据进行特征向量的提取,这里使用的是CountVectorizer,一种相对简单的提取方法
count_vector = CountVectorizer()
# 拟合训练数据,然后返回矩阵
training_data = count_vector.fit_transform(X_train)
# 转换测试数据并返回矩阵
testing_data = count_vector.transform(X_test)

# 通过朴素贝叶斯对训练集进行拟合,以及预测
naive_bayes = MultinomialNB()
naive_bayes.fit(training_data, y_train)
predictions = naive_bayes.predict(testing_data)

# 输出该模型的准确率、精准率、召回率、F1值
print('Accuracy score: ', format(accuracy_score(y_test, predictions)))
print('Precision score: ', format(precision_score(y_test, predictions)))
print('Recall score: ', format(recall_score(y_test, predictions)))
print('F1 score: ', format(f1_score(y_test, predictions)))
print(classification_report(y_test, predictions))

# predictions 与 y_test
confusion_matrix = confusion_matrix(y_test, predictions)
print(confusion_matrix)
plt.matshow(confusion_matrix)
plt.title("混淆矩阵", fontproperties="SimSun", size=18)
plt.colorbar()
plt.ylabel("真实值", fontproperties="SimSun", size=18)
plt.xlabel("预测值", fontproperties="SimSun", size=18)
plt.show()

运行之后的结果为:

在这里插入图片描述

在这里插入图片描述

使用逻辑回归模型

代码如下:

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report, roc_curve, auc
# 二元分类分类指标
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

df = pd.read_csv('./SMSSpamCollection', delimiter='\t', header=None)  # 标签和数据之间用\t分隔

# 首先统计垃圾邮件和正常邮件的数量
print("垃圾邮件个数:%s" % df[df[0] == 'spam'][0].count())
print("正常邮件个数:%s" % df[df[0] == 'ham'][0].count())

X = df[1].values
y = df[0].values
X_train_raw, X_test_raw, y_train, y_test = train_test_split(X, y, test_size=0.1)

# 用TF—IDF算法来提取邮件的特征向量
vectorizer = TfidfVectorizer()
X_train = vectorizer.fit_transform(X_train_raw)
X_test = vectorizer.transform(X_test_raw)

# 用逻辑回归对训练集进行拟合,以及预测
LR = LogisticRegression()
LR.fit(X_train, y_train)
predictions = LR.predict(X_test)

# 给出 precision  recall  f1-score   support
print(classification_report(y_test, predictions))
# 准确率
scores = cross_val_score(LR, X_train, y_train, cv=10)
print("准确率为: ", scores)
print("平均准确率为: ", scores.mean())
# 有时必须要将标签转为数值
class_le = LabelEncoder()
y_train_n = class_le.fit_transform(y_train)
y_test_n = class_le.fit_transform(y_test)
# 精准率
precision = cross_val_score(LR, X_train, y_train_n, cv=10, scoring='precision')
print("平均精准率为: ", precision.mean())
# 召回率
recall = cross_val_score(LR, X_train, y_train_n, cv=10, scoring='recall')
print("平均召回率为: ", recall.mean())
# F1值
f1 = cross_val_score(LR, X_train, y_train_n, cv=10, scoring='f1')
print("平均F1值为: ", f1.mean())

# predictions 与 y_test
confusion_matrix = confusion_matrix(y_test, predictions)
print(confusion_matrix)
plt.matshow(confusion_matrix)
plt.title("混淆矩阵", fontproperties="SimSun", size=18)
plt.colorbar()
plt.ylabel("真实值", fontproperties="SimSun", size=18)
plt.xlabel("预测值", fontproperties="SimSun", size=18)
plt.show()

# ROC曲线 y_test_n为数值
predictions_pro = LR.predict_proba(X_test)
false_positive_rate, recall, thresholds = roc_curve(y_test_n, predictions_pro[:,1])
roc_auc = auc(false_positive_rate, recall)
plt.title("受试者操作特征曲线(ROC)", fontproperties="SimSun", size=18)
plt.plot(false_positive_rate, recall, 'b', label='AUC = % 0.2f' % roc_auc)
plt.legend(loc='lower right')
plt.plot([0,1], [0,1], 'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('假阳性率', fontproperties="SimSun", size=18)
plt.ylabel('召回率', fontproperties="SimSun", size=18)
plt.show()

运行之后的结果为:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

使用多层感知机模型

代码如下:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report

# 二元分类分类指标
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

# 通过read_table给分割成两份的信息设置标签
df = pd.read_table('./SMSSpamCollection', sep='\t', names=['label', 'sms_message'])

# 需要首先将label标签都转换成数字,0表示有用邮件,1表示垃圾邮件
df['label'] = df.label.map({'ham':0, 'spam':1})
# 将数据划分成训练集和测试集,其中因为test_size=0.2时,精确率达到了0.99,所以我便设置了test_size=0.2
X_train, X_test, y_train, y_test = train_test_split(df['sms_message'],
                                                    df['label'],
                                                    test_size=0.2,
                                                    random_state=1)
# 统计一下邮件总数、训练集大小、测试集大小
print('Number of rows in the total set: {}'.format(df.shape[0]))
print('Number of rows in the training set: {}'.format(X_train.shape[0]))
print('Number of rows in the test set: {}'.format(X_test.shape[0]))

# 用TF—IDF算法来提取邮件的特征向量
# 这里我一开始是使用CountVectorizer来提取特征向量的,但是不知道为什么,后面求精确率这些值的时候报错了,然后我又找到另一种比CountVectorizer更好的提取向量的方法,即TfidfVectorizer,而且后面的求精确率这些值也是没问题的
vectorizer = TfidfVectorizer()
X_train = vectorizer.fit_transform(X_train)
X_test = vectorizer.transform(X_test)

# 神经网络输入为2,第一隐藏层神经元个数为5,第二隐藏层神经元个数为2,输出结果为2分类。
# solver='lbfgs',  MLP的求解方法:L-BFGS 在小数据上表现较好,Adam 较为鲁棒,
# SGD在参数调整较优时会有最佳表现(分类效果与迭代次数),SGD标识随机梯度下降。
mlp = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(30, 20), random_state=1)
mlp.fit(X_train, y_train)
predictions = mlp.predict(X_test)

# 输出该模型的准确率、精准率、召回率、F1值
print('Accuracy score: ', format(accuracy_score(y_test, predictions)))
print('Precision score: ', format(precision_score(y_test, predictions)))
print('Recall score: ', format(recall_score(y_test, predictions)))
print('F1 score: ', format(f1_score(y_test, predictions)))
print(classification_report(y_test, predictions))

# predictions 与 y_test
confusion_matrix = confusion_matrix(y_test, predictions)
print(confusion_matrix)
plt.matshow(confusion_matrix)
plt.title("混淆矩阵", fontproperties="SimSun", size=18)
plt.colorbar()
plt.ylabel("真实值", fontproperties="SimSun", size=18)
plt.xlabel("预测值", fontproperties="SimSun", size=18)
plt.show()

运行之后的结果为:
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花无凋零之时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值