Machine Learning - Spam Recognition - SVM, MNB Model Use

In this case, the SVM and MNB models are used for comparison.

Evaluate with Accuracy, F1 Score

Not much to say, go directly to the code, including the data set download

CSDNicon-default.png?t=M276https://mp.csdn.net/mp_download/manage/download/UpDetailed


# 读取数据并用空字符串替换空值
df1 = pd.read_csv("spamham.csv")
df = df1.where((pd.notnull(df1)), '')

# 将垃圾邮件分类为 0,将非垃圾邮件分类为 1
df.loc[df["Category"] == 'ham', "Category",] = 1
df.loc[df["Category"] == 'spam', "Category",] = 0
# 将数据拆分为标签和文本。系统应该能够根据文本预测标签
df_x = df['Message']
df_y = df['Category']
# 拆分表格 - 80% 用于训练,20% 用于测试大小
x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, train_size=0.8, test_size=0.2, random_state=4)

# 使用 Tfidf 矢量化器 进行特征提取、小写转换和停用词去除
tfvec = TfidfVectorizer(min_df=1, stop_words='english', lowercase=True)
x_trainFeat = tfvec.fit_transform(x_train)
x_testFeat = tfvec.transform(x_test)

# SVM 用于建模
y_trainSvm = y_train.astype('int')
classifierModel = LinearSVC()
classifierModel.fit(x_trainFeat, y_trainSvm)
predResult = classifierModel.predict(x_testFeat)

# GNB 用于建模
y_trainGnb = y_train.astype('int')
classifierModel2 = MultinomialNB()
classifierModel2.fit(x_trainFeat, y_trainGnb)
predResult2 = classifierModel2.predict(x_testFeat)

# 计算精度,转换为 int - 解决 - 无法处理未知和二进制的混合
y_test = y_test.astype('int')
#actual_Y = y_test.as_matrix()

print("~~~~~~~~~~SVM RESULTS~~~~~~~~~~")
#使用 SVM 的准确度得分
print("Accuracy Score : {0:.4f}".format(accuracy_score(y_test, predResult)*100))
#使用 SVM 的 FScore
print("F Score: {0: .4f}".format(f1_score(y_test, predResult, average='macro')*100))
cmSVM=confusion_matrix(y_test, predResult)
#“[真阴性假阳性\假阴性真阳性]”
print("Confusion matrix:")
print(cmSVM)


print("~~~~~~~~~~MNB RESULTS~~~~~~~~~~")
#使用 MNB 的准确度得分
print("Accuracy Score: {0:.4f}".format(accuracy_score(y_test, predResult2)*100))
#使用 MNB 的 FScore
print("F Score:{0: .4f}".format(f1_score(y_test, predResult2, average='macro')*100))
cmMNb=confusion_matrix(y_test, predResult2)
#“[真阴性假阳性\假阴性真阳性]”
print("Confusion matrix:")
print(cmMNb)

operation result: 

Summarize:

Nominal type: The result of the nominal type target variable only takes values ​​in a limited target set, such as true and false (the nominal type target variable is mainly used for classification)

Numerical: Numerical target variables can take values ​​from an infinite set of values, such as 0.100, 42.001, etc. (numeric target variables are mainly used for regression analysis)

Naive Bayes

优点:在数据较少的情况下仍然有效,可以处理多类别问题。
缺点:对于输入数据的准备方式较为敏感。
适用数据类型:标称型数据

一般流程:
(1) 收集数据:可以使用任何方法。
(2) 准备数据:需要数值型或者布尔型数据。
(3) 分析数据:有大量特征时,绘制特征作用不大,此时使用直方图效果更好。
(4) 训练算法:计算不同的独立特征的条件概率。
(5) 测试算法:计算错误率。
(6) 使用算法:一个常见的朴素贝叶斯应用是文档分类。可以在任意的分类场景中使用朴
素贝叶斯分类器,不一定非要是文本。

Support Vector Machines

优点:泛化错误率低,计算开销不大,结果易解释。
缺点:对参数调节和核函数的选择敏感,原始分类器不加修改仅适用于处理二类问题。
适用数据类型:数值型和标称型数据。

一般流程:
(1) 收集数据:可以使用任意方法。 
(2) 准备数据:需要数值型数据。 
(3) 分析数据:有助于可视化分隔超平面。 
(4) 训练算法:SVM的大部分时间都源自训练,该过程主要实现两个参数的调优。 
(5) 测试算法:十分简单的计算过程就可以实现。 
(6) 使用算法:几乎所有分类问题都可以使用SVM,值得一提的是,SVM本身是一个二类
分类器,对多类问题应用SVM需要对代码做一些修改。

Guess you like

Origin blog.csdn.net/qq_21402983/article/details/124159772