打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
sklearn学习笔记

模型验证方法

1.学习率曲线(learn_curve)
2.交叉验证得分(cross_val_score)
3.验证曲线(validation_curve)

一.学习率曲线

计算指定的学习器模型在不同大小的训练集上经过交叉验证的训练得分和测试得分

首先,用一个交叉验证生成器划分整体数据集K次,每一次划分都有一个训练集和测试
集。然后从每次划分的训练集中拿出若干个数量不断增加的子集,在这些训练集上训
练模型。然后再计算模型在对应的子训练集和测试集上的得分,最后,对于在每种子
训练集大小下,将K次训练集得分和测试集得分分别进行平均。
  1. # import numpy as np  
  2. # from sklearn.model_selection import learning_curve, ShuffleSplit  
  3. # from sklearn.datasets import load_digits  
  4. # from sklearn.naive_bayes import GaussianNB  
  5. # from sklearn import svm  
  6. # import matplotlib.pyplot as plt  
  7. # def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,n_jobs=1, train_size=np.linspace(.1, 1.0, 5 )):  
  8. #     if __name__ == '__main__':  
  9. #         plt.figure()  
  10. #         plt.title(title)  
  11. #         if ylim is not None:  
  12. #             plt.ylim(*ylim)  
  13. #         plt.xlabel('Training example')  
  14. #         plt.ylabel('score')  
  15. #         train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_size)  
  16. #         train_scores_mean = np.mean(train_scores, axis=1)  
  17. #         train_scores_std = np.std(train_scores, axis=1)  
  18. #         test_scores_mean = np.mean(test_scores, axis=1)  
  19. #         test_scores_std = np.std(test_scores, axis=1)  
  20. #         plt.grid()#区域  
  21. #         plt.fill_between(train_sizes, train_scores_mean - train_scores_std,  
  22. #                          train_scores_mean + train_scores_std, alpha=0.1,  
  23. #                          color="r")  
  24. #         plt.fill_between(train_sizes, test_scores_mean - test_scores_std,  
  25. #                          test_scores_mean + test_scores_std, alpha=0.1,  
  26. #                          color="g")  
  27. #         plt.plot(train_sizes, train_scores_mean, 'o-', color='r',  
  28. #                  label="Training score")  
  29. #         plt.plot(train_sizes, test_scores_mean,'o-',color="g",  
  30. #                  label="Cross-validation score")  
  31. #         plt.legend(loc="best")  
  32. #         return plt  
  33. # digits = load_digits()  
  34. # X = digits.data  
  35. # y = digits.target  
  36. # cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0)#切割100ci  
  37. # estimator = GaussianNB()  
  38. # title = "Learning Curves(naive_bayes)"  
  39. # plot_learning_curve(estimator, title, X, y, ylim=(0.7, 1.01), cv=cv, n_jobs=4)  
  40. # title = "Learning Curves(SVM,RBF kernel, $\gamma=0.001$)"  
  41. # cv = ShuffleSplit(n_splits=10, test_size=0.2, random_state=0)#交叉验证传入别的方法,而不是默认的k折交叉验证  
  42. # estimator = svm.SVC(gamma=0.001)  
  43. # plot_learning_curve(estimator, title, X, y, (0.7, 1.01), cv=cv, n_jobs=4)  
  44. # plt.show()  


二.交叉验证得分

  1. # import matplotlib.pyplot as plt  
  2. # from sklearn.model_selection import cross_val_score  
  3. # import numpy as np  
  4. # from sklearn import datasets, svm  
  5. # digits = datasets.load_digits()  
  6. # x = digits.data  
  7. # y = digits.target  
  8. # vsc = svm.SVC(kernel='linear')  
  9. # if __name__=='__main__':  
  10. #     c_S = np.logspace(-10, 0, 10)#在范围内取是个对数  
  11. #     # print ("length", len(c_S))  
  12. #     scores = list()  
  13. #     scores_std = list()  
  14. #     for c in c_S:  
  15. #         vsc.C = c  
  16. #         this_scores = cross_val_score(vsc, x, y, n_jobs=4)#多线程 n_jobs,默认三次交叉验证  
  17. #         scores.append(np.mean(this_scores))  
  18. #         scores_std.append(np.std(this_scores))  
  19. #     plt.figure(1, figsize=(4, 3))#绘图  
  20. #     plt.clf()  
  21. #     plt.semilogx(c_S, scores)#划线  
  22. #     plt.semilogx(c_S, np.array(scores)+np.array(scores_std), 'b--')  
  23. #     plt.semilogx(c_S, np.array(scores)-np.array(scores_std), 'b--')  
  24. #     locs, labels = plt.yticks()  
  25. #     plt.yticks(locs, list(map(lambda X: "%g" % X, locs)))#阶段点  
  26. #     plt.ylabel('CV score')  
  27. #     plt.xlabel('parameter C')  
  28. #     plt.ylim(0, 1.1)#范围  
  29. #     plt.show()  

三.验证曲线

当某个参数不断变化是,在每一个取值上计算出的模型在训练集和测试集上的得分
在一个不断变化的参数上计算学习器的得分,类似于只有一个参数的网格搜索,但是这个函数也会计算训练集上的得分
  1. # from sklearn import svm  
  2. # from sklearn.model_selection import validation_curve  
  3. # from sklearn.datasets import load_digits  
  4. # import numpy as np  
  5. # import matplotlib.pyplot as plt  
  6. # digits = load_digits()  
  7. # X = digits.data  
  8. # y = digits.target  
  9. # param_range = np.logspace(-6, -1, 5)  
  10. # vsc = svm.SVC()  
  11. # train_score, test_score = validation_curve(vsc, X, y, param_name='gamma', param_range=param_range, cv=10, scoring="accuracy", n_jobs=1)  
  12. # train_score_mean = np.mean(train_score, axis=1)  
  13. # train_score_std = np.std(train_score, axis=1)  
  14. # test_score_mean = np.mean(test_score, axis=1)  
  15. # test_score_std = np.std(test_score, axis=1)  
  16. # plt.title("validation curve with SVM")  
  17. # plt.xlabel("$\gamma%")  
  18. # plt.ylabel("Score")  
  19. # plt.ylim()  
  20. # lw = 2  
  21. # plt.semilogx(param_range, train_score_mean,label="training score", color="darkorange", lw=lw)  
  22. # plt.fill_between(param_range, train_score_mean-train_score_std, train_score_mean+train_score_std, alpha=0.2, color="navy", lw=lw)  
  23. # plt.legend(loc="best")  
  24. # plt.show()  





本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
万字长文总结机器学习的模型评估与调参,附代码下载
Python遇见机器学习 ---- 逻辑回归 Logistic Regression
sklearn库的学习
机器学习入门实践——鸢尾花分类
9种常用的机器学习算法实现
Python机器学习(十)经典算法大全
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服