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

代码含学习曲线绘制。

 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

 

上一篇:FastAPI(27)- Handling Errors 处理错误


下一篇:励志---决定你一生成就的21个信念及要点