3.1. Cross-Validation - Evaluating Estimator Performance - Scikit-Learn 1.3.0 Documentation
3.1. Cross-Validation - Evaluating Estimator Performance - Scikit-Learn 1.3.0 Documentation
3.1. Cross-Validation - Evaluating Estimator Performance - Scikit-Learn 1.3.0 Documentation
In scikit-learn a random split into training and test sets can be quickly computed with the train_test_split helper function. Let’s load the iris
data set to fit a linear support vector machine on it:
>>> X, y = datasets.load_iris(return_X_y=True)
>>> X.shape, y.shape
((150, 4), (150,))
We can now quickly sample a training set while holding out 40% of the data for testing (evaluating) our classifier:
When evaluating different settings (“hyperparameters”) for estimators, such as the C setting that must be manually set for an SVM, there is still
a risk of overfitting on the test set because the parameters can be tweaked until the estimator performs optimally. This way, knowledge about
the test set can “leak” into the model and evaluation metrics no longer report on generalization performance. To solve this problem, yet another
part of the dataset can be held out as a so-called “validation set”: training proceeds on the training set, after which evaluation is done on the
validation set, and when the experiment seems to be successful, final evaluation can be done on the test set.
However, by partitioning the available data into three sets, we drastically reduce the number of samples which can be used for learning the
model, and the results can depend on a particular random choice for the pair of (train, validation) sets.
A solution to this problem is a procedure called cross-validation (CV for short). A test set should still be held out for final evaluation, but the val‐
idation set is no longer needed when doing CV. In the basic approach, called k-fold CV, the training set is split into k smaller sets (other ap‐
proaches are described below, but generally follow the same principles). The following procedure is followed for each of the k “folds”:
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 standard deviation 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 The scoring parameter: defining model evaluation rules 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 esti‐
mator derives from ClassifierMixin.
It is also possible to use other cross validation strategies by passing a cross validation iterator instead, for instance:
Another option is to use an iterable yielding (train, test) splits as arrays of indices, for example:
Toggle Menu
>>> def custom_cv_2folds(X):
... n = X.shape[0]
... i = 1
... while i <= 2:
... idx = np.arange(n * (i - 1) / 2, n * i / 2, dtype=int)
... yield idx, idx
... i += 1
...
>>> custom_cv = custom_cv_2folds(X)
>>> cross_val_score(clf, X, y, cv=custom_cv)
array([1. , 0.973...])
A Pipeline makes it easier to compose estimators, providing this behavior under cross-validation:
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 False by default to save computation time. To evaluate the scores on the training set as well you need to set it to
True . You may also retain the estimator fitted on each training set by setting return_estimator=True . Similarly, you may set
return_indices=True to retain the training and testing indices used to split the dataset into train and test sets for each cv split.
The multiple metrics can be specified either as a list, tuple or set of predefined scorer names:
Toggle Menu
>>> from sklearn.metrics import make_scorer
>>> scoring = {'prec_macro': 'precision_macro',
... 'rec_macro': make_scorer(recall_score, average='macro')}
>>> scores = cross_validate(clf, X, y, scoring=scoring,
... cv=5, return_train_score=True)
>>> sorted(scores.keys())
['fit_time', 'score_time', 'test_prec_macro', 'test_rec_macro',
'train_prec_macro', 'train_rec_macro']
>>> scores['train_rec_macro']
array([0.97..., 0.97..., 0.99..., 0.98..., 0.98...])
The available cross validation iterators are introduced in the following section.
Examples
Receiver Operating Characteristic (ROC) with cross validation,
Recursive feature elimination with cross-validation,
Custom refit strategy of a grid search with cross-validation,
Sample pipeline for text feature extraction and evaluation,
Plotting Cross-Validated Predictions,
Nested versus non-nested cross-validation.
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 is safer to use a time-series aware cross-validation scheme. Similarly, if we know that the
generative process has a group structure (samples collected from different subjects, experiments, measurement devices), it is safer to use
group-wise cross-validation.
K-fold
KFold divides all the samples in groups of samples, called folds (if k = n, this is equivalent to the Leave One Out strategy), of equal sizes (if
k
possible). The prediction function is learned using k − 1 folds, and the fold left out is used for test.
Toggle Menu
Example of 2-fold cross-validation on a dataset with 4 samples:
Here is a visualization of the cross-validation behavior. Note that KFold is not affected by classes or groups.
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:
Repeated K-Fold
RepeatedKFold repeats K-Fold n times. It can be used when one requires to run KFold n times, producing different splits in each repetition.
Similarly, RepeatedStratifiedKFold repeats Stratified K-Fold n times with different randomization in each repetition.
>>> X = [1, 2, 3, 4]
>>> loo = LeaveOneOut()
>>> for train, test in loo.split(X):
... print("%s %s" % (train, test))
[1 2 3] [0]
[0 2 3] [1]
[0 1 3] [2]
[0 1 2] [3]
Potential users of LOO for model selection should weigh a few known caveats. When compared with k-fold cross validation, one builds n mod‐
els from n samples instead of k models, where n > k . Moreover, each is trained on n − 1 samples rather than (k − 1)n/k. In both ways, as‐
suming k is not too large and k < n , LOO is more computationally expensive than k-fold cross validation.
Toggle Menu
In terms of accuracy, LOO often results in high variance as an estimator for the test error. Intuitively, since n − 1 of the n 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.
Leave P Out (LPO)
LeavePOut is very similar to LeaveOneOut as it creates all the possible training/test sets by removing p samples from the complete set. For n sam‐
ples, this produces ( n
p
) train-test pairs. Unlike LeaveOneOut and KFold, the test sets will overlap for p > 1 .
>>> X = np.ones(4)
>>> lpo = LeavePOut(p=2)
>>> for train, test in lpo.split(X):
... print("%s %s" % (train, test))
[2 3] [0 1]
[1 3] [0 2]
[1 2] [0 3]
[0 3] [1 2]
[0 2] [1 3]
[0 1] [2 3]
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 visualization of the cross-validation behavior. Note that ShuffleSplit is not affected by classes or groups.
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
Toggle on each side of the train / test split.
Menu
3.1.2.2. Cross-validation iterators with stratification based on class labels.
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.
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.
Here is an example of stratified 3-fold cross-validation on a dataset with 50 samples from two unbalanced classes. We show the number of
samples in each class and compare with KFold.
We can see that StratifiedKFold preserves the class ratios (approximately 1 / 10) in both train and test dataset.
RepeatedStratifiedKFold can be used to repeat Stratified K-Fold n times with different randomization in each repetition.
Toggle Menu
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 sam‐
ple 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.
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:
>>> X = [0.1, 0.2, 2.2, 2.4, 2.3, 4.55, 5.8, 8.8, 9, 10]
>>> y = ["a", "b", "b", "b", "c", "c", "c", "d", "d", "d"]
>>> groups = [1, 1, 1, 2, 2, 2, 3, 3, 3, 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. If class proportions must be balanced across folds, StratifiedGroupKFold is a better option.
Similar to KFold, the test sets from GroupKFold will form a complete partition of all the data. Unlike KFold, GroupKFold is not randomized at all,
whereas KFold is randomized when shuffle=True .
StratifiedGroupKFold
StratifiedGroupKFold is a cross-validation scheme that combines both StratifiedKFold and GroupKFold. The idea is to try to preserve the distri‐
bution of classes in each split while keeping each group within a single split. That might be useful when you have an unbalanced dataset so that
using just GroupKFold might produce skewed splits.
Example:
Implementation notes:
With the current implementation full shuffle is not possible in most scenarios. When shuffle=True, the following happens:
Toggle 1.
Menu
All groups are shuffled.
2. Groups are sorted by standard deviation of classes using stable sort.
3. Sorted groups are iterated over and assigned to folds.
That means that only groups with the same standard deviation of class distribution will be shuffled, which might be useful when each
group has only a single class.
The algorithm greedily assigns each group to one of n_splits test sets, choosing the test set that minimises the variance in class distribu‐
tion across test sets. Group assignment proceeds from groups with highest to lowest variance in class frequency, i.e. large groups peaked
on one or few classes are assigned first.
This split is suboptimal in a sense that it might produce imbalanced splits even if perfect stratification is possible. If you have relatively
close distribution of classes in each group, using GroupKFold is better.
Each training set is thus constituted by all the samples except the ones related to a specific group. This is the same as LeavePGroupsOut with
n_groups=1 and the same as GroupKFold with n_splits equal to the number of unique labels passed to the groups parameter.
For example, in the cases of multiple experiments, LeaveOneGroupOut can be used to create a cross-validation based on the different experi‐
ments: 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.
>>> X = np.arange(6)
>>> y = [1, 1, 1, 2, 2, 2]
>>> groups = [1, 1, 2, 2, 3, 3]
>>> lpgo = LeavePGroupsOut(n_groups=2)
>>> for train, test in lpgo.split(X, y, groups=groups):
... print("%s %s" % (train, test))
[4 5] [0 1 2 3]
[2 3] [0 1 4 5]
[0 1] [2 3 4 5]
Toggle Menu
The GroupShuffleSplit iterator behaves as a combination of ShuffleSplit and LeavePGroupsOut, and generates a sequence of randomized parti‐
tions in which a subset of groups are held out for each split. Each train/test split is performed independently meaning there is no guaranteed
relationship between successive test sets.
This class is useful when the behavior of LeavePGroupsOut is desired, but the number of groups is large enough that generating all possible par‐
titions with P groups withheld would be prohibitively expensive. In such a scenario, GroupShuffleSplit provides a random sample (with re‐
placement) of the train / test splits generated by LeavePGroupsOut.
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.
To perform the train and test split, use the indices for the train and test subsets yielded by the generator output by the split() method of the
cross-validation splitter. For example:
Toggle Menu
3.1.2.6. Cross validation of time series data
Time series data is characterized by the correlation between observations that are near in time (autocorrelation). However, classical cross-valida‐
tion techniques such as KFold and ShuffleSplit assume the samples are independent and identically distributed, and would result in unreason‐
able correlation between training and testing instances (yielding poor estimates of generalization 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.
This class can be used to cross-validate time series data samples that are observed at fixed time intervals.
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([1, 2, 3, 4, 5, 6])
>>> tscv = TimeSeriesSplit(n_splits=3)
>>> print(tscv)
TimeSeriesSplit(gap=0, max_train_size=None, n_splits=3, test_size=None)
>>> for train, test in tscv.split(X):
... print("%s %s" % (train, test))
[0 1 2] [3]
[0 1 2 3] [4]
[0 1 2 3 4] [5]
Some cross validation iterators, such as KFold, have an inbuilt option to shuffle the data indices before splitting them. Note that:
For more details on how to control the randomness of cv splitters and avoid common pitfalls, see Controlling randomness.
A low p-value provides evidence that the dataset contains real dependency between features and labels and the classifier was able to utilize this
to obtain good results. A high p-value could be due to a lack of dependency between features and labels (there is no difference in feature val‐
ues between the classes) or because the classifier was not able to use the dependency in the data. In the latter case, using a more appropriate
classifier that is able to utilize the structure in the data, would result in a lower p-value.
Cross-validation provides information about how well a classifier generalizes, specifically the range of expected errors of the classifier. However,
a classifier trained on a high dimensional dataset with no structure may still perform better than expected on cross-validation, just by chance.
This can typically happen with small datasets with less than a few hundred samples. permutation_test_score provides information on whether
the classifier has found a real class structure and can help in evaluating the performance of the classifier.
It is important to note that this test has been shown to produce low p-values even if there is only weak structure in the data because in the cor‐
responding permutated datasets there is absolutely no structure. This test is therefore only able to show when the model reliably outperforms
random guessing.
Finally, permutation_test_score is computed using brute force and internally fits (n_permutations + 1) * n_cv models. It is therefore only
tractable with small datasets for which fitting an individual model is very fast.
Examples
References:
Ojala and Garriga. Permutation Tests for Studying Classifier Performance. J. Mach. Learn. Res. 2010.
© 2007 - 2023, scikit-learn developers (BSD License). Show this page source
Toggle Menu