中文文檔: http://sklearn.apachecn.org/cn/stable/modules/cross_validation.html
英文文檔: http://sklearn.apachecn.org/en/stable/modules/cross_validation.html
官方文檔: http://scikit-learn.org/stable/
GitHub: https://github.com/apachecn/scikit-learn-doc-zh(覺得不錯麻煩給個 Star,我們一直在努力)
貢獻者: https://github.com/apachecn/scikit-learn-doc-zh#貢獻者
關於我們: http://www.apachecn.org/organization/209.html
注意: 該文檔正在翻譯中 。。。
3.1. 交叉驗證: 評估(衡量)機器學習模型的性能
學習一個預測函數的參數,並在相同數據集上進行測試是一種錯誤的做法: 一個僅給出測試用例標籤的模型將會獲得極高的分數,但對於尚未出現過的數據 它則無法預測出任何有用的信息。 這種情況稱爲“過擬合”(overfitting). 爲了避免這種情況,在進行(監督)機器學習實驗時,通常取出部分可利用數據作爲實驗測試集(test set): X_test, y_test
。
需要強調的是這裏說的“實驗(experiment)”並不僅限於學術(academic),因爲即使是在商業場景下機器學習也往往是從實驗開始的。
利用scikit-learn包中的`train_test_split`輔助函數可以很快地將實驗數據集劃分爲任何訓練集(training sets)和測試集(test sets)。 下面讓我們載入 iris 數據集,並在此數據集上訓練出線性支持向量機:
我們能快速採樣到原數據集的40%作爲測試集,從而測試(評估)我們的分類器:
當評價估計器的不同設置(超參數)時,例如手動爲SVM設置的“C”參數, 由於在訓練集上,通過調整參數設置使估計器的性能達到了最佳狀態;但在測試集上可能會出現過擬合的情況。 此時,測試集上的信息反饋足以顛覆訓練好的模型,評估的指標不再有效反映出模型的泛化性能。 爲了解決此類問題,還應該準備另一部分被稱爲“驗證集”的數據集,模型訓練完成以後在驗證集上對模型進行評估。 當驗證集上的評估實驗比較成功時,在測試集上進行最後的評估。
然而,通過將原始數據分爲3個數據集合,我們就大大減少了可用於模型學習的樣本數量, 並且得到的結果依賴於集合對(訓練,驗證)的隨機選擇。
這個問題可以通過 交叉驗證(CV 縮寫) 來解決。 交叉驗證仍需要測試集做最後的模型評估,但不再需要驗證集。
最基本的方法被稱之爲,k-折交叉驗證。 k-折交叉驗證將訓練集劃分爲 k 個較小的集合(其他方法會在下面描述,主要原則基本相同)。 每一個*k*折都會遵循下面的過程:
- 將k-1份訓練集子集作爲訓練集訓練模型,
- 將剩餘的1份訓練集子集作爲驗證集用於模型驗證(也就是利用該數據集計算模型的性能指標,例如準確率)。
k-折交叉驗證得出的性能指標是循環計算中每個值的平均值。 該方法雖然計算代價很高,但是它不會浪費太多的數據(就像是固定了一個任意的測試集), 在處理樣本數據集較少的問題(例如,逆向推理)時比較有優勢。
3.1.1. 計算交叉驗證的指標
最簡單的方式 The simplest way to use cross-validation is to call the cross_val_score
helper
function on the estimator and the dataset.
The following example demonstrates how to estimate the accuracy of a linear kernel support vector machine on the iris dataset by splitting the data, fitting a model and computing the score 5 consecutive times (with different splits each time):
The mean score and the 95% confidence interval of the score estimate are hence given by:
By default, the score computed at each CV iteration is the score
method
of the estimator. It is possible to change this by using the scoring parameter:
See scoring 參數: 定義模型評估規則 for details. In the case of the Iris dataset, the samples are balanced across target classes hence the accuracy and the F1-score are almost equal.
When the cv
argument
is an integer, cross_val_score
uses
the KFold
or StratifiedKFold
strategies
by default, the latter being used if the estimator derives from ClassifierMixin
.
It is also possible to use other cross validation strategies by passing a cross validation iterator instead, for instance:
Data transformation with held out data
Just as it is important to test a predictor on data held-out from training, preprocessing (such as standardization, feature selection, etc.) and similar data transformations similarly should be learnt from a training set and applied to held-out data for prediction:
A Pipeline
makes
it easier to compose estimators, providing this behavior under cross-validation:
3.1.1.1. The cross_validate function and multiple metric evaluation
The cross_validate
function
differs from cross_val_score
in two ways -
- It allows specifying multiple metrics for evaluation.
- It returns a dict containing training scores, fit-times and score-times in addition to the test score.
For single metric evaluation, where the scoring parameter is a string, callable or None, the keys will be - ['test_score', 'fit_time', 'score_time']
And for multiple metric evaluation, the return value is a dict with the following keys -['test_<scorer1_name>', 'test_<scorer2_name>', 'test_<scorer...>', 'fit_time', 'score_time']
return_train_score
is
set to True
by default. It adds train score keys for all
the scorers. If train scores are not needed, this should be set to False
explicitly.
The multiple metrics can be specified either as a list, tuple or set of predefined scorer names:
Or as a dict mapping scorer name to a predefined or custom scoring function:
Here is an example of cross_validate
using
a single metric:
3.1.1.2. 通過交叉驗證獲取預測
The function cross_val_predict
has
a similar interface to cross_val_score
,
but returns, for each element in the input, the prediction that was obtained for that element when it was in the test set. Only cross-validation strategies that assign all elements to a test set exactly once can be used (otherwise, an exception is raised).
These prediction can then be used to evaluate the classifier:
Note that the result of this computation may be slightly different from those obtained using cross_val_score
as
the elements are grouped in different ways.
The available cross validation iterators are introduced in the following section.
3.1.2. 交叉驗證迭代器
The following sections list utilities to generate indices that can be used to generate dataset splits according to different cross validation strategies.
3.1.3. 交叉驗證迭代器–循環遍歷數據
Assuming that some data is Independent and Identically Distributed (i.i.d.) is making the assumption that all samples stem from the same generative process and that the generative process is assumed to have no memory of past generated samples.
The following cross-validators can be used in such cases.
NOTE
While i.i.d. data is a common assumption in machine learning theory, it rarely holds in practice. If one knows that the samples have been generated using a time-dependent process, it’s safer to use a time-series aware cross-validation scheme Similarly if we know that the generative process has a group structure (samples from collected from different subjects, experiments, measurement devices) it safer to use group-wise cross-validation.
3.1.3.1. K-fold
KFold
divides
all the samples in groups of samples, called folds (if ,
this is equivalent to the Leave One Outstrategy), of equal sizes (if possible). The prediction function is learned using folds,
and the fold left out is used for test.
Example of 2-fold cross-validation on a dataset with 4 samples:
Each fold is constituted by two arrays: the first one is related to the training set, and the second one to the test set. Thus, one can create the training/test sets using numpy indexing:
3.1.3.2. 重複 K-折交叉驗證
RepeatedKFold
repeats
K-Fold n times. It can be used when one requires to run KFold
n
times, producing different splits in each repetition.
Example of 2-fold K-Fold repeated 2 times:
Similarly, RepeatedStratifiedKFold
repeats
Stratified K-Fold n times with different randomization in each repetition.
3.1.3.3. 留一交叉驗證 (LOO)
LeaveOneOut
(or
LOO) is a simple cross-validation. Each learning set is created by taking all the samples except one, the test set being the sample left out. Thus, for samples,
we have different training sets and different
tests set. This cross-validation procedure does not waste much data as only one sample is removed from the training set:
Potential users of LOO for model selection should weigh a few known caveats. When compared with -fold cross validation, one builds models from samples instead of models, where . Moreover, each is trained on samples rather than . In both ways, assuming is not too large and , LOO is more computationally expensive than -fold cross validation.
In terms of accuracy, LOO often results in high variance as an estimator for the test error. Intuitively, since of the samples are used to build each model, models constructed from folds are virtually identical to each other and to the model built from the entire training set.
However, if the learning curve is steep for the training size in question, then 5- or 10- fold cross validation can overestimate the generalization error.
As a general rule, most authors, and empirical evidence, suggest that 5- or 10- fold cross validation should be preferred to LOO.
References:
- http://www.faqs.org/faqs/ai-faq/neural-nets/part3/section-12.html;
- T. Hastie, R. Tibshirani, J. Friedman, The Elements of Statistical Learning, Springer 2009
- L. Breiman, P. Spector Submodel selection and evaluation in regression: The X-random case, International Statistical Review 1992;
- R. Kohavi, A Study of Cross-Validation and Bootstrap for Accuracy Estimation and Model Selection, Intl. Jnt. Conf. AI
- R. Bharat Rao, G. Fung, R. Rosales, On the Dangers of Cross-Validation. An Experimental Evaluation, SIAM 2008;
- G. James, D. Witten, T. Hastie, R Tibshirani, An Introduction to Statistical Learning, Springer 2013.
3.1.3.4. Leave P Out (LPO)
LeavePOut
is
very similar to LeaveOneOut
as
it creates all the possible training/test sets by removing samples
from the complete set. For samples, this produces train-test
pairs. Unlike LeaveOneOut
and KFold
,
the test sets will overlap for .
Example of Leave-2-Out on a dataset with 4 samples:
3.1.3.5. Random permutations cross-validation a.k.a. Shuffle & Split
The ShuffleSplit
iterator
will generate a user defined number of independent train / test dataset splits. Samples are first shuffled and then split into a pair of train and test sets.
It is possible to control the randomness for reproducibility of the results by explicitly seeding the random_state
pseudo
random number generator.
Here is a usage example:
ShuffleSplit
is
thus a good alternative to KFold
cross
validation that allows a finer control on the number of iterations and the proportion of samples on each side of the train / test split.
3.1.4. 基於類標籤、具有分層的交叉驗證迭代器
Some classification problems can exhibit a large imbalance in the distribution of the target classes: for instance there could be several times more negative samples than positive samples.
In such cases it is recommended to use stratified sampling as implemented in StratifiedKFold
and StratifiedShuffleSplit
to
ensure that relative class frequencies is approximately preserved in each train and validation fold.
3.1.4.1. Stratified k-fold
StratifiedKFold
is
a variation of k-fold which returns stratified folds: each set contains approximately the same percentage of samples of each target class as the complete set.
Example of stratified 3-fold cross-validation on a dataset with 10 samples from two slightly unbalanced classes:
RepeatedStratifiedKFold
can
be used to repeat Stratified K-Fold n times with different randomization in each repetition.
3.1.4.2. Stratified Shuffle Split
StratifiedShuffleSplit
is
a variation of ShuffleSplit, which returns stratified splits, i.e which creates splits by preserving the same percentage for each target class as in the complete set.
3.1.5. 用於分組數據的交叉驗證迭代器
The i.i.d. assumption is broken if the underlying generative process yield groups of dependent samples.
Such a grouping of data is domain specific. An example would be when there is medical data collected from multiple patients, with multiple samples taken from each patient. And such data is likely to be dependent on the individual group. In our example, the patient id for each sample will be its group identifier.
In this case we would like to know if a model trained on a particular set of groups generalizes well to the unseen groups. To measure this, we need to ensure that all the samples in the validation fold come from groups that are not represented at all in the paired training fold.
The following cross-validation splitters can be used to do that. The grouping identifier for the samples is specified via the groups
parameter.
3.1.5.1. Group k-fold
GroupKFold
is
a variation of k-fold which ensures that the same group is not represented in both testing and training sets. For example if the data is obtained from different subjects with several samples per-subject and if the model is flexible enough to learn from highly
person specific features it could fail to generalize to new subjects. GroupKFold
makes
it possible to detect this kind of overfitting situations.
Imagine you have three subjects, each with an associated number from 1 to 3:
Each subject is in a different testing fold, and the same subject is never in both testing and training. Notice that the folds do not have exactly the same size due to the imbalance in the data.
3.1.5.2. Leave One Group Out
LeaveOneGroupOut
is
a cross-validation scheme which holds out the samples according to a third-party provided array of integer groups. This group information can be used to encode arbitrary domain specific pre-defined cross-validation folds.
Each training set is thus constituted by all the samples except the ones related to a specific group.
For example, in the cases of multiple experiments, LeaveOneGroupOut
can
be used to create a cross-validation based on the different experiments: we create a training set using the samples of all the experiments except one:
Another common application is to use time information: for instance the groups could be the year of collection of the samples and thus allow for cross-validation against time-based splits.
3.1.5.3. Leave P Groups Out
LeavePGroupsOut
is
similar as LeaveOneGroupOut
,
but removes samples related to groups for each training/test
set.
Example of Leave-2-Group Out:
3.1.5.4. Group Shuffle Split
The GroupShuffleSplit
iterator
behaves as a combination of ShuffleSplit
and LeavePGroupsOut
,
and generates a sequence of randomized partitions in which a subset of groups are held out for each split.
Here is a usage example:
This class is useful when the behavior of LeavePGroupsOut
is
desired, but the number of groups is large enough that generating all possible partitions with groups
withheld would be prohibitively expensive. In such a scenario, GroupShuffleSplit
provides
a random sample (with replacement) of the train / test splits generated by LeavePGroupsOut
.
3.1.6. 預定義的摺疊 / 驗證集
For some datasets, a pre-defined split of the data into training- and validation fold or into several cross-validation folds already exists. Using PredefinedSplit
it
is possible to use these folds e.g. when searching for hyperparameters.
For example, when using a validation set, set the test_fold
to
0 for all samples that are part of the validation set, and to -1 for all other samples.
3.1.7. 交叉驗證在時間序列數據中應用
Time series data is characterised by the correlation between observations that are near in time (autocorrelation). However, classical cross-validation techniques such as KFold
and ShuffleSplit
assume
the samples are independent and identically distributed, and would result in unreasonable correlation between training and testing instances (yielding poor estimates of generalisation error) on time series data. Therefore, it is very important to evaluate
our model for time series data on the “future” observations least like those that are used to train the model. To achieve this, one solution is provided by TimeSeriesSplit
.
3.1.7.1. Time Series Split
TimeSeriesSplit
is
a variation of k-fold which returns first folds
as train set and the th fold as test set. Note that
unlike standard cross-validation methods, successive training sets are supersets of those that come before them. Also, it adds all surplus data to the first training partition, which is always used to train the model.
This class can be used to cross-validate time series data samples that are observed at fixed time intervals.
Example of 3-split time series cross-validation on a dataset with 6 samples:
3.1.8. A note on shuffling
If the data ordering is not arbitrary (e.g. samples with the same class label are contiguous), shuffling it first may be essential to get a meaningful cross- validation result. However, the opposite may be true if the samples are not independently and identically distributed. For example, if samples correspond to news articles, and are ordered by their time of publication, then shuffling the data will likely lead to a model that is overfit and an inflated validation score: it will be tested on samples that are artificially similar (close in time) to training samples.
Some cross validation iterators, such as KFold
,
have an inbuilt option to shuffle the data indices before splitting them. Note that:
- This consumes less memory than shuffling the data directly.
- By default no shuffling occurs, including for the (stratified) K fold cross- validation performed by specifying
cv=some_integer
tocross_val_score
, grid search, etc. Keep in mind thattrain_test_split
still returns a random split. - The
random_state
parameter defaults toNone
, meaning that the shuffling will be different every timeKFold(..., shuffle=True)
is iterated. However,GridSearchCV
will use the same shuffling for each set of parameters validated by a single call to itsfit
method. - To get identical results for each split, set
random_state
to an integer.
3.1.9. 交叉驗證和模型選擇
Cross validation iterators can also be used to directly perform model selection using Grid Search for the optimal hyperparameters of the model. This is the topic of the next section: 調整估計器的超參數.
中文文檔: http://sklearn.apachecn.org/cn/stable/modules/cross_validation.html
英文文檔: http://sklearn.apachecn.org/en/stable/modules/cross_validation.html
官方文檔: http://scikit-learn.org/stable/
GitHub: https://github.com/apachecn/scikit-learn-doc-zh(覺得不錯麻煩給個 Star,我們一直在努力)
貢獻者: https://github.com/apachecn/scikit-learn-doc-zh#貢獻者
關於我們: http://www.apachecn.org/organization/209.html
有興趣的們也可以和我們一起來維護,持續更新中 。。。
機器學習交流羣: 629470233