#导入威斯康星乳腺肿瘤数据集import matplotlib.pyplot as pltimport numpy as npfrom sklearn.datasets import load_breast_cancerfrom sklearn.model_selection import train_test_split #导入分类库cancer = load_breast_cancer()#打印数据集的键值cancer.keys()print('肿瘤的分类:',cancer['target_names'])print('\n肿瘤的特征:',cancer['feature_names'])#将数据集的数值和分类目标赋值给X和yX, 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 plttitle = "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()
如果觉得《机器学习贝叶斯测试(威斯康星乳腺肿瘤数据集)》对你有帮助,请点赞、收藏,并留下你的观点哦!