<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from . dynamic import dynamic_import <EOL> def _construct_folds_iterator ( y , cfg , key ) : <EOL> """<STR_LIT>""" <EOL> cv_cfg = cfg [ key ] <EOL> dyn_cla = dynamic_import ( cv_cfg [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> kwargs = cv_cfg [ "<STR_LIT>" ] [ <NUM_LIT:1> ] <EOL> if cfg [ "<STR_LIT>" ] : <EOL> fg = dyn_cla ( y , ** kwargs ) <EOL> else : <EOL> fg = dyn_cla ( y . shape [ <NUM_LIT:0> ] , ** kwargs ) <EOL> return fg <EOL> def make_folds ( y , cfg , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> folds = [ ] <EOL> keys = [ ] <EOL> if cfg [ "<STR_LIT>" ] and y . ndim > <NUM_LIT:1> : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> else : <EOL> fg = _construct_folds_iterator ( y , cfg , "<STR_LIT>" ) <EOL> for i , ( train , test ) in enumerate ( fg ) : <EOL> if verbose : <EOL> print ( "<STR_LIT>" . format ( <EOL> i , np . sort ( y [ train ] ) , np . sort ( y [ test ] ) ) ) <EOL> folds . append ( ( train , test ) ) <EOL> keys . append ( "<STR_LIT>" % i ) <EOL> if cfg [ "<STR_LIT>" ] : <EOL> yi = y [ train ] <EOL> fgi = _construct_folds_iterator ( yi , cfg , "<STR_LIT>" ) <EOL> for k , ( itrain , itest ) in enumerate ( fgi ) : <EOL> if verbose : <EOL> print ( "<STR_LIT>" . format ( <EOL> k , np . sort ( yi [ itrain ] ) , np . sort ( yi [ itest ] ) ) ) <EOL> folds . append ( ( itrain , itest ) ) <EOL> keys . append ( "<STR_LIT>" % ( i , k ) ) <EOL> if cfg [ "<STR_LIT>" ] : <EOL> folds_dic = dict ( zip ( keys , folds ) , n_inner = ( k + <NUM_LIT:1> ) , n_outer = ( i + <NUM_LIT:1> ) ) <EOL> else : <EOL> folds_dic = dict ( zip ( keys , folds ) , n_outer = ( i + <NUM_LIT:1> ) ) <EOL> return folds_dic <EOL> def get_fold ( folds , outer , inner = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if inner is None : <EOL> return folds [ "<STR_LIT>" % outer ] <EOL> else : <EOL> outer_ind = folds [ "<STR_LIT>" % outer ] [ <NUM_LIT:0> ] <EOL> inner_ind = folds [ "<STR_LIT>" % ( outer , inner ) ] <EOL> return outer_ind [ inner_ind [ <NUM_LIT:0> ] ] , outer_ind [ inner_ind [ <NUM_LIT:1> ] ] <EOL> except KeyError : <EOL> raise KeyError ( "<STR_LIT>" . format ( <EOL> outer , inner ) ) <EOL> def print_fold ( train_index , test_index ) : <EOL> """<STR_LIT>""" <EOL> print ( train_index ) <EOL> print ( test_index ) <EOL> print ( "<STR_LIT>" . format ( <EOL> train_index . size , <EOL> test_index . size ) ) <EOL> print ( "<STR_LIT>" ) </s>
