机器学习贝叶斯测试(威斯康星乳腺肿瘤数据集)

机器学习贝叶斯测试(威斯康星乳腺肿瘤数据集),第1张

#导入威斯康星乳腺肿瘤数据集
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split  #导入分类库
cancer = load_breast_cancer()
#打印数据集的键值
cancer.keys()
print('肿瘤的分类:',cancer['target_names'])
print('\n肿瘤的特征:',cancer['feature_names'])
#将数据集的数值和分类目标赋值给X和y
X, y = cancer.data, cancer.target
#将数据集拆分为训练集和测试集
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=38)
print('训练集数据形态:',X_train.shape)
print('测试集数据形态:',X_test.shape)

#导入高斯贝叶斯模型
from sklearn.naive_bayes import GaussianNB
#用高斯贝叶斯模型拟合数据
gnb = GaussianNB()
gnb.fit(X_train,y_train)
print('模型测试集得分:{:.3f}'.format(gnb.score(X_train,y_train)))
print('模型训练集得分:{:.3f}'.format(gnb.score(X_test,y_test)))
print('模型预测的分类是:{}'.format(gnb.predict([X[312]])))
print('样本的正确分类是:{}',y[312])

#绘制高斯朴素贝叶斯在威斯康星乳腺肿瘤数据集中的学习曲线
from sklearn.model_selection import learning_curve #导入学习曲线库
from sklearn.model_selection import ShuffleSplit #导入随即拆分工具

def plot_learning_curve(estimator, title,X, y, ylim=None,cv=None,
                        n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(
        estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    plt.grid()
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             lable="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             lable="Cross-validation score")
    plt.legend(loc="lower right")
    return plt

title = "Learning Curves(Naive Bayes)"
cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0)
estimator = GaussianNB()
plot_learning_curve(estimator, title,X, y, ylim=(0.9, 1.01), cv=cv, n_jobs=4)
plt.show()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/883712.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-14
下一篇 2022-05-14

发表评论

登录后才能评论

评论列表(0条)

保存