码迷,mamicode.com
首页 > 其他好文 > 详细

高斯贝叶斯分类器(GNB实战)

时间:2020-02-03 18:56:07      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:dataset   div   gauss   ati   ini   not   core   code   gaussian   

代码含学习曲线绘制。

 1 from sklearn.datasets import load_breast_cancer
 2 data=load_breast_cancer()
 3 X,y=data.data,data.target
 4 
 5 from sklearn.model_selection import train_test_split
 6 X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=38)
 7 
 8 from sklearn.naive_bayes import GaussianNB
 9 gnb=GaussianNB().fit(X_train,y_train)
10 print("the score of this model on training set:{}".format(gnb.score(X_train,y_train)))
11 print("the score of this model on test set:{}".format(gnb.score(X_test,y_test)))
 1 from sklearn.model_selection import learning_curve
 2 from matplotlib import pyplot as plt
 3 import numpy as np
 4 def plot_learning_curve(estimator,title,X,y,
 5                         ylim=None,cv=None,n_jobs=1,
 6                         train_sizes=np.linspace(.1,1.0,5)):
 7     plt.figure()
 8     plt.title(title)
 9     if ylim is not None:
10         plt.ylim(*ylim)
11     plt.xlabel("Training Examples")
12     plt.ylabel("Score")
13     train_sizes,train_scores,test_scores=learning_curve(
14         estimator,X,y,cv=cv,n_jobs=n_jobs,train_sizes=train_sizes
15     )
16     train_scores_mean=np.mean(train_scores,axis=1)
17     test_scores_mean=np.mean(test_scores,axis=1)
18     plt.grid()
19     plt.plot(train_sizes, train_scores_mean, o-, color="r", label="Training Score")
20     plt.plot(train_sizes, test_scores_mean, o-, color="g", label="Cross-validation Score")
21     plt.legend(loc="lower right")
22     return plt

 

高斯贝叶斯分类器(GNB实战)

标签:dataset   div   gauss   ati   ini   not   core   code   gaussian   

原文地址:https://www.cnblogs.com/St-Lovaer/p/12245957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!