<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from __future__ import division <EOL> import warnings <EOL> import inspect <EOL> from itertools import chain , combinations <EOL> from collections import Iterable <EOL> from math import ceil , floor <EOL> import numbers <EOL> from abc import ABCMeta , abstractmethod <EOL> import numpy as np <EOL> from scipy . misc import comb <EOL> from . . utils import indexable , check_random_state , safe_indexing <EOL> from . . utils . validation import _num_samples , column_or_1d <EOL> from . . utils . multiclass import type_of_target <EOL> from . . externals . six import with_metaclass <EOL> from . . externals . six . moves import zip <EOL> from . . utils . fixes import bincount <EOL> from . . utils . fixes import signature <EOL> from . . base import _pprint <EOL> from . . gaussian_process . kernels import Kernel as GPKernel <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> class BaseCrossValidator ( with_metaclass ( ABCMeta ) ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> pass <EOL> def split ( self , X , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> X , y , labels = indexable ( X , y , labels ) <EOL> indices = np . arange ( _num_samples ( X ) ) <EOL> for test_index in self . _iter_test_masks ( X , y , labels ) : <EOL> train_index = indices [ np . logical_not ( test_index ) ] <EOL> test_index = indices [ test_index ] <EOL> yield train_index , test_index <EOL> def _iter_test_masks ( self , X = None , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> for test_index in self . _iter_test_indices ( X , y , labels ) : <EOL> test_mask = np . zeros ( _num_samples ( X ) , dtype = np . bool ) <EOL> test_mask [ test_index ] = True <EOL> yield test_mask <EOL> def _iter_test_indices ( self , X = None , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ abstractmethod <EOL> def get_n_splits ( self , X = None , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> def __repr__ ( self ) : <EOL> return _build_repr ( self ) <EOL> class LeaveOneOut ( BaseCrossValidator ) : <EOL> """<STR_LIT>""" <EOL> def _iter_test_indices ( self , X , y = None , labels = None ) : <EOL> return range ( _num_samples ( X ) ) <EOL> def get_n_splits ( self , X , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> if X is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return _num_samples ( X ) <EOL> class LeavePOut ( BaseCrossValidator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , p ) : <EOL> self . p = p <EOL> def _iter_test_indices ( self , X , y = None , labels = None ) : <EOL> for combination in combinations ( range ( _num_samples ( X ) ) , self . p ) : <EOL> yield np . array ( combination ) <EOL> def get_n_splits ( self , X , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> if X is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return int ( comb ( _num_samples ( X ) , self . p , exact = True ) ) <EOL> class _BaseKFold ( with_metaclass ( ABCMeta , BaseCrossValidator ) ) : <EOL> """<STR_LIT>""" <EOL> @ abstractmethod <EOL> def __init__ ( self , n_folds , shuffle , random_state ) : <EOL> if not isinstance ( n_folds , numbers . Integral ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( n_folds , type ( n_folds ) ) ) <EOL> n_folds = int ( n_folds ) <EOL> if n_folds <= <NUM_LIT:1> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( n_folds ) ) <EOL> if not isinstance ( shuffle , bool ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( shuffle ) ) <EOL> self . n_folds = n_folds <EOL> self . shuffle = shuffle <EOL> self . random_state = random_state <EOL> def split ( self , X , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> X , y , labels = indexable ( X , y , labels ) <EOL> n_samples = _num_samples ( X ) <EOL> if self . n_folds > n_samples : <EOL> raise ValueError ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( self . n_folds , <EOL> n_samples ) ) <EOL> for train , test in super ( _BaseKFold , self ) . split ( X , y , labels ) : <EOL> yield train , test <EOL> def get_n_splits ( self , X = None , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> return self . n_folds <EOL> class KFold ( _BaseKFold ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n_folds = <NUM_LIT:3> , shuffle = False , <EOL> random_state = None ) : <EOL> super ( KFold , self ) . __init__ ( n_folds , shuffle , random_state ) <EOL> def _iter_test_indices ( self , X , y = None , labels = None ) : <EOL> n_samples = _num_samples ( X ) <EOL> indices = np . arange ( n_samples ) <EOL> if self . shuffle : <EOL> check_random_state ( self . random_state ) . shuffle ( indices ) <EOL> n_folds = self . n_folds <EOL> fold_sizes = ( n_samples // n_folds ) * np . ones ( n_folds , dtype = np . int ) <EOL> fold_sizes [ : n_samples % n_folds ] += <NUM_LIT:1> <EOL> current = <NUM_LIT:0> <EOL> for fold_size in fold_sizes : <EOL> start , stop = current , current + fold_size <EOL> yield indices [ start : stop ] <EOL> current = stop <EOL> class LabelKFold ( _BaseKFold ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n_folds = <NUM_LIT:3> ) : <EOL> super ( LabelKFold , self ) . __init__ ( n_folds , shuffle = False , <EOL> random_state = None ) <EOL> def _iter_test_indices ( self , X , y , labels ) : <EOL> if labels is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> unique_labels , labels = np . unique ( labels , return_inverse = True ) <EOL> n_labels = len ( unique_labels ) <EOL> if self . n_folds > n_labels : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( self . n_folds , n_labels ) ) <EOL> n_samples_per_label = np . bincount ( labels ) <EOL> indices = np . argsort ( n_samples_per_label ) [ : : - <NUM_LIT:1> ] <EOL> n_samples_per_label = n_samples_per_label [ indices ] <EOL> n_samples_per_fold = np . zeros ( self . n_folds ) <EOL> label_to_fold = np . zeros ( len ( unique_labels ) ) <EOL> for label_index , weight in enumerate ( n_samples_per_label ) : <EOL> lightest_fold = np . argmin ( n_samples_per_fold ) <EOL> n_samples_per_fold [ lightest_fold ] += weight <EOL> label_to_fold [ indices [ label_index ] ] = lightest_fold <EOL> indices = label_to_fold [ labels ] <EOL> for f in range ( self . n_folds ) : <EOL> yield np . where ( indices == f ) [ <NUM_LIT:0> ] <EOL> class StratifiedKFold ( _BaseKFold ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n_folds = <NUM_LIT:3> , shuffle = False , random_state = None ) : <EOL> super ( StratifiedKFold , self ) . __init__ ( n_folds , shuffle , random_state ) <EOL> def _make_test_folds ( self , X , y = None , labels = None ) : <EOL> if self . shuffle : <EOL> rng = check_random_state ( self . random_state ) <EOL> else : <EOL> rng = self . random_state <EOL> y = np . asarray ( y ) <EOL> n_samples = y . shape [ <NUM_LIT:0> ] <EOL> unique_y , y_inversed = np . unique ( y , return_inverse = True ) <EOL> y_counts = bincount ( y_inversed ) <EOL> min_labels = np . min ( y_counts ) <EOL> if np . all ( self . n_folds > y_counts ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( self . n_folds ) ) <EOL> if self . n_folds > min_labels : <EOL> warnings . warn ( ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( min_labels , self . n_folds ) ) , Warning ) <EOL> per_cls_cvs = [ <EOL> KFold ( self . n_folds , shuffle = self . shuffle , <EOL> random_state = rng ) . split ( np . zeros ( max ( count , self . n_folds ) ) ) <EOL> for count in y_counts ] <EOL> test_folds = np . zeros ( n_samples , dtype = np . int ) <EOL> for test_fold_indices , per_cls_splits in enumerate ( zip ( * per_cls_cvs ) ) : <EOL> for cls , ( _ , test_split ) in zip ( unique_y , per_cls_splits ) : <EOL> cls_test_folds = test_folds [ y == cls ] <EOL> test_split = test_split [ test_split < len ( cls_test_folds ) ] <EOL> cls_test_folds [ test_split ] = test_fold_indices <EOL> test_folds [ y == cls ] = cls_test_folds <EOL> return test_folds <EOL> def _iter_test_masks ( self , X , y = None , labels = None ) : <EOL> test_folds = self . _make_test_folds ( X , y ) <EOL> for i in range ( self . n_folds ) : <EOL> yield test_folds == i <EOL> def split ( self , X , y , labels = None ) : <EOL> """<STR_LIT>""" <EOL> return super ( StratifiedKFold , self ) . split ( X , y , labels ) <EOL> class LeaveOneLabelOut ( BaseCrossValidator ) : <EOL> """<STR_LIT>""" <EOL> def _iter_test_masks ( self , X , y , labels ) : <EOL> if labels is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> labels = np . array ( labels , copy = True ) <EOL> unique_labels = np . unique ( labels ) <EOL> for i in unique_labels : <EOL> yield labels == i <EOL> def get_n_splits ( self , X , y , labels ) : <EOL> """<STR_LIT>""" <EOL> if labels is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return len ( np . unique ( labels ) ) <EOL> class LeavePLabelOut ( BaseCrossValidator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n_labels ) : <EOL> self . n_labels = n_labels <EOL> def _iter_test_masks ( self , X , y , labels ) : <EOL> if labels is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> labels = np . array ( labels , copy = True ) <EOL> unique_labels = np . unique ( labels ) <EOL> combi = combinations ( range ( len ( unique_labels ) ) , self . n_labels ) <EOL> for indices in combi : <EOL> test_index = np . zeros ( _num_samples ( X ) , dtype = np . bool ) <EOL> for l in unique_labels [ np . array ( indices ) ] : <EOL> test_index [ labels == l ] = True <EOL> yield test_index <EOL> def get_n_splits ( self , X , y , labels ) : <EOL> """<STR_LIT>""" <EOL> if labels is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return int ( comb ( len ( np . unique ( labels ) ) , self . n_labels , exact = True ) ) <EOL> class BaseShuffleSplit ( with_metaclass ( ABCMeta ) ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n_iter = <NUM_LIT:10> , test_size = <NUM_LIT:0.1> , train_size = None , <EOL> random_state = None ) : <EOL> _validate_shuffle_split_init ( test_size , train_size ) <EOL> self . n_iter = n_iter <EOL> self . test_size = test_size <EOL> self . train_size = train_size <EOL> self . random_state = random_state <EOL> def split ( self , X , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> X , y , labels = indexable ( X , y , labels ) <EOL> for train , test in self . _iter_indices ( X , y , labels ) : <EOL> yield train , test <EOL> @ abstractmethod <EOL> def _iter_indices ( self , X , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> def get_n_splits ( self , X = None , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> return self . n_iter <EOL> def __repr__ ( self ) : <EOL> return _build_repr ( self ) <EOL> class ShuffleSplit ( BaseShuffleSplit ) : <EOL> """<STR_LIT>""" <EOL> def _iter_indices ( self , X , y = None , labels = None ) : <EOL> n_samples = _num_samples ( X ) <EOL> n_train , n_test = _validate_shuffle_split ( n_samples , self . test_size , <EOL> self . train_size ) <EOL> rng = check_random_state ( self . random_state ) <EOL> for i in range ( self . n_iter ) : <EOL> permutation = rng . permutation ( n_samples ) <EOL> ind_test = permutation [ : n_test ] <EOL> ind_train = permutation [ n_test : ( n_test + n_train ) ] <EOL> yield ind_train , ind_test <EOL> class LabelShuffleSplit ( ShuffleSplit ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , n_iter = <NUM_LIT:5> , test_size = <NUM_LIT> , train_size = None , <EOL> random_state = None ) : <EOL> super ( LabelShuffleSplit , self ) . __init__ ( <EOL> n_iter = n_iter , <EOL> test_size = test_size , <EOL> train_size = train_size , <EOL> random_state = random_state ) <EOL> def _iter_indices ( self , X , y , labels ) : <EOL> if labels is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> classes , label_indices = np . unique ( labels , return_inverse = True ) <EOL> for label_train , label_test in super ( <EOL> LabelShuffleSplit , self ) . _iter_indices ( X = classes ) : <EOL> train = np . flatnonzero ( np . in1d ( label_indices , label_train ) ) <EOL> test = np . flatnonzero ( np . in1d ( label_indices , label_test ) ) <EOL> yield train , test <EOL> class StratifiedShuffleSplit ( BaseShuffleSplit ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n_iter = <NUM_LIT:10> , test_size = <NUM_LIT:0.1> , train_size = None , <EOL> random_state = None ) : <EOL> super ( StratifiedShuffleSplit , self ) . __init__ ( <EOL> n_iter , test_size , train_size , random_state ) <EOL> def _iter_indices ( self , X , y , labels = None ) : <EOL> n_samples = _num_samples ( X ) <EOL> n_train , n_test = _validate_shuffle_split ( n_samples , self . test_size , <EOL> self . train_size ) <EOL> classes , y_indices = np . unique ( y , return_inverse = True ) <EOL> n_classes = classes . shape [ <NUM_LIT:0> ] <EOL> class_counts = bincount ( y_indices ) <EOL> if np . min ( class_counts ) < <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if n_train < n_classes : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( n_train , n_classes ) ) <EOL> if n_test < n_classes : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( n_test , n_classes ) ) <EOL> rng = check_random_state ( self . random_state ) <EOL> p_i = class_counts / float ( n_samples ) <EOL> n_i = np . round ( n_train * p_i ) . astype ( int ) <EOL> t_i = np . minimum ( class_counts - n_i , <EOL> np . round ( n_test * p_i ) . astype ( int ) ) <EOL> for _ in range ( self . n_iter ) : <EOL> train = [ ] <EOL> test = [ ] <EOL> for i , class_i in enumerate ( classes ) : <EOL> permutation = rng . permutation ( class_counts [ i ] ) <EOL> perm_indices_class_i = np . where ( ( y == class_i ) ) [ <NUM_LIT:0> ] [ permutation ] <EOL> train . extend ( perm_indices_class_i [ : n_i [ i ] ] ) <EOL> test . extend ( perm_indices_class_i [ n_i [ i ] : n_i [ i ] + t_i [ i ] ] ) <EOL> if len ( train ) + len ( test ) < n_train + n_test : <EOL> missing_indices = np . where ( bincount ( train + test , <EOL> minlength = len ( y ) ) == <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> missing_indices = rng . permutation ( missing_indices ) <EOL> n_missing_train = n_train - len ( train ) <EOL> n_missing_test = n_test - len ( test ) <EOL> if n_missing_train > <NUM_LIT:0> : <EOL> train . extend ( missing_indices [ : n_missing_train ] ) <EOL> if n_missing_test > <NUM_LIT:0> : <EOL> test . extend ( missing_indices [ - n_missing_test : ] ) <EOL> train = rng . permutation ( train ) <EOL> test = rng . permutation ( test ) <EOL> yield train , test <EOL> def split ( self , X , y , labels = None ) : <EOL> """<STR_LIT>""" <EOL> return super ( StratifiedShuffleSplit , self ) . split ( X , y , labels ) <EOL> def _validate_shuffle_split_init ( test_size , train_size ) : <EOL> """<STR_LIT>""" <EOL> if test_size is None and train_size is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if test_size is not None : <EOL> if np . asarray ( test_size ) . dtype . kind == '<STR_LIT:f>' : <EOL> if test_size >= <NUM_LIT:1.> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % test_size ) <EOL> elif np . asarray ( test_size ) . dtype . kind != '<STR_LIT:i>' : <EOL> raise ValueError ( "<STR_LIT>" % test_size ) <EOL> if train_size is not None : <EOL> if np . asarray ( train_size ) . dtype . kind == '<STR_LIT:f>' : <EOL> if train_size >= <NUM_LIT:1.> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % train_size ) <EOL> elif ( np . asarray ( test_size ) . dtype . kind == '<STR_LIT:f>' and <EOL> ( train_size + test_size ) > <NUM_LIT:1.> ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( train_size + test_size ) ) <EOL> elif np . asarray ( train_size ) . dtype . kind != '<STR_LIT:i>' : <EOL> raise ValueError ( "<STR_LIT>" % train_size ) <EOL> def _validate_shuffle_split ( n_samples , test_size , train_size ) : <EOL> """<STR_LIT>""" <EOL> if ( test_size is not None and np . asarray ( test_size ) . dtype . kind == '<STR_LIT:i>' <EOL> and test_size >= n_samples ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( test_size , n_samples ) ) <EOL> if ( train_size is not None and np . asarray ( train_size ) . dtype . kind == '<STR_LIT:i>' <EOL> and train_size >= n_samples ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( train_size , n_samples ) ) <EOL> if np . asarray ( test_size ) . dtype . kind == '<STR_LIT:f>' : <EOL> n_test = ceil ( test_size * n_samples ) <EOL> elif np . asarray ( test_size ) . dtype . kind == '<STR_LIT:i>' : <EOL> n_test = float ( test_size ) <EOL> if train_size is None : <EOL> n_train = n_samples - n_test <EOL> elif np . asarray ( train_size ) . dtype . kind == '<STR_LIT:f>' : <EOL> n_train = floor ( train_size * n_samples ) <EOL> else : <EOL> n_train = float ( train_size ) <EOL> if test_size is None : <EOL> n_test = n_samples - n_train <EOL> if n_train + n_test > n_samples : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( n_train + n_test , n_samples ) ) <EOL> return int ( n_train ) , int ( n_test ) <EOL> class PredefinedSplit ( BaseCrossValidator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , test_fold ) : <EOL> self . test_fold = np . array ( test_fold , dtype = np . int ) <EOL> self . test_fold = column_or_1d ( self . test_fold ) <EOL> self . unique_folds = np . unique ( self . test_fold ) <EOL> self . unique_folds = self . unique_folds [ self . unique_folds != - <NUM_LIT:1> ] <EOL> def split ( self , X = None , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> ind = np . arange ( len ( self . test_fold ) ) <EOL> for test_index in self . _iter_test_masks ( ) : <EOL> train_index = ind [ np . logical_not ( test_index ) ] <EOL> test_index = ind [ test_index ] <EOL> yield train_index , test_index <EOL> def _iter_test_masks ( self ) : <EOL> """<STR_LIT>""" <EOL> for f in self . unique_folds : <EOL> test_index = np . where ( self . test_fold == f ) [ <NUM_LIT:0> ] <EOL> test_mask = np . zeros ( len ( self . test_fold ) , dtype = np . bool ) <EOL> test_mask [ test_index ] = True <EOL> yield test_mask <EOL> def get_n_splits ( self , X = None , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . unique_folds ) <EOL> class _CVIterableWrapper ( BaseCrossValidator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cv ) : <EOL> self . cv = cv <EOL> def get_n_splits ( self , X = None , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . cv ) <EOL> def split ( self , X = None , y = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> for train , test in self . cv : <EOL> yield train , test <EOL> def check_cv ( cv = <NUM_LIT:3> , y = None , classifier = False ) : <EOL> """<STR_LIT>""" <EOL> if cv is None : <EOL> cv = <NUM_LIT:3> <EOL> if isinstance ( cv , numbers . Integral ) : <EOL> if ( classifier and ( y is not None ) and <EOL> ( type_of_target ( y ) in ( '<STR_LIT>' , '<STR_LIT>' ) ) ) : <EOL> return StratifiedKFold ( cv ) <EOL> else : <EOL> return KFold ( cv ) <EOL> if not hasattr ( cv , '<STR_LIT>' ) or isinstance ( cv , str ) : <EOL> if not isinstance ( cv , Iterable ) or isinstance ( cv , str ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % cv ) <EOL> return _CVIterableWrapper ( cv ) <EOL> return cv <EOL> def train_test_split ( * arrays , ** options ) : <EOL> """<STR_LIT>""" <EOL> n_arrays = len ( arrays ) <EOL> if n_arrays == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> test_size = options . pop ( '<STR_LIT>' , None ) <EOL> train_size = options . pop ( '<STR_LIT>' , None ) <EOL> random_state = options . pop ( '<STR_LIT>' , None ) <EOL> stratify = options . pop ( '<STR_LIT>' , None ) <EOL> if options : <EOL> raise TypeError ( "<STR_LIT>" % str ( options ) ) <EOL> if test_size is None and train_size is None : <EOL> test_size = <NUM_LIT> <EOL> arrays = indexable ( * arrays ) <EOL> if stratify is not None : <EOL> CVClass = StratifiedShuffleSplit <EOL> else : <EOL> CVClass = ShuffleSplit <EOL> cv = CVClass ( test_size = test_size , <EOL> train_size = train_size , <EOL> random_state = random_state ) <EOL> train , test = next ( cv . split ( X = arrays [ <NUM_LIT:0> ] , y = stratify ) ) <EOL> return list ( chain . from_iterable ( ( safe_indexing ( a , train ) , <EOL> safe_indexing ( a , test ) ) for a in arrays ) ) <EOL> train_test_split . __test__ = False <EOL> def _safe_split ( estimator , X , y , indices , train_indices = None ) : <EOL> """<STR_LIT>""" <EOL> if ( hasattr ( estimator , '<STR_LIT>' ) and callable ( estimator . kernel ) and <EOL> not isinstance ( estimator . kernel , GPKernel ) ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not hasattr ( X , "<STR_LIT>" ) : <EOL> if getattr ( estimator , "<STR_LIT>" , False ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> X_subset = [ X [ index ] for index in indices ] <EOL> else : <EOL> if getattr ( estimator , "<STR_LIT>" , False ) : <EOL> if X . shape [ <NUM_LIT:0> ] != X . shape [ <NUM_LIT:1> ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if train_indices is None : <EOL> X_subset = X [ np . ix_ ( indices , indices ) ] <EOL> else : <EOL> X_subset = X [ np . ix_ ( indices , train_indices ) ] <EOL> else : <EOL> X_subset = safe_indexing ( X , indices ) <EOL> if y is not None : <EOL> y_subset = safe_indexing ( y , indices ) <EOL> else : <EOL> y_subset = None <EOL> return X_subset , y_subset <EOL> def _build_repr ( self ) : <EOL> cls = self . __class__ <EOL> init = getattr ( cls . __init__ , '<STR_LIT>' , cls . __init__ ) <EOL> init_signature = signature ( init ) <EOL> if init is object . __init__ : <EOL> args = [ ] <EOL> else : <EOL> args = sorted ( [ p . name for p in init_signature . parameters . values ( ) <EOL> if p . name != '<STR_LIT>' and p . kind != p . VAR_KEYWORD ] ) <EOL> class_name = self . __class__ . __name__ <EOL> params = dict ( ) <EOL> for key in args : <EOL> warnings . simplefilter ( "<STR_LIT>" , DeprecationWarning ) <EOL> try : <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> value = getattr ( self , key , None ) <EOL> if len ( w ) and w [ <NUM_LIT:0> ] . category == DeprecationWarning : <EOL> continue <EOL> finally : <EOL> warnings . filters . pop ( <NUM_LIT:0> ) <EOL> params [ key ] = value <EOL> return '<STR_LIT>' % ( class_name , _pprint ( params , offset = len ( class_name ) ) ) </s>
