利用sklearn的Learning curve和Validation curve工具绘图评估模型的过拟合和欠拟合

3.5. Validation curves: plotting scores to evaluate models

Every estimator has its advantages and drawbacks. Its generalization error can be decomposed in terms of bias, variance and noise. The bias of an estimator is its average error for different training sets. The variance of an estimator indicates how sensitive it is to varying training sets. Noise is a property of the data.

In the following plot, we see a function f(x)=cos⁡(32πx) and some noisy samples from that function. We use three different estimators to fit the function: linear regression with polynomial features of degree 1, 4 and 15. We see that the first estimator can at best provide only a poor fit to the samples and the true function because it is too simple (high bias), the second estimator approximates it almost perfectly and the last estimator approximates the training data perfectly but does not fit the true function very well, i.e. it is very sensitive to varying training data (high variance).

 

Bias and variance are inherent properties of estimators and we usually have to select learning algorithms and hyperparameters so that both bias and variance are as low as possible (see Bias-variance dilemma). Another way to reduce the variance of a model is to use more training data. However, you should only collect more training data if the true function is too complex to be approximated by an estimator with a lower variance.

In the simple one-dimensional problem that we have seen in the example it is easy to see whether the estimator suffers from bias or variance. However, in high-dimensional spaces, models can become very difficult to visualize. For this reason, it is often helpful to use the tools described below.

Examples:

3.5.1. Validation curve

To validate a model we need a scoring function (see Metrics and scoring: quantifying the quality of predictions), for example accuracy for classifiers. The proper way of choosing multiple hyperparameters of an estimator are of course grid search or similar methods (see Tuning the hyper-parameters of an estimator) that select the hyperparameter with the maximum score on a validation set or multiple validation sets. Note that if we optimized the hyperparameters based on a validation score the validation score is biased and not a good estimate of the generalization any longer. To get a proper estimate of the generalization we have to compute the score on another test set.

However, it is sometimes helpful to plot the influence of a single hyperparameter on the training score and the validation score to find out whether the estimator is overfitting or underfitting for some hyperparameter values.

The function validation_curve can help in this case:

>>>

>>> import numpy as np
>>> from sklearn.model_selection import validation_curve
>>> from sklearn.datasets import load_iris
>>> from sklearn.linear_model import Ridge

>>> np.random.seed(0)
>>> X, y = load_iris(return_X_y=True)
>>> indices = np.arange(y.shape[0])
>>> np.random.shuffle(indices)
>>> X, y = X[indices], y[indices]

>>> train_scores, valid_scores = validation_curve(Ridge(), X, y, "alpha",
...                                               np.logspace(-7, 3, 3),
...                                               cv=5)
>>> train_scores
array([[0.93..., 0.94..., 0.92..., 0.91..., 0.92...],
       [0.93..., 0.94..., 0.92..., 0.91..., 0.92...],
       [0.51..., 0.52..., 0.49..., 0.47..., 0.49...]])
>>> valid_scores
array([[0.90..., 0.84..., 0.94..., 0.96..., 0.93...],
       [0.90..., 0.84..., 0.94..., 0.96..., 0.93...],
       [0.46..., 0.25..., 0.50..., 0.49..., 0.52...]])

If the training score and the validation score are both low, the estimator will be underfitting. If the training score is high and the validation score is low, the estimator is overfitting and otherwise it is working very well. A low training score and a high validation score is usually not possible. All three cases can be found in the plot below where we vary the parameter γ of an SVM on the digits dataset.

 

3.5.2. Learning curve

A learning curve shows the validation and training score of an estimator for varying numbers of training samples. It is a tool to find out how much we benefit from adding more training data and whether the estimator suffers more from a variance error or a bias error. Consider the following example where we plot the learning curve of a naive Bayes classifier and an SVM.

For the naive Bayes, both the validation score and the training score converge to a value that is quite low with increasing size of the training set. Thus, we will probably not benefit much from more training data.

In contrast, for small amounts of data, the training score of the SVM is much greater than the validation score. Adding more training samples will most likely increase generalization.

 

We can use the function learning_curve to generate the values that are required to plot such a learning curve (number of samples that have been used, the average scores on the training sets and the average scores on the validation sets):

>>>

>>> from sklearn.model_selection import learning_curve
>>> from sklearn.svm import SVC

>>> train_sizes, train_scores, valid_scores = learning_curve(
...     SVC(kernel='linear'), X, y, train_sizes=[50, 80, 110], cv=5)
>>> train_sizes
array([ 50, 80, 110])
>>> train_scores
array([[0.98..., 0.98 , 0.98..., 0.98..., 0.98...],
       [0.98..., 1.   , 0.98..., 0.98..., 0.98...],
       [0.98..., 1.   , 0.98..., 0.98..., 0.99...]])
>>> valid_scores
array([[1. ,  0.93...,  1. ,  1. ,  0.96...],
       [1. ,  0.96...,  1. ,  1. ,  0.96...],
       [1. ,  0.96...,  1. ,  1. ,  0.96...]])

实际项目的学习曲线绘制:

  1. from sklearn.model_selection import learning_curve
  2. def plot_learning_curve(estimator, title, X, y, axes=None, ylim=None, cv=None,
  3.                         n_jobs=None, train_sizes=np.linspace(.1, 1.0, 5)):
  4.     axes.set_title(title)
  5.     if ylim is not None:
  6.         axes.set_ylim(*ylim)
  7.     axes.set_xlabel("Training examples")
  8.     axes.set_ylabel("Score")
  9.     train_sizes, train_scores, test_scores, fit_times, _ = \
  10.         learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs,
  11.                        train_sizes=train_sizes,
  12.                        return_times=True)
  13.     train_scores_mean = np.mean(train_scores, axis=1)
  14.     train_scores_std = np.std(train_scores, axis=1)
  15.     test_scores_mean = np.mean(test_scores, axis=1)
  16.     test_scores_std = np.std(test_scores, axis=1)
  17.     fit_times_mean = np.mean(fit_times, axis=1)
  18.     fit_times_std = np.std(fit_times, axis=1)
  19.     # Plot learning curve
  20.     axes.grid()
  21.     axes.fill_between(train_sizes, train_scores_mean - train_scores_std,
  22.                          train_scores_mean + train_scores_std, alpha=0.1,
  23.                          color="r")
  24.     axes.fill_between(train_sizes, test_scores_mean - test_scores_std,
  25.                          test_scores_mean + test_scores_std, alpha=0.1,
  26.                          color="g")
  27.     axes.plot(train_sizes, train_scores_mean, 'o-', color="r",
  28.                  label="Training score")
  29.     axes.plot(train_sizes, test_scores_mean, 'o-', color="g",
  30.                  label="Cross-validation score")
  31.     axes.legend(loc="best")
  32.     
  33.     return plt

 

  1. fig, axes = plt.subplots(1, 1, figsize=(10, 15))
  2. title = "Learning Curves (GBDT) raw"
  3. # Cross validation with 100 iterations to get smoother mean test and train
  4. # score curves, each time with 20% data randomly selected as a validation set.
  5. # cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0)
  6. cv = 5
  7. estimator = gradient_boosted
  8. plot_learning_curve(estimator, title, x_train, y_train, axes = axes, ylim=(0.7, 1.01),
  9.                     cv=cv, n_jobs=4)
  10. plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值