声明:
1、 学生刚开始学习python,代码会有很多不严谨,也较为粗糙,单纯用于广大网友参考,希望能起到一定的帮助
2、 如果要转载,请标记出来源
3、本文纯粹用于技术练习,请勿用作非法途径
4、如果有问题请在评论区指出,虚心接受立马改正
做题途中所遇问题:
无
代码块:
在这里插入代码片
#4、导入sklearn库自带的乳腺癌数据集,分别使用GaussianNB、MultinomialNb、BernouliNB、SVM及KNN5种分类器进行分类预测,并比较输出5种分类器预测的准确率优劣。
#sklearn数据库中的乳腺癌采集数据
from sklearn import datasets
#朴素贝叶斯
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.naive_bayes import BernoulliNB
#SVM
from sklearn.svm import SVC
#KNN
from sklearn.neighbors import KNeighborsClassifier
#数据集分割
from sklearn.model_selection import train_test_split
cancers=datasets.load_breast_cancer()
X=cancers.data
Y=cancers.target
# 注意返回值: 训练集train,x_train,y_train,测试集test,x_test,y_test
# x_train为训练集的特征值,y_train为训练集的目标值,x_test为测试集的特征值,y_test为测试集的目标值
# 注意,接收参数的顺序固定
# 训练集占80%,测试集占20%
#此处是将数据集拆分为训练集和测试集
x_train,x_test,y_train,y_test=train_test_split(X, Y, test_size=0.2)
#朴素贝叶斯
#高斯贝叶斯分类器
model_linear =GaussianNB()
model_linear.fit(x_train, y_train)
train_score = model_linear.score(x_train, y_train)
test_score = model_linear.score(x_test, y_test)
print('高斯贝叶斯训练集的准确率:%.3f; 测试集的准确率:%.3f'%(train_score, test_score))
preresult=model_linear.predict(x_test)
print(preresult)
#多项式贝叶斯分类器
model_linear =MultinomialNB()
model_linear.fit(x_train, y_train)
train_score = model_linear.score(x_train, y_train)
test_score = model_linear.score(x_test, y_test)
print('多项式贝叶斯训练集的准确率:%.3f; 测试集的准确率:%.3f'%(train_score, test_score))
preresult=model_linear.predict(x_test)
print(preresult)
#伯努利贝叶斯分类器
model_linear=BernoulliNB()
model_linear.fit(x_train, y_train)
train_score = model_linear.score(x_train, y_train)
test_score = model_linear.score(x_test, y_test)
print('伯努利贝叶斯训练集的准确率:%.3f; 测试集的准确率:%.3f'%(train_score, test_score))
preresult=model_linear.predict(x_test)
print(preresult)
#SVM法
model_linear = SVC(C=1.0, kernel='linear') # 线性核
model_linear.fit(x_train, y_train)
train_score = model_linear.score(x_train, y_train)
test_score = model_linear.score(x_test, y_test)
print('SVM法训练集的准确率:%.3f; 测试集的准确率:%.3f'%(train_score, test_score))
preresult=model_linear.predict(x_test)
print(preresult)
#KNN法
model_linear =KNeighborsClassifier(n_neighbors=15)
model_linear.fit(x_train, y_train)
train_score = model_linear.score(x_train, y_train)
test_score = model_linear.score(x_test, y_test)
print('KNN法训练集的准确率:%.3f; 测试集的准确率:%.3f'%(train_score, test_score))
preresult=model_linear.predict(x_test)
print(preresult)
结果: