#NEWLINE# def main( ) : #NEWLINE# #INDENT# settings = utils . load_settings ( 'SETTINGS.json' ) #NEWLINE# base_filepaths = ( settings [ 'file_bryan_submission' ] , settings [ 'file_miroslaw_submission' ] ) #NEWLINE# segment_weights = settings [ 'ensemble_segment_weights' ] #NEWLINE# segments = segment_weights . keys ( ) #NEWLINE# targets = segment_weights [ segments [ 0 ] ] . keys ( ) #NEWLINE# log . info ( '==========ENSEMBLE WEIGHTS (B,M)============' ) #NEWLINE# for segment in segment_weights : #NEWLINE# #INDENT# log . info ( ( segment . upper ( ) + ':' ) ) #NEWLINE# for target in segment_weights [ segment ] : #NEWLINE# #INDENT# log . info ( ( ( ( ( ( ( '    ' + target . upper ( ) ) + ' -- [' ) + segment_weights [ segment ] [ target ] [ '0' ] ) + ',' ) + segment_weights [ segment ] [ target ] [ '1' ] ) + ']' ) ) #NEWLINE# #UNINDENT# #UNINDENT# base_subs = [ ] #NEWLINE# for file in base_filepaths : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# base_subs . append ( pd . read_csv ( file ) . set_index ( [ 'id' ] , drop = False ) . sort ( ) ) #NEWLINE# log . info ( ( 'Base submission successfully loaded: %s.' % file ) ) #NEWLINE# #UNINDENT# except IOError : #NEWLINE# #INDENT# log . info ( ( 'Base submission file does not exist: %s. Run base model to generate, or update filepath.' % file ) ) #NEWLINE# sys . exit ( '---Exiting---' ) #NEWLINE# #UNINDENT# #UNINDENT# utils . line_break ( ) #NEWLINE# file = settings [ 'file_segment_ids' ] #NEWLINE# try : #NEWLINE# #INDENT# segment_ids = pd . read_csv ( file ) #NEWLINE# log . info ( ( 'Segment IDs successfully loaded from: %s.' % file ) ) #NEWLINE# #UNINDENT# except IOError : #NEWLINE# #INDENT# log . info ( ( 'Segment IDs file does not exist: %s. Update filepath in SETTINGS.json.' % file ) ) #NEWLINE# #UNINDENT# utils . line_break ( ) #NEWLINE# if ( settings [ 'avg_log_space' ] == 'y' ) : #NEWLINE# #INDENT# log . info ( 'Transforming base predictions to log space prior to averaging.' ) #NEWLINE# for i in range ( len ( base_subs ) ) : #NEWLINE# #INDENT# for target in targets : #NEWLINE# #INDENT# base_subs [ i ] [ target ] = np . log ( ( base_subs [ i ] [ target ] + 1 ) ) #NEWLINE# #UNINDENT# #UNINDENT# utils . line_break ( ) #NEWLINE# #UNINDENT# log . info ( 'Applying segment weights to base submissions then combining to create ensemble.' ) #NEWLINE# for i in range ( len ( base_subs ) ) : #NEWLINE# #INDENT# base_subs [ i ] = base_subs [ i ] . merge ( segment_ids , on = 'id' , how = 'inner' ) #NEWLINE# for segment in segments : #NEWLINE# #INDENT# for target in targets : #NEWLINE# #INDENT# base_subs [ i ] [ target ] [ ( base_subs [ i ] [ 'Segment' ] == segment ) ] *= float ( segment_weights [ segment ] [ target ] [ str ( i ) ] ) #NEWLINE# #UNINDENT# #UNINDENT# del base_subs [ i ] [ 'Segment' ] #NEWLINE# #UNINDENT# ensemble_sub = base_subs [ 0 ] . ix [ : ] #NEWLINE# for i in range ( ( len ( base_subs ) - 1 ) ) : #NEWLINE# #INDENT# for target in targets : #NEWLINE# #INDENT# ensemble_sub [ target ] += base_subs [ ( i + 1 ) ] [ target ] #NEWLINE# #UNINDENT# #UNINDENT# utils . line_break ( ) #NEWLINE# if ( settings [ 'avg_log_space' ] == 'y' ) : #NEWLINE# #INDENT# log . info ( 'Transforming ensemble predictions back to normal from log space.' ) #NEWLINE# for target in targets : #NEWLINE# #INDENT# ensemble_sub [ target ] = ( np . exp ( ensemble_sub [ target ] ) - 1 ) #NEWLINE# #UNINDENT# utils . line_break ( ) #NEWLINE# #UNINDENT# for target in targets : #NEWLINE# #INDENT# ensemble_sub [ target ] *= float ( settings [ 'target_scalars' ] [ segments ] ) #NEWLINE# #UNINDENT# timestamp = datetime . now ( ) . strftime ( '%m-%d-%y_%H%M' ) #NEWLINE# filename = ( ( ( settings [ 'dir_ensemble_submissions' ] + 'ensemble_predictions_' ) + timestamp ) + '.csv' ) #NEWLINE# ensemble_sub . to_csv ( filename , index = False ) #NEWLINE# log . info ( ( 'Ensemble submission saved: %s' % filename ) ) #NEWLINE# utils . line_break ( ) #NEWLINE# log . info ( 'Program executed successfully without error! Exiting.' )