朴素贝叶斯算法
输入:样本集合D={(x_1,y_1),(x_2,y_2)
(x_m,y_m); 待预测样本x; 样本标记的所有可能取值{c_1,c_2,c_3
c_k}; 样本输入变量X的每个属性变量X^i的所有可能取值{a_i1,a_i2,~,a_iAi}; 输出:待预测样本x所属的类别
1.计算标记为c_k的样本出现概率。
2.计算标记c_k的样本,其X^i分量的属性值为a_ip的概率。
3.根据上面的估计值计算x属于y_k的概率值,并选择概率最大的作为输出。
1.使用sklearn的朴素贝叶斯算法对垃圾短信数据集进行分类
要求:
(1)划分训练集和测试集(测试集占20%) (2)对测试集的预测类别标签和真实标签进行对比 (3)掌握特征提取方法 (4)输出分类的准确率
代码:
from sklearn.feature_extraction.text import CountVectorizer as CV from sklearn.model_selection import train_test_split from sklearn.naive_bayes import MultinomialNB as NB import pandas as pd # 加载SMS垃圾短息数据集 with open('SMSSpamCollection.txt', 'r', encoding='utf8') as f: sms = [line.split('\t') for line in f] y, x = zip(*sms) # SMS垃圾短息数据集的特征提取 y = [label == 'spam' for label in y] x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0) counter = CV(token_pattern='[a-zA-Z]{2,}') x_train = counter.fit_transform(x_train) x_test = counter.transform(x_test) # 朴素贝叶斯分类器的构造与训练 model = NB() model.fit(x_train, y_train) train_score = model.score(x_train, y_train) test_score = model.score(x_test, y_test) print('train score:', train_score) print('test score:', test_score) # 对测试集的预测类别标签和真实标签进行对比 y_predict = model.predict(x_test) print('测试集的预测类别标签与真实标签的对比:\n', pd.concat([pd.DataFrame(x_test), pd.DataFrame(y_test), pd.DataFrame(y_predict)], axis=1