<commit_before>=======================
Cross-Validation
=======================

.. currentmodule:: scikits.learn.cross_val

Learning the parameters of a prediction function and testing it on the same
data yields a methodological bias. To avoid over-fitting, we have to define two
different sets : a *learning set* :math:`X^l, y^l` which is used for learning
the prediction function (also called *training set*), and a *test set*
:math:`X^t, y^t` which is used for testing the prediction function.
However, by defining these two sets, we drastically reduce the number of samples
which can be used for learning the model, and the results can depend on a
particular couple of *learning set* and *test set*.

A solution is to split the whole data in different learning set and test set,
and to return the the averaged value of the prediction scores obtained with
the different sets. Such a procedure is called *cross-validation*. This approach
can  be computationally expensive, but does not waste too much data (as it is the
case when fixing an arbitrary test set), which is a major advantage in problem
such as inverse inference where the number of samples is very small.





Examples
--------

:ref:`example_plot_roc_crossval.py`,
:ref:`example_grid_search_digits.py`,
:ref:`example_rfe_with_cross_validation.py`,







Leave-One-Out - LOO
===================

:class:`LeaveOneOut`
The *Leave-One-Out* (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 *n* samples, we have *n* different learning sets and
*n* different tests set. This cross-validation procedure does not waste much
data as only one sample is removed from the learning set. 



    >>> import numpy as np
    >>> from scikits.learn.cross_val import LeaveOneOut
    >>> X = np.array([[0., 0.], [1., 1.], [-1., -1.], [2., 2.]])
    >>> Y = np.array([0, 1, 0, 1])
    >>> loo = LeaveOneOut(len(Y))
    >>> print loo
    scikits.learn.cross_val.LeaveOneOut(n=4)
    >>> for train, test in loo: print train,test
    [False  True  True  True] [ True False False False]
    [ True False  True  True] [False  True False False]
    [ True  True False  True] [False False  True False]
    [ True  True  True False] [False False False  True]


Each fold is constitued 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:

    >>> X_train, X_test, y_train, y_test = X[train], X[test], Y[train], Y[test]







Leave-P-Out - LPO
=================


:class:`LeavePOut`
*Leave-P-Out* is very similar to *Leave-One-Out*, as it creates all the
possible training/test sets by removing *P* samples from the complete set.


Example of Leave-2-Out:

>>> from scikits.learn.cross_val import LeavePOut
>>> X = [[0., 0.], [1., 1.], [-1., -1.], [2., 2.]]
>>> Y = [0, 1, 0, 1]
>>> loo = LeavePOut(len(Y), 2)
>>> print loo
scikits.learn.cross_val.LeavePOut(n=4, p=2)
>>> for train, test in loo: print train,test
[False False  True  True] [ True  True False False]
[False  True False  True] [ True False  True False]
[False  True  True False] [ True False False  True]
[ True False False  True] [False  True  True False]
[ True False  True False] [False  True False  True]
[ True  True False False] [False False  True  True]

All the possible folds are created, and again, one can create the training/test<commit_msg>DOC: update RST doc for crossval with indices<commit_after>=======================
Cross-Validation
=======================

.. currentmodule:: scikits.learn.cross_val

Learning the parameters of a prediction function and testing it on the same
data yields a methodological bias. To avoid over-fitting, we have to define two
different sets : a *learning set* :math:`X^l, y^l` which is used for learning
the prediction function (also called *training set*), and a *test set*
:math:`X^t, y^t` which is used for testing the prediction function.
However, by defining these two sets, we drastically reduce the number of samples
which can be used for learning the model, and the results can depend on a
particular couple of *learning set* and *test set*.

A solution is to split the whole data in different learning set and test set,
and to return the the averaged value of the prediction scores obtained with
the different sets. Such a procedure is called *cross-validation*. This approach
can  be computationally expensive, but does not waste too much data (as it is the
case when fixing an arbitrary test set), which is a major advantage in problem
such as inverse inference where the number of samples is very small.





Examples
--------

:ref:`example_plot_roc_crossval.py`,
:ref:`example_grid_search_digits.py`,
:ref:`example_rfe_with_cross_validation.py`,







Leave-One-Out - LOO
===================

:class:`LeaveOneOut`
The *Leave-One-Out* (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 *n* samples, we have *n* different learning sets and
*n* different tests set. This cross-validation procedure does not waste much
data as only one sample is removed from the learning set. 



    >>> import numpy as np
    >>> from scikits.learn.cross_val import LeaveOneOut
    >>> X = np.array([[0., 0.], [1., 1.], [-1., -1.], [2., 2.]])
    >>> Y = np.array([0, 1, 0, 1])
    >>> loo = LeaveOneOut(len(Y))
    >>> print loo
    scikits.learn.cross_val.LeaveOneOut(n=4)
    >>> for train, test in loo: print train, test
    [False  True  True  True] [ True False False False]
    [ True False  True  True] [False  True False False]
    [ True  True False  True] [False False  True False]
    [ True  True  True False] [False False False  True]


Each fold is constitued 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:

    >>> X_train, X_test, y_train, y_test = X[train], X[test], Y[train], Y[test]

If X or Y are `scipy.sparse` matrices, train and test need to be
integer indices. It can be obtained by setting the parameter indices to True
when creating the cross-validation procedure.

    >>> import numpy as np
    >>> from scikits.learn.cross_val import LeaveOneOut
    >>> X = np.array([[0., 0.], [1., 1.], [-1., -1.], [2., 2.]])
    >>> Y = np.array([0, 1, 0, 1])
    >>> loo = LeaveOneOut(len(Y), indices=True)
    >>> print loo
    scikits.learn.cross_val.LeaveOneOut(n=4)
    >>> for train, test in loo: print train, test
    [1 2 3] [0]
    [0 2 3] [1]
    [0 1 3] [2]
    [0 1 2] [3]


Leave-P-Out - LPO
=================


:class:`LeavePOut`
*Leave-P-Out* is very similar to *Leave-One-Out*, as it creates all the
possible training/test sets by removing *P* samples from the complete set.


Example of Leave-2-Out:

>>> from scikits.learn.cross_val import LeavePOut
>>> X = [[0., 0.], [1., 1.], [-1., -1.], [2., 2.]]
>>> Y = [0, 1, 0, 1]
>>> loo = LeavePOut(len(Y), 2)
>>> print loo
scikits.learn.cross_val.LeavePOut(n=4, p=2)
>>> for train, test in loo: print train,test
[False False  True  True] [ True  True False False]
[False  True False  True] [ True False  True False]
[False  True  True False] [ True False False  True]
[ True False False  True] [False  True  True False]
[ True False  True False] [False  True False  True]
[ True  True False False] [False False  True  True]

All the possible folds are created, and again, one can create the training/test
{'fraction_non_alphanumeric': 0.12887417218543046, 'fraction_numerical': 0.01152317880794702, 'mean_word_length': 3.7371392722710164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 108, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '949812', 'n_tokens_mistral': 2393, 'n_tokens_neox': 2269, 'n_words': 1038}