博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学习曲线
阅读量:7046 次
发布时间:2019-06-28

本文共 3738 字,大约阅读时间需要 12 分钟。

 

import numpy as npimport matplotlib.pyplot as pltfrom sklearn.naive_bayes import GaussianNBfrom sklearn.svm import SVCfrom sklearn.datasets import load_digitsfrom sklearn.model_selection import learning_curvefrom sklearn.model_selection import ShuffleSplitdef plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,                        n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):    """    Generate a simple plot of the test and training learning curve.    Parameters    ----------    estimator : object type that implements the "fit" and "predict" methods        An object of that type which is cloned for each validation.    title : string        Title for the chart.    X : array-like, shape (n_samples, n_features)        Training vector, where n_samples is the number of samples and        n_features is the number of features.    y : array-like, shape (n_samples) or (n_samples, n_features), optional        Target relative to X for classification or regression;        None for unsupervised learning.    ylim : tuple, shape (ymin, ymax), optional        Defines minimum and maximum yvalues plotted.    cv : int, cross-validation generator or an iterable, optional        Determines the cross-validation splitting strategy.        Possible inputs for cv are:          - None, to use the default 3-fold cross-validation,          - integer, to specify the number of folds.          - An object to be used as a cross-validation generator.          - An iterable yielding train/test splits.        For integer/None inputs, if ``y`` is binary or multiclass,        :class:`StratifiedKFold` used. If the estimator is not a classifier        or if ``y`` is neither binary nor multiclass, :class:`KFold` is used.        Refer :ref:`User Guide 
` for the various cross-validators that can be used here. n_jobs : integer, optional Number of jobs to run in parallel (default 1). """ 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) train_scores_std = np.std(train_scores, axis=1) test_scores_mean = np.mean(test_scores, axis=1) test_scores_std = np.std(test_scores, axis=1) plt.grid() plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.1, color="r") plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.1, color="g") plt.plot(train_sizes, train_scores_mean, 'o-', color="r", label="Training score") plt.plot(train_sizes, test_scores_mean, 'o-', color="g", label="Cross-validation score") plt.legend(loc="best") return pltdigits = load_digits()X, y = digits.data, digits.targettitle = "Learning Curves (Naive Bayes)"# Cross validation with 100 iterations to get smoother mean test and train# score curves, each time with 20% data randomly selected as a validation set.cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0)estimator = GaussianNB()plot_learning_curve(estimator, title, X, y, ylim=(0.7, 1.01), cv=cv, n_jobs=4)title = "Learning Curves (SVM, RBF kernel, $\gamma=0.001$)"# SVC is more expensive so we do a lower number of CV iterations:cv = ShuffleSplit(n_splits=10, test_size=0.2, random_state=0)estimator = SVC(gamma=0.001)plot_learning_curve(estimator, title, X, y, (0.7, 1.01), cv=cv, n_jobs=4)plt.show()

 

转载于:https://www.cnblogs.com/similarface/p/6290053.html

你可能感兴趣的文章
MongoDB(三):创建、更新及删除文档
查看>>
Maven项目部署问题
查看>>
javax.servlet包加载不了
查看>>
matlab-线性代数 齐次方程组 判断是否有无穷多解
查看>>
myeclipse建立maven项目
查看>>
DKHadoop集群添加节点的图文步骤说明
查看>>
6、旋转数组的最小位置------------>剑指offer系列
查看>>
php中关于socket函数无法使用问题
查看>>
前端面试基础题
查看>>
C#串口通信总结
查看>>
三点提高你的网站的用户体验
查看>>
最近的工作反思 2017-08-31
查看>>
VirtualBox 主机与虚拟机互通
查看>>
css06背景图片
查看>>
TYVJ P1088 treat Label:鞭笞人的DP
查看>>
nyoj429 骨牌铺方格
查看>>
ttt
查看>>
日语能力考试N2级核心词汇必备—接续词
查看>>
【第42题】【062题库】2019年OCP认证062考试新题
查看>>
Slider Page Control
查看>>