声明: 1、 学生刚开始学习python,代码会有很多不严谨,也较为粗糙,单纯用于广大网友参考,希望能起到一定的帮助 2、 如果要转载,请标记出来源 3、本文纯粹用于技术练习,请勿用作非法途径 4、如果有问题请在评论区指出,虚心接受立马改正 做题途中所遇问题: 无 代码块:
在这里插入代码片
from sklearn import datasets
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.naive_bayes import BernoulliNB
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
cancers=datasets.load_breast_cancer()
X=cancers.data
Y=cancers.target
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)
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)
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)
结果:
|