sklearn中的朴素贝叶斯模型及其应用
使用朴素贝叶斯模型对iris数据集进行花分类(用三种不同类型的朴素贝叶斯),并使用sklearn.model_selection.cross_val_score()对模型进行验证
# 导入鸢尾花数据集from sklearn.datasets import load_iris# 数据选取iris_data = load_iris()['data']iris_target = load_iris()['target']# 用高斯模型进行预测并评估from sklearn.naive_bayes import GaussianNBmol = GaussianNB()result = mol.fit(iris_data,iris_target)# 对模型进行评估from sklearn.model_selection import cross_val_scorescores = cross_val_score(mol,iris_data,iris_target,cv=10)# 对预测结果的正确个数进行计算print("高斯模型:")print("数据总数:",len(iris_data)," 错误个数:",(iris_target != predi).sum())print("Accuracy:%.3f"%scores.mean())# 用贝努里模型进行预测和评估from sklearn.naive_bayes import BernoulliNBbnb = BernoulliNB()result2 = bnb.fit(iris_data,iris_target)pred2 = bnb.predict(iris_data)# 计算错误个数print("贝努里模型:")print("数据总数:",len(iris_data)," 错误个数:",(iris_target != pred2).sum())#模型评分scores2 = cross_val_score(bnb,iris_data,iris_target)print("Accuracy:%.3f"%scores2.mean())# 用多项式建立模型进行预测和评估from sklearn.naive_bayes import MultinomialNBmnb = MultinomialNB()result3 = mnb.fit(iris_data,iris_target)# 预测pred3 = result3.predict(iris_data)# 计算错误个数print("多项式模型:")print("数据总数:",iris_data.shape[0]," 错误个数:",(iris_target != pred3).sum())# 模型评分scores3 = cross_val_score(mnb,iris_data,iris_target)print("Accuracy:%.3f"%scores3.mean())
截图: