
    'YHh.                         d Z ddlmZ ddlZddlmZ  G d d      Z G d d      Z G d	 d
      Z	 G d d      Z
d Z	  G d d      Zy)a!  
Utilities for cross validation.

taken from scikits.learn

# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>,
#         Gael Varoquaux    <gael.varoquaux@normalesup.org>
# License: BSD Style.
# $Id$

changes to code by josef-pktd:
 - docstring formatting: underlines of headers

    )lrangeN)combinationsc                   "    e Zd ZdZd Zd Zd Zy)LeaveOneOutzs
    Leave-One-Out cross validation iterator:
    Provides train/test indexes to split data in train test sets
    c                     || _         y)a9  
        Leave-One-Out cross validation iterator:
        Provides train/test indexes to split data in train test sets

        Parameters
        ----------
        n: int
            Total number of elements

        Examples
        --------
        >>> from scikits.learn import cross_val
        >>> X = [[1, 2], [3, 4]]
        >>> y = [1, 2]
        >>> loo = cross_val.LeaveOneOut(2)
        >>> for train_index, test_index in loo:
        ...    print "TRAIN:", train_index, "TEST:", test_index
        ...    X_train, X_test, y_train, y_test = cross_val.split(train_index, test_index, X, y)
        ...    print X_train, X_test, y_train, y_test
        TRAIN: [False  True] TEST: [ True False]
        [[3 4]] [[1 2]] [2] [1]
        TRAIN: [ True False] TEST: [False  True]
        [[1 2]] [[3 4]] [1] [2]
        N)n)selfr   s     \/var/www/html/planif/env/lib/python3.12/site-packages/statsmodels/sandbox/tools/cross_val.py__init__zLeaveOneOut.__init__   s    2     c              #      K   | j                   }t        |      D ]=  }t        j                  |t              }d||<   t        j
                  |      }||f ? y wNdtypeT)r   rangenpzerosboollogical_not)r	   r   i
test_indextrain_indexs        r
   __iter__zLeaveOneOut.__iter__8   sT     FFq 	*A((1D1J JqM..4Kz))		*s   AAc                 v    d| j                   j                  | j                   j                  | j                  fz  S Nz%s.%s(n=%i)	__class__
__module____name__r   r	   s    r
   __repr__zLeaveOneOut.__repr__A   4     9 9 $ 7 7 $ " " 	"r   Nr   r   __qualname____doc__r   r   r!    r   r
   r   r      s    
8*"r   r   c                   "    e Zd ZdZd Zd Zd Zy)	LeavePOutzq
    Leave-P-Out cross validation iterator:
    Provides train/test indexes to split data in train test sets
    c                      || _         || _        y)aV  
        Leave-P-Out cross validation iterator:
        Provides train/test indexes to split data in train test sets

        Parameters
        ----------
        n: int
            Total number of elements
        p: int
            Size test sets

        Examples
        --------
        >>> from scikits.learn import cross_val
        >>> X = [[1, 2], [3, 4], [5, 6], [7, 8]]
        >>> y = [1, 2, 3, 4]
        >>> lpo = cross_val.LeavePOut(4, 2)
        >>> for train_index, test_index in lpo:
        ...    print "TRAIN:", train_index, "TEST:", test_index
        ...    X_train, X_test, y_train, y_test = cross_val.split(train_index, test_index, X, y)
        TRAIN: [False False  True  True] TEST: [ True  True False False]
        TRAIN: [False  True False  True] TEST: [ True False  True False]
        TRAIN: [False  True  True False] TEST: [ True False False  True]
        TRAIN: [ True False False  True] TEST: [False  True  True False]
        TRAIN: [ True False  True False] TEST: [False  True False  True]
        TRAIN: [ True  True False False] TEST: [False False  True  True]
        N)r   p)r	   r   r*   s      r
   r   zLeavePOut.__init__P   s    8 r   c              #     K   | j                   }| j                  }t        t        |      |      }|D ]P  }t	        j
                  |t              }d|t	        j                  |      <   t	        j                  |      }||f R y wr   )	r   r*   r   r   r   r   r   arrayr   )r	   r   r*   combidxr   r   s          r
   r   zLeavePOut.__iter__p   sr     FFFFF1Iq) 	*C!40J(,Jrxx}%..4Kz))		*   BBc                     d| j                   j                  | j                   j                  | j                  | j                  fz  S )Nz%s.%s(n=%i, p=%i))r   r   r   r   r*   r    s    r
   r!   zLeavePOut.__repr__{   <    " $ 9 9 $ 7 7 $ $	&" " 	"r   Nr#   r&   r   r
   r(   r(   J   s    
@*"r   r(   c                   "    e Zd ZdZd Zd Zd Zy)KFoldzm
    K-Folds cross validation iterator:
    Provides train/test indexes to split data in train test sets
    c                 ~    |dkD  sJ t        d             ||k  sJ t        d||fz               || _        || _        y)a  
        K-Folds cross validation iterator:
        Provides train/test indexes to split data in train test sets

        Parameters
        ----------
        n: int
            Total number of elements
        k: int
            number of folds

        Examples
        --------
        >>> from scikits.learn import cross_val
        >>> X = [[1, 2], [3, 4], [1, 2], [3, 4]]
        >>> y = [1, 2, 3, 4]
        >>> kf = cross_val.KFold(4, k=2)
        >>> for train_index, test_index in kf:
        ...    print "TRAIN:", train_index, "TEST:", test_index
        ...    X_train, X_test, y_train, y_test = cross_val.split(train_index, test_index, X, y)
        TRAIN: [False False  True  True] TEST: [ True  True False False]
        TRAIN: [ True  True False False] TEST: [False False  True  True]

        Notes
        -----
        All the folds have size trunc(n/k), the last one has the complementary
        r   zcannot have k below 1z cannot have k=%d greater than %dN)
ValueErrorr   k)r	   r   r6   s      r
   r   zKFold.__init__   sI    8 s7J677ssJJAAq6IJJsr   c              #   H  K   | j                   }| j                  }t        t        j                  ||z              }t        |      D ]W  }t        j                  |t              }||dz
  k  rd|||z  |dz   |z   nd|||z  d  t        j                  |      }||f Y y w)Nr      T)	r   r6   intr   ceilr   r   r   r   )r	   r   r6   jr   r   r   s          r
   r   zKFold.__iter__   s     FFFF!q 	*A((1D1J1u*.
1Q3!Qw'#'
1Q34 ..4Kz))	*s   B B"c                     d| j                   j                  | j                   j                  | j                  | j                  fz  S )Nz%s.%s(n=%i, k=%i))r   r   r   r   r6   r    s    r
   r!   zKFold.__repr__   r1   r   Nr#   r&   r   r
   r3   r3      s    
D*"r   r3   c                   "    e Zd ZdZd Zd Zd Zy)LeaveOneLabelOutzy
    Leave-One-Label_Out cross-validation iterator:
    Provides train/test indexes to split data in train test sets
    c                     || _         y)a  
        Leave-One-Label_Out cross validation:
        Provides train/test indexes to split data in train test sets

        Parameters
        ----------
        labels : list
                List of labels

        Examples
        --------
        >>> from scikits.learn import cross_val
        >>> X = [[1, 2], [3, 4], [5, 6], [7, 8]]
        >>> y = [1, 2, 1, 2]
        >>> labels = [1, 1, 2, 2]
        >>> lol = cross_val.LeaveOneLabelOut(labels)
        >>> for train_index, test_index in lol:
        ...    print "TRAIN:", train_index, "TEST:", test_index
        ...    X_train, X_test, y_train, y_test = cross_val.split(train_index,             test_index, X, y)
        ...    print X_train, X_test, y_train, y_test
        TRAIN: [False False  True  True] TEST: [ True  True False False]
        [[5 6]
        [7 8]] [[1 2]
        [3 4]] [1 2] [1 2]
        TRAIN: [ True  True False False] TEST: [False False  True  True]
        [[1 2]
        [3 4]] [[5 6]
        [7 8]] [1 2] [1 2]
        N)labels)r	   r@   s     r
   r   zLeaveOneLabelOut.__init__   s    > r   c              #     K   t        j                  | j                  d      }t        j                  |      D ]I  }t        j                  t        |      t              }d|||k(  <   t        j                  |      }||f K y w)NT)copyr   )r   r,   r@   uniquer   lenr   r   )r	   r@   r   r   r   s        r
   r   zLeaveOneLabelOut.__iter__   sm     $++D16" 	*A((3v;d;J$(Jvqy!..4Kz))		*r/   c                     dj                  | j                  j                  | j                  j                  | j                        S )Nz{}.{}(labels={}))formatr   r   r   r@   r    s    r
   r!   zLeaveOneLabelOut.__repr__   s6    !(( $ 9 9 $ 7 7 $" 	"r   Nr#   r&   r   r
   r>   r>      s    
D*"r   r>   c                     g }|D ]C  }t        j                  |      }||    }||   }|j                  |       |j                  |       E |S )zx
    For each arg return a train and test subsets defined by indexes provided
    in train_indexes and test_indexes
    )r   
asanyarrayappend)train_indexestest_indexesargsretarg	arg_trainarg_tests          r
   splitrQ      sY    
 C mmC &	%

9

8 Jr   c                   $    e Zd ZdZddZd Zd Zy)
KStepAheadzn
    KStepAhead cross validation iterator:
    Provides fit/test indexes to split data in sequential sets
    Nc                     || _         || _        |!t        t        j                  |dz              }|| _        || _        || _        y)a=  
        KStepAhead cross validation iterator:
        Provides train/test indexes to split data in train test sets

        Parameters
        ----------
        n: int
            Total number of elements
        k : int
            number of steps ahead
        start : int
            initial size of data for fitting
        kall : bool
            if true. all values for up to k-step ahead are included in the test index.
            If false, then only the k-th step ahead value is returnd


        Notes
        -----
        I do not think this is really useful, because it can be done with
        a very simple loop instead.
        Useful as a plugin, but it could return slices instead for faster array access.

        Examples
        --------
        >>> from scikits.learn import cross_val
        >>> X = [[1, 2], [3, 4]]
        >>> y = [1, 2]
        >>> loo = cross_val.LeaveOneOut(2)
        >>> for train_index, test_index in loo:
        ...    print "TRAIN:", train_index, "TEST:", test_index
        ...    X_train, X_test, y_train, y_test = cross_val.split(train_index, test_index, X, y)
        ...    print X_train, X_test, y_train, y_test
        TRAIN: [False  True] TEST: [ True False]
        [[3 4]] [[1 2]] [2] [1]
        TRAIN: [ True False] TEST: [False  True]
        [[1 2]] [[3 4]] [1] [2]
        Ng      ?)r   r6   r9   r   truncstartkallreturn_slice)r	   r   r6   rV   rW   rX   s         r
   r   zKStepAhead.__init__   sE    N =4()E
	(r   c              #     K   | j                   }| j                  }| j                  }| j                  rYt	        |||z
        D ]F  }t        d |d       }| j                  rt        |||z         }nt        ||z   dz
  ||z         }||f H y t	        |||z
        D ]f  }t        j                  |t              }d|d | t        j                  |t              }| j                  r	d||||z    nd|||z   dz
  ||z    ||f h y w)Nr8   r   T)
r   r6   rV   rX   r   slicerW   r   r   r   )	r	   r   r6   rV   r   train_slice
test_slicer   r   s	            r
   r   zKStepAhead.__iter__P  s    FFFF

5!A#& .#D!T299!&q!A#J!&qs1uac!2J!:--. 5!A#& 
.!xx6"&BQ hhq5
99(,Jq1%,0Jqs1uQqS) ":--
.s   DDc                 v    d| j                   j                  | j                   j                  | j                  fz  S r   r   r    s    r
   r!   zKStepAhead.__repr__k  r"   r   )r8   NTTr#   r&   r   r
   rS   rS     s    
-)`.6"r   rS   )r%   statsmodels.compat.pythonr   numpyr   	itertoolsr   r   r(   r3   r>   rQ   rS   r&   r   r
   <module>ra      sW    -  "/" /"h7" 7"v=" ="B7" 7"tU" U"r   