<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> import numpy as np <EOL> import warnings <EOL> try : <EOL> from sklearn . cross_validation import ( KFold , StratifiedKFold , ShuffleSplit , <EOL> StratifiedShuffleSplit ) <EOL> except ImportError : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> from pylearn2 . compat import OrderedDict <EOL> from pylearn2 . cross_validation . blocks import StackedBlocksCV <EOL> from pylearn2 . cross_validation . subset_iterators import ( <EOL> ValidationKFold , StratifiedValidationKFold , ValidationShuffleSplit , <EOL> StratifiedValidationShuffleSplit ) <EOL> from pylearn2 . datasets . dense_design_matrix import DenseDesignMatrix <EOL> from pylearn2 . datasets . transformer_dataset import TransformerDataset <EOL> class DatasetCV ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset , subset_iterator , preprocessor = None , <EOL> fit_preprocessor = False , which_set = None , return_dict = True ) : <EOL> self . dataset = dataset <EOL> self . subset_iterator = list ( subset_iterator ) <EOL> dataset_iterator = dataset . iterator ( mode = '<STR_LIT>' , num_batches = <NUM_LIT:1> , <EOL> data_specs = dataset . data_specs , <EOL> return_tuple = True ) <EOL> self . _data = dataset_iterator . next ( ) <EOL> self . preprocessor = preprocessor <EOL> self . fit_preprocessor = fit_preprocessor <EOL> self . which_set = which_set <EOL> if which_set is not None : <EOL> which_set = np . atleast_1d ( which_set ) <EOL> assert len ( which_set ) <EOL> for label in which_set : <EOL> if label not in [ '<STR_LIT:train>' , '<STR_LIT>' , '<STR_LIT:test>' ] : <EOL> raise ValueError ( "<STR_LIT>" . format ( label ) ) <EOL> self . which_set = which_set <EOL> self . return_dict = return_dict <EOL> def get_data_subsets ( self ) : <EOL> """<STR_LIT>""" <EOL> for subsets in self . subset_iterator : <EOL> labels = None <EOL> if len ( subsets ) == <NUM_LIT:3> : <EOL> labels = [ '<STR_LIT:train>' , '<STR_LIT>' , '<STR_LIT:test>' ] <EOL> elif len ( subsets ) == <NUM_LIT:2> : <EOL> labels = [ '<STR_LIT:train>' , '<STR_LIT:test>' ] <EOL> data_subsets = OrderedDict ( ) <EOL> for i , subset in enumerate ( subsets ) : <EOL> subset_data = tuple ( data [ subset ] for data in self . _data ) <EOL> if len ( subset_data ) == <NUM_LIT:2> : <EOL> X , y = subset_data <EOL> else : <EOL> X , = subset_data <EOL> y = None <EOL> data_subsets [ labels [ i ] ] = ( X , y ) <EOL> yield data_subsets <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> for data_subsets in self . get_data_subsets ( ) : <EOL> datasets = { } <EOL> for label , data in data_subsets . items ( ) : <EOL> X , y = data <EOL> datasets [ label ] = DenseDesignMatrix ( X = X , y = y ) <EOL> if self . preprocessor is not None : <EOL> self . preprocessor . apply ( datasets [ '<STR_LIT:train>' ] , <EOL> can_fit = self . fit_preprocessor ) <EOL> for label , dataset in datasets . items ( ) : <EOL> if label == '<STR_LIT:train>' : <EOL> continue <EOL> self . preprocessor . apply ( dataset , can_fit = False ) <EOL> if self . which_set is not None : <EOL> for label , dataset in list ( datasets . items ( ) ) : <EOL> if label not in self . which_set : <EOL> del datasets [ label ] <EOL> del data_subsets [ label ] <EOL> if not len ( datasets ) : <EOL> raise ValueError ( "<STR_LIT>" + <EOL> "<STR_LIT:{}>" . format ( self . which_set ) ) <EOL> if not self . return_dict : <EOL> datasets = list ( datasets [ label ] <EOL> for label in data_subsets . keys ( ) ) <EOL> if len ( datasets ) == <NUM_LIT:1> : <EOL> datasets , = datasets <EOL> yield datasets <EOL> class StratifiedDatasetCV ( DatasetCV ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def get_y ( dataset ) : <EOL> """<STR_LIT>""" <EOL> y = np . asarray ( dataset . y ) <EOL> if y . ndim > <NUM_LIT:1> : <EOL> assert np . array_equal ( np . unique ( y ) , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> y = np . argmax ( y , axis = <NUM_LIT:1> ) <EOL> return y <EOL> class TransformerDatasetCV ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset_iterator , transformers ) : <EOL> self . dataset_iterator = dataset_iterator <EOL> self . transformers = transformers <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> for k , datasets in enumerate ( self . dataset_iterator ) : <EOL> if isinstance ( self . transformers , list ) : <EOL> transformer = self . transformers [ k ] <EOL> elif isinstance ( self . transformers , StackedBlocksCV ) : <EOL> transformer = self . transformers . select_fold ( k ) <EOL> else : <EOL> transformer = self . transformers <EOL> if isinstance ( datasets , list ) : <EOL> for i , dataset in enumerate ( datasets ) : <EOL> datasets [ i ] = TransformerDataset ( dataset , transformer ) <EOL> else : <EOL> for key , dataset in datasets . items ( ) : <EOL> datasets [ key ] = TransformerDataset ( dataset , transformer ) <EOL> yield datasets <EOL> class DatasetKFold ( DatasetCV ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset , n_folds = <NUM_LIT:3> , shuffle = False , random_state = None , <EOL> ** kwargs ) : <EOL> n = dataset . X . shape [ <NUM_LIT:0> ] <EOL> cv = KFold ( n , n_folds = n_folds , shuffle = shuffle , <EOL> random_state = random_state ) <EOL> super ( DatasetKFold , self ) . __init__ ( dataset , cv , ** kwargs ) <EOL> class StratifiedDatasetKFold ( StratifiedDatasetCV ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset , n_folds = <NUM_LIT:3> , shuffle = False , random_state = None , <EOL> ** kwargs ) : <EOL> y = self . get_y ( dataset ) <EOL> try : <EOL> cv = StratifiedKFold ( y , n_folds = n_folds , shuffle = shuffle , <EOL> random_state = random_state ) <EOL> except TypeError : <EOL> assert not shuffle and not random_state , ( <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> cv = StratifiedKFold ( y , n_folds = n_folds ) <EOL> super ( StratifiedDatasetKFold , self ) . __init__ ( dataset , cv , ** kwargs ) <EOL> class DatasetShuffleSplit ( DatasetCV ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset , n_iter = <NUM_LIT:10> , test_size = <NUM_LIT:0.1> , train_size = None , <EOL> random_state = None , ** kwargs ) : <EOL> n = dataset . X . shape [ <NUM_LIT:0> ] <EOL> cv = ShuffleSplit ( n , n_iter = n_iter , test_size = test_size , <EOL> train_size = train_size , random_state = random_state ) <EOL> super ( DatasetShuffleSplit , self ) . __init__ ( dataset , cv , ** kwargs ) <EOL> class StratifiedDatasetShuffleSplit ( StratifiedDatasetCV ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset , n_iter = <NUM_LIT:10> , test_size = <NUM_LIT:0.1> , train_size = None , <EOL> random_state = None , ** kwargs ) : <EOL> y = self . get_y ( dataset ) <EOL> cv = StratifiedShuffleSplit ( y , n_iter = n_iter , test_size = test_size , <EOL> train_size = train_size , <EOL> random_state = random_state ) <EOL> super ( StratifiedDatasetShuffleSplit , self ) . __init__ ( dataset , cv , <EOL> ** kwargs ) <EOL> class DatasetValidationKFold ( DatasetCV ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset , n_folds = <NUM_LIT:3> , shuffle = False , random_state = None , <EOL> ** kwargs ) : <EOL> n = dataset . X . shape [ <NUM_LIT:0> ] <EOL> cv = ValidationKFold ( n , n_folds , shuffle , random_state ) <EOL> super ( DatasetValidationKFold , self ) . __init__ ( dataset , cv , ** kwargs ) <EOL> class StratifiedDatasetValidationKFold ( StratifiedDatasetCV ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset , n_folds = <NUM_LIT:3> , shuffle = False , random_state = None , <EOL> ** kwargs ) : <EOL> y = self . get_y ( dataset ) <EOL> cv = StratifiedValidationKFold ( y , n_folds , shuffle , random_state ) <EOL> super ( StratifiedDatasetValidationKFold , self ) . __init__ ( dataset , cv , <EOL> ** kwargs ) <EOL> class DatasetValidationShuffleSplit ( DatasetCV ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset , n_iter = <NUM_LIT:10> , test_size = <NUM_LIT:0.1> , valid_size = None , <EOL> train_size = None , random_state = None , ** kwargs ) : <EOL> n = dataset . X . shape [ <NUM_LIT:0> ] <EOL> cv = ValidationShuffleSplit ( n , n_iter , test_size , valid_size , <EOL> train_size , random_state ) <EOL> super ( DatasetValidationShuffleSplit , self ) . __init__ ( dataset , cv , <EOL> ** kwargs ) <EOL> class StratifiedDatasetValidationShuffleSplit ( StratifiedDatasetCV ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset , n_iter = <NUM_LIT:10> , test_size = <NUM_LIT:0.1> , valid_size = None , <EOL> train_size = None , random_state = None , ** kwargs ) : <EOL> y = self . get_y ( dataset ) <EOL> cv = StratifiedValidationShuffleSplit ( y , n_iter , test_size , valid_size , <EOL> train_size , random_state ) <EOL> super ( StratifiedDatasetValidationShuffleSplit , self ) . __init__ ( dataset , <EOL> cv , <EOL> ** kwargs ) </s>
